歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Java壓縮/解壓ZIP

Java壓縮/解壓ZIP

日期:2017/3/1 10:37:39   编辑:Linux編程

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

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

[java]

  1. new ZipUtil().zip("D:\\ziptest.zip", new String[]{"D:\\ziptest"});
  2. new ZipUtil().unzip("D:\\ziptest.zip", "E:\\");
Copyright © Linux教程網 All Rights Reserved