ITeye博客

JAVA实现实用的ZIP压缩与解压

ssxxjjii · 2013-04-07 22:40 · core java · 互联网 · Markdown

程序实现了ZIP压缩。共分为2部分 : 压缩(compression)与解压(decompression)

大致功能包括用了多态,递归等JAVA核心技术,可以对单个文件和任意级联文件夹进行压缩和解压。 需在代码中自定义源输入路径和目标输出路径。 

[java] view plaincopy

  1. package com.han;
  2. import java.io.*;
  3. import java.util.zip.*;
  4. /**
  5. * 程序实现了ZIP压缩。共分为2部分 : 压缩(compression)与解压(decompression)
  6. * <p>
  7. * 大致功能包括用了多态,递归等JAVA核心技术,可以对单个文件和任意级联文件夹进行压缩和解压。 需在代码中自定义源输入路径和目标输出路径。
  8. * <p>
  9. * 在本段代码中,实现的是压缩部分;解压部分见本包中Decompression部分。
  10. *
  11. * @author HAN
  12. *
  13. */
  14. public class MyZipCompressing {
  15. private int k = 1; // 定义递归次数变量
  16. public MyZipCompressing() {
  17. // TODO Auto-generated constructor stub
  18. }
  19. /**
  20. * @param args
  21. */
  22. public static void main(String[] args) {
  23. // TODO Auto-generated method stub
  24. MyZipCompressing book = new MyZipCompressing();
  25. try {
  26. book.zip("C:\\Users\\Gaowen\\Desktop\\ZipTestCompressing.zip",
  27. new File("C:\\Users\\Gaowen\\Documents\\Tencent Files"));
  28. } catch (Exception e) {
  29. // TODO Auto-generated catch block
  30. e.printStackTrace();
  31. }
  32. }
  33. private void zip(String zipFileName, File inputFile) throws Exception {
  34. System.out.println("压缩中...");
  35. ZipOutputStream out = new ZipOutputStream(new FileOutputStream(
  36. 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,
  44. BufferedOutputStream bo) throws Exception { // 方法重载
  45. if (f.isDirectory()) {
  46. File[] fl = f.listFiles();
  47. if (fl.length == 0) {
  48. out.putNextEntry(new ZipEntry(base + "/")); // 创建zip压缩进入点base
  49. System.out.println(base + "/");
  50. }
  51. for (int i = 0; i < fl.length; i++) {
  52. zip(out, fl[i], base + "/" + fl[i].getName(), bo); // 递归遍历子文件夹
  53. }
  54. System.out.println("第" + k + "次递归");
  55. k++;
  56. } else {
  57. out.putNextEntry(new ZipEntry(base)); // 创建zip压缩进入点base
  58. System.out.println(base);
  59. FileInputStream in = new FileInputStream(f);
  60. BufferedInputStream bi = new BufferedInputStream(in);
  61. int b;
  62. while ((b = bi.read()) != -1) {
  63. bo.write(b); // 将字节流写入当前zip目录
  64. }
  65. bi.close();
  66. in.close(); // 输入流关闭
  67. }
  68. }
  69. }

 

[java] view plaincopy

  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. }

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。