`
wlhok
  • 浏览: 56161 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

Java 压缩解压缩文件

    博客分类:
  • Java
阅读更多

使用org.apache.tools.zip包来压缩解压缩文件。

java 代码
  1. import java.io.File;   
  2. import java.io.FileInputStream;   
  3. import java.io.FileOutputStream;   
  4. import java.io.IOException;   
  5. import java.io.InputStream;   
  6. import java.util.Enumeration;   
  7. import org.apache.tools.zip.ZipEntry;   
  8. import org.apache.tools.zip.ZipFile;   
  9. import org.apache.tools.zip.ZipOutputStream;   
  10.   
  11. public class CompressFile {   
  12.     private static CompressFile instance = new CompressFile();   
  13.        
  14.     private CompressFile() {   
  15.     }   
  16.        
  17.     public static CompressFile getInstance() {   
  18.         return instance;   
  19.     }   
  20.   
  21.     public synchronized void zip(String inputFilename, String zipFilename)   
  22.             throws IOException {   
  23.         zip(new File(inputFilename), zipFilename);   
  24.     }   
  25.        
  26.     public synchronized void zip(File inputFile, String zipFilename) throws IOException {   
  27.         ZipOutputStream out = new ZipOutputStream(new FileOutputStream(   
  28.                 zipFilename));   
  29.   
  30.         try {   
  31.             zip(inputFile, out, "");   
  32.         } catch (IOException e) {   
  33.             throw e;   
  34.         } finally {   
  35.             out.close();   
  36.         }   
  37.     }   
  38.   
  39.     private synchronized void zip(File inputFile, ZipOutputStream out, String base)   
  40.             throws IOException {   
  41.         if (inputFile.isDirectory()) {   
  42.             File[] inputFiles = inputFile.listFiles();   
  43.             out.putNextEntry(new ZipEntry(base + "/"));   
  44.             base = base.length() == 0 ? "" : base + "/";   
  45.             for (int i = 0; i < inputFiles.length; i++) {   
  46.                 zip(inputFiles[i], out, base + inputFiles[i].getName());   
  47.             }   
  48.   
  49.         } else {   
  50.             if (base.length() > 0) {   
  51.                 out.putNextEntry(new ZipEntry(base));   
  52.             } else {   
  53.                 out.putNextEntry(new ZipEntry(inputFile.getName()));   
  54.             }   
  55.   
  56.             FileInputStream in = new FileInputStream(inputFile);   
  57.             try {   
  58.                 int c;   
  59.                 byte[] by = new byte[BUFFEREDSIZE];   
  60.                 while ((c = in.read(by)) != -1) {   
  61.                     out.write(by, 0, c);   
  62.                 }   
  63.             } catch (IOException e) {   
  64.                 throw e;   
  65.             } finally {   
  66.                 in.close();   
  67.             }   
  68.         }   
  69.     }   
  70.   
  71.     public synchronized void unzip(String zipFilename, String outputDirectory)   
  72.             throws IOException {   
  73.         File outFile = new File(outputDirectory);   
  74.         if (!outFile.exists()) {   
  75.             outFile.mkdirs();   
  76.         }   
  77.   
  78.         ZipFile zipFile = new ZipFile(zipFilename);   
  79.         Enumeration en = zipFile.getEntries();   
  80.         ZipEntry zipEntry = null;   
  81.         while (en.hasMoreElements()) {   
  82.             zipEntry = (ZipEntry) en.nextElement();   
  83.             if (zipEntry.isDirectory()) {   
  84.                 // mkdir directory   
  85.                 String dirName = zipEntry.getName();   
  86.                 dirName = dirName.substring(0, dirName.length() - 1);   
  87.   
  88.                 File f = new File(outFile.getPath() + File.separator + dirName);   
  89.                 f.mkdirs();   
  90.   
  91.             } else {   
  92.                 // unzip file   
  93.                 File f = new File(outFile.getPath() + File.separator   
  94.                         + zipEntry.getName());   
  95.                 f.createNewFile();   
  96.                 InputStream in = zipFile.getInputStream(zipEntry);   
  97.                 FileOutputStream out = new FileOutputStream(f);   
  98.                 try {   
  99.                     int c;   
  100.                     byte[] by = new byte[BUFFEREDSIZE];   
  101.                     while ((c = in.read(by)) != -1) {   
  102.                         out.write(by, 0, c);   
  103.                     }   
  104.                     // out.flush();   
  105.                 } catch (IOException e) {   
  106.                     throw e;   
  107.                 } finally {   
  108.                     out.close();   
  109.                     in.close();   
  110.                 }   
  111.             }   
  112.         }   
  113.     }   
  114.   
  115.     private static final int BUFFEREDSIZE = 1024;   
  116.        
  117.     public static void main(String[] args) {      
  118.         CompressFile bean = new CompressFile();      
  119.         try {      
  120.             bean.zip("d:/temp""d:/test.zip");      
  121.      
  122.             bean.unzip("d:/test.zip""d:/out/temp");      
  123.         } catch (IOException e) {      
  124.             e.printStackTrace();      
  125.         }      
  126.     }    
  127. }   
分享到:
评论
2 楼 七色花-许愿 2013-08-08  
every 写道
借鉴
谢谢

1 楼 every 2009-02-27  
借鉴
谢谢

相关推荐

Global site tag (gtag.js) - Google Analytics