1. 程式人生 > >Java實現zip檔案壓縮(單個檔案、資料夾以及檔案和資料夾的組合壓縮)

Java實現zip檔案壓縮(單個檔案、資料夾以及檔案和資料夾的組合壓縮)

Java實現zip檔案壓縮(單個檔案、資料夾以及檔案和資料夾的組合壓縮)

2016年10月04日 23:22:24 ljheee 閱讀數:13215 標籤: 壓縮javazip 更多

個人分類: Java應用

版權宣告:本文為博主原創文章,未經博主允許不得轉載。 https://blog.csdn.net/ljheee/article/details/52736035

Java實現zip檔案壓縮(單個檔案、資料夾以及檔案和資料夾的組合壓縮)

 

 

 
  1. package com.ljheee.ziptool.core;

  2.  
  3. import java.io.File;

  4. import java.io.FileInputStream;

  5. import java.io.FileOutputStream;

  6. import java.io.IOException;

  7. import java.util.zip.ZipEntry;

  8. import java.util.zip.ZipOutputStream;

  9.  
  10. /**

  11. * 壓縮演算法類

  12. * 實現檔案壓縮,資料夾壓縮,以及檔案和資料夾的混合壓縮

  13. * @author ljheee

  14. *

  15. */

  16. public class CompactAlgorithm {

  17.  
  18. /**

  19. * 完成的結果檔案--輸出的壓縮檔案

  20. */

  21. File targetFile;

  22.  
  23. public CompactAlgorithm() {}

  24.  
  25. public CompactAlgorithm(File target) {

  26. targetFile = target;

  27. if (targetFile.exists())

  28. targetFile.delete();

  29. }

  30.  
  31. /**

  32. * 壓縮檔案

  33. *

  34. * @param srcfile

  35. */

  36. public void zipFiles(File srcfile) {

  37.  
  38. ZipOutputStream out = null;

  39. try {

  40. out = new ZipOutputStream(new FileOutputStream(targetFile));

  41.  
  42. if(srcfile.isFile()){

  43. zipFile(srcfile, out, "");

  44. } else{

  45. File[] list = srcfile.listFiles();

  46. for (int i = 0; i < list.length; i++) {

  47. compress(list[i], out, "");

  48. }

  49. }

  50.  
  51. System.out.println("壓縮完畢");

  52. } catch (Exception e) {

  53. e.printStackTrace();

  54. } finally {

  55. try {

  56. if (out != null)

  57. out.close();

  58. } catch (IOException e) {

  59. e.printStackTrace();

  60. }

  61. }

  62. }

  63.  
  64. /**

  65. * 壓縮資料夾裡的檔案

  66. * 起初不知道是檔案還是資料夾--- 統一呼叫該方法

  67. * @param file

  68. * @param out

  69. * @param basedir

  70. */

  71. private void compress(File file, ZipOutputStream out, String basedir) {

  72. /* 判斷是目錄還是檔案 */

  73. if (file.isDirectory()) {

  74. this.zipDirectory(file, out, basedir);

  75. } else {

  76. this.zipFile(file, out, basedir);

  77. }

  78. }

  79.  
  80. /**

  81. * 壓縮單個檔案

  82. *

  83. * @param srcfile

  84. */

  85. public void zipFile(File srcfile, ZipOutputStream out, String basedir) {

  86. if (!srcfile.exists())

  87. return;

  88.  
  89. byte[] buf = new byte[1024];

  90. FileInputStream in = null;

  91.  
  92. try {

  93. int len;

  94. in = new FileInputStream(srcfile);

  95. out.putNextEntry(new ZipEntry(basedir + srcfile.getName()));

  96.  
  97. while ((len = in.read(buf)) > 0) {

  98. out.write(buf, 0, len);

  99. }

  100. } catch (Exception e) {

  101. e.printStackTrace();

  102. } finally {

  103. try {

  104. if (out != null)

  105. out.closeEntry();

  106. if (in != null)

  107. in.close();

  108. } catch (IOException e) {

  109. e.printStackTrace();

  110. }

  111. }

  112. }

  113.  
  114. /**

  115. * 壓縮資料夾

  116. * @param dir

  117. * @param out

  118. * @param basedir

  119. */

  120. public void zipDirectory(File dir, ZipOutputStream out, String basedir) {

  121. if (!dir.exists())

  122. return;

  123.  
  124. File[] files = dir.listFiles();

  125. for (int i = 0; i < files.length; i++) {

  126. /* 遞迴 */

  127. compress(files[i], out, basedir + dir.getName() + "/");

  128. }

  129. }

  130.  
  131.  
  132. //測試

  133. public static void main(String[] args) {

  134. File f = new File("E:/Study/Java");

  135. new CompactAlgorithm(new File( "D:/test",f.getName()+".zip")).zipFiles(f);

  136. }

  137.  
  138. }

 

 

        完整工程實現介面化,通過介面完成對檔案的壓縮和解壓,如下圖:

 

Java實現zip檔案解壓[到指定目錄]http://blog.csdn.net/ljheee/article/details/52736091

完整工程:https://github.com/ljheee/MyZip1.0