歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> JAVA實現實用的ZIP壓縮與解壓

JAVA實現實用的ZIP壓縮與解壓

日期:2017/3/1 10:42:00   编辑:Linux編程

程序實現了ZIP壓縮。共分為2部分 : 壓縮(compression)與解壓(decompression)

大致功能包括用了多態,遞歸等JAVA核心技術,可以對單個文件和任意級聯文件夾進行壓縮和解壓。 需在代碼中自定義源輸入路徑和目標輸出路徑。

  1. package com.han;
  2. import java.io.*;
  3. import java.util.zip.*;
  4. /**
  5. * 程序實現了ZIP壓縮。共分為2部分 :
  6. * 壓縮(compression)與解壓(decompression)
  7. * <p>
  8. * 大致功能包括用了多態,遞歸等JAVA核心技術,可以對單個文件和任意級聯文件夾進行壓縮和解壓。
  9. * 需在代碼中自定義源輸入路徑和目標輸出路徑。
  10. * <p>
  11. * 在本段代碼中,實現的是壓縮部分;解壓部分見本包中decompression部分。
  12. * @author HAN
  13. *
  14. */
  15. public class CopyOfMyZipCompressing {
  16. private int k=1; //定義遞歸次數變量
  17. public CopyOfMyZipCompressing() {
  18. // TODO Auto-generated constructor stub
  19. }
  20. public static void main(String[] args) {
  21. // TODO Auto-generated method stub
  22. long startTime=System.currentTimeMillis();
  23. CopyOfMyZipCompressing book=new CopyOfMyZipCompressing();
  24. try {
  25. book.zip("C:\\Users\\HAN\\Desktop\\stock\\SpectreCompressed.zip", //自定義的zip輸出路徑
  26. new File("C:\\Users\\HAN\\Desktop\\CombinedSpectres.txt")); //自定義的源輸入路徑,即要壓縮的文件或文件夾
  27. } catch (Exception e) {
  28. // TODO Auto-generated catch block
  29. e.printStackTrace();
  30. }
  31. long endTime=System.currentTimeMillis();
  32. System.out.println("耗費時間: "+(endTime-startTime)+" ms");
  33. }
  34. private void zip(String zipFileName, File inputFile) throws Exception{
  35. System.out.println("壓縮中...");
  36. ZipOutputStream out=new ZipOutputStream(new FileOutputStream(zipFileName));
  37. BufferedOutputStream bo=new BufferedOutputStream(out);
  38. zip(out,inputFile, "/"+inputFile.getName(),bo);
  39. bo.close();
  40. out.close(); //輸出流關閉
  41. System.out.println("壓縮完成");
  42. }
  43. private void zip(ZipOutputStream out, File f, String base, BufferedOutputStream bo)
  44. throws Exception{ //方法重載
  45. if (f.isDirectory()){
  46. File[] fl=f.listFiles();
  47. for(int i=0;i<fl.length;i++){
  48. zip(out, fl[i],base+"/"+fl[i].getName(),bo); //遞歸遍歷子文件夾
  49. }
  50. System.out.println("第"+k+"次遞歸");
  51. k++;
  52. }else{
  53. out.putNextEntry(new ZipEntry(base)); // 創建zip壓縮進入點base
  54. System.out.println(base);
  55. FileInputStream in=new FileInputStream(f);
  56. BufferedInputStream bi=new BufferedInputStream(in);
  57. int b;
  58. while((b=bi.read())!=-1){
  59. bo.write(b); //將字節流寫入當前zip目錄
  60. }
  61. bi.close();
  62. in.close(); //輸入流關閉
  63. }
  64. }
  65. }

  1. package com.han;
  2. import java.io.*;
  3. import java.util.zip.*;
  4. /**
  5. * 程序實現了ZIP壓縮。共分為2部分 :
  6. * 壓縮(compression)與解壓(decompression)
  7. * <p>
  8. * 大致功能包括用了多態,遞歸等JAVA核心技術,可以對單個文件和任意級聯文件夾進行壓縮和解壓。
  9. * 需在代碼中自定義源輸入路徑和目標輸出路徑。
  10. * <p>
  11. * 在本段代碼中,實現的是解壓部分;壓縮部分見本包中compression部分。
  12. * @author HAN
  13. *
  14. */
  15. public class CopyOfMyzipDecompressing {
  16. public static void main(String[] args) {
  17. // TODO Auto-generated method stub
  18. long startTime=System.currentTimeMillis();
  19. try {
  20. ZipInputStream Zin=new ZipInputStream(new FileInputStream(
  21. "C:\\Users\\HAN\\Desktop\\stock\\SpectreCompressed.zip"));//輸入源zip路徑
  22. BufferedInputStream Bin=new BufferedInputStream(Zin);
  23. String Parent="C:\\Users\\HAN\\Desktop"; //輸出路徑(文件夾目錄)
  24. File Fout=null;
  25. ZipEntry entry;
  26. try {
  27. while((entry = Zin.getNextEntry())!=null && !entry.isDirectory()){
  28. Fout=new File(Parent,entry.getName());
  29. if(!Fout.exists()){
  30. (new File(Fout.getParent())).mkdirs();
  31. }
  32. FileOutputStream out=new FileOutputStream(Fout);
  33. BufferedOutputStream Bout=new BufferedOutputStream(out);
  34. int b;
  35. while((b=Bin.read())!=-1){
  36. Bout.write(b);
  37. }
  38. Bout.close();
  39. out.close();
  40. System.out.println(Fout+"解壓成功");
  41. }
  42. Bin.close();
  43. Zin.close();
  44. } catch (IOException e) {
  45. // TODO Auto-generated catch block
  46. e.printStackTrace();
  47. }
  48. } catch (FileNotFoundException e) {
  49. // TODO Auto-generated catch block
  50. e.printStackTrace();
  51. }
  52. long endTime=System.currentTimeMillis();
  53. System.out.println("耗費時間: "+(endTime-startTime)+" ms");
  54. }
  55. }
Copyright © Linux教程網 All Rights Reserved