Java zip工具类

解压缩zip

  1. package com.test;  
  2. import java.io.File;  
  3. import java.io.FileInputStream;  
  4. import java.io.FileOutputStream;  
  5. import java.io.IOException;  
  6. import java.io.InputStream;  
  7. import org.apache.tools.zip.ZipOutputStream;  
  8. /** 
  9. * @author cai
  10. * @date 2014年10月9日 上午8:04:51 
  11. */  
  12. public final class ZipUtils {  
  13. public static void main(String[] args) throws Exception {  
  14. // ZipFile("/home/k/Documents/testzip/宽屏透明html5产品展示.zip", "/home/k/Documents/testzip/index.html");  
  15. unZipFile("/home/k/Documents/testzip/宽屏透明html5产品展示.zip",  
  16. "/home/k/Documents/testzip/zip");  
  17. }  
  18. public static void zip(ZipOutputStream out, File f, String base,  
  19. boolean first) throws Exception {  
  20. if (first) {  
  21. if (f.isDirectory()) {  
  22. out.putNextEntry(new org.apache.tools.zip.ZipEntry("/"));  
  23. base = base + f.getName();  
  24. first = false;  
  25. else  
  26. base = f.getName();  
  27. }  
  28. if (f.isDirectory()) {  
  29. File[] fl = f.listFiles();  
  30. base = base + "/";  
  31. for (int i = 0; i < fl.length; i++) {  
  32. zip(out, fl[i], base + fl[i].getName(), first);  
  33. }  
  34. else {  
  35. out.putNextEntry(new org.apache.tools.zip.ZipEntry(base));  
  36. FileInputStream in = new FileInputStream(f);  
  37. int b;  
  38. System.out.println(base);  
  39. while ((b = in.read()) != -1) {  
  40. out.write(b);  
  41. }  
  42. in.close();  
  43. }  
  44. }  



  1. @SuppressWarnings("unchecked")  
  2. public static void unZipFileByOpache(org.apache.tools.zip.ZipFile zipFile,  
  3. String unZipRoot) throws Exception, IOException {  
  4. java.util.Enumeration e = zipFile.getEntries();  
  5. System.out.println(zipFile.getEncoding());  
  6. org.apache.tools.zip.ZipEntry zipEntry;  
  7. while (e.hasMoreElements()) {  
  8. zipEntry = (org.apache.tools.zip.ZipEntry) e.nextElement();  
  9. InputStream fis = zipFile.getInputStream(zipEntry);  
  10. if (zipEntry.isDirectory()) {  
  11. else {  
  12. File file = new File(unZipRoot + File.separator  
  13. + zipEntry.getName());  
  14. File parentFile = file.getParentFile();  
  15. parentFile.mkdirs();  
  16. FileOutputStream fos = new FileOutputStream(file);  
  17. byte[] b = new byte[1024];  
  18. int len;  
  19. while ((len = fis.read(b, 0, b.length)) != -1) {  
  20. fos.write(b, 0, len);  
  21. }  
  22. fos.close();  
  23. fis.close();  
  24. }  
  25. }  
  26. }  
  27. public static void ZipFile(String zipFileName, String inputFileName)  
  28. throws Exception {  
  29. org.apache.tools.zip.ZipOutputStream out = new org.apache.tools.zip.ZipOutputStream(  
  30. new FileOutputStream(zipFileName));  
  31. out.setEncoding("gbk");// 设置的和文件名字格式一样或开发环境编码设置一样的话就能正常显示了  
  32. File inputFile = new File(inputFileName);  
  33. zip(out, inputFile, ""true);  
  34. System.out.println("zip done");  
  35. out.close();  
  36. }  
  37. public static void unZipFile(String unZipFileName, String unZipPath)  
  38. throws Exception {  
  39. org.apache.tools.zip.ZipFile zipFile = new org.apache.tools.zip.ZipFile(  
  40. unZipFileName, "gbk");  
  41. unZipFileByOpache(zipFile, unZipPath);  
  42. System.out.println("unZip Ok");  
  43. }  
  44. }

<a target="_blank" href="http://shang.qq.com/wpa/qunwpa?idkey=5da56147c6ed58a2875077d3395b30bf8723b1f7e7196e27102351389f5b3952"><img border="0" src="http://pub.idqqimg.com/wpa/images/group.png" alt="Java交流" title="Java交流"></a>

你可能感兴趣的:(java,java,zip工具类)