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

Java实现zip文件压缩(单个文件、文件夹以及文件和文件夹的组合压缩)

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