Java压缩/解压ZIP


使用ant.jar中的org.apache.tools.zip.*

[java]
  1.   
  2. import java.io.BufferedInputStream;  
  3. import java.io.File;  
  4. import java.io.FileInputStream;  
  5. import java.io.FileOutputStream;  
  6. import java.io.InputStream;  
  7. import java.util.Enumeration;  
  8.   
  9. import org.apache.tools.zip.ZipEntry;  
  10. import org.apache.tools.zip.ZipFile;  
  11. import org.apache.tools.zip.ZipOutputStream;  
  12.   
  13. public class ZipUtil {  
  14.       
  15.     public boolean zip(String zipFilePath,String[] fromZipFileArray) throws Exception  
  16.     {  
  17.         File toZipFile = new File(zipFilePath);  
  18.         if (!toZipFile.exists())  
  19.         {  
  20.             toZipFile.createNewFile();  
  21.         }  
  22.         ZipOutputStream out = new ZipOutputStream(new FileOutputStream(toZipFile));  
  23.         for (String str : fromZipFileArray)  
  24.         {  
  25.             this.writeToZip(out, new File(str), "");  
  26.         }  
  27.         out.close();  
  28.         return true;  
  29.     }  
  30.       
  31.     private void writeToZip(ZipOutputStream out,File fromZipFile,String base) throws Exception  
  32.     {  
  33.         if (fromZipFile.isDirectory())  
  34.         {  
  35.             for (File file : fromZipFile.listFiles())  
  36.             {  
  37.                 this.writeToZip(out, file, base + fromZipFile.getName() + File.separator);  
  38.             }  
  39.         }  
  40.         else  
  41.         {  
  42.             //add this file   
  43.             this.addFileToZip(out,fromZipFile,base + fromZipFile.getName());  
  44.         }  
  45.     }  
  46.       
  47.     private void addFileToZip(ZipOutputStream out,File file,String base) throws Exception  
  48.     {  
  49.         byte[] buff = new byte[1024];  
  50.         int bytesRead = -1;  
  51.         ZipEntry entry = new ZipEntry(base);  
  52.         out.putNextEntry(entry);  
  53.         InputStream in = new BufferedInputStream(new FileInputStream(file));  
  54.         while (-1 != (bytesRead = in.read(buff, 0, buff.length))) {  
  55.             out.write(buff, 0, bytesRead);  
  56.         }  
  57.         in.close();  
  58.         out.flush();  
  59.     }  
  60.       
  61.     public void unzip(String zipFilePath,String unzipFilePath) throws Exception  
  62.     {  
  63.         ZipFile zipfile = new ZipFile(zipFilePath,"GB2312");  
  64.         Enumeration enu = zipfile.getEntries();  
  65.         while(enu.hasMoreElements())  
  66.         {  
  67.             ZipEntry entry = (ZipEntry)enu.nextElement();  
  68.             this.writeToDir(zipfile, entry, new File(unzipFilePath + File.separator + entry.getName()));  
  69.               
  70.         }  
  71.     }  
  72.       
  73.     private void writeToDir(ZipFile zip, ZipEntry entry, File toFile) throws Exception  
  74.     {  
  75.         if (!entry.isDirectory()) {  
  76.             File file = toFile.getParentFile();  
  77.             if (!file.exists())  
  78.             {  
  79.                 file.mkdirs();  
  80.             }  
  81.             FileOutputStream fos = new FileOutputStream(toFile);  
  82.             byte[] buffer = new byte[1024];  
  83.             InputStream is = zip.getInputStream(entry);  
  84.             int len;  
  85.             while((len = is.read(buffer,0,buffer.length)) != -1)  
  86.             {  
  87.                 fos.write(buffer,0,len);  
  88.             }  
  89.             fos.close();  
  90.         }  
  91.     }  
  92. }  
调用方法

[java]

  1. new ZipUtil().zip("D:\\ziptest.zip"new String[]{"D:\\ziptest"});  
  2. new ZipUtil().unzip("D:\\ziptest.zip""E:\\");  

相关内容