天天看點

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. import java.io.File;

  3. import java.io.FileInputStream;

  4. import java.io.FileOutputStream;

  5. import java.io.IOException;

  6. import java.util.zip.ZipEntry;

  7. import java.util.zip.ZipOutputStream;

  8. public class CompactAlgorithm {

  9. File targetFile;

  10. public CompactAlgorithm() {}

  11. public CompactAlgorithm(File target) {

  12. targetFile = target;

  13. if (targetFile.exists())

  14. targetFile.delete();

  15. }

  16. public void zipFiles(File srcfile) {

  17. ZipOutputStream out = null;

  18. try {

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

  20. if(srcfile.isFile()){

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

  22. } else{

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

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

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

  26. }

  27. }

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

  29. } catch (Exception e) {

  30. e.printStackTrace();

  31. } finally {

  32. try {

  33. if (out != null)

  34. out.close();

  35. } catch (IOException e) {

  36. e.printStackTrace();

  37. }

  38. }

  39. }

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

  41. if (file.isDirectory()) {

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

  43. } else {

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

  45. }

  46. }

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

  48. if (!srcfile.exists())

  49. return;

  50. byte[] buf = new byte[1024];

  51. FileInputStream in = null;

  52. try {

  53. int len;

  54. in = new FileInputStream(srcfile);

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

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

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

  58. }

  59. } catch (Exception e) {

  60. e.printStackTrace();

  61. } finally {

  62. try {

  63. if (out != null)

  64. out.closeEntry();

  65. if (in != null)

  66. in.close();

  67. } catch (IOException e) {

  68. e.printStackTrace();

  69. }

  70. }

  71. }

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

  73. if (!dir.exists())

  74. return;

  75. File[] files = dir.listFiles();

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

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

  78. }

  79. }

  80. //測試

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

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

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

  84. }

  85. }

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

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

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

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