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

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

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

最新文章

  1. MySQL主从复制
  2. JS实现图片预加载无需等待
  3. snprintf 使用注意
  4. mysql经纬度查询并且计算2KM范围内附近用户的sql查询性能优化实例教程
  5. 使用cow将socks5代理转为http代理(Windows版)
  6. BZOJ-3282 Tree Link-Cut-Tree(似乎树链剖分亦可)
  7. 使用 Entity Framework
  8. 流行的MySql版本
  9. Android中界面实现全屏显示的两种方式
  10. (转)Ubuntu中让终端对于历史输出的内容保持足够长
  11. 如果在文档已完成加载后执行 document.write,整个 HTML 页面将被覆盖
  12. 【从翻译mos文章】oracle linux 和外部存储系统 关系
  13. 【java基础之jdk源码】Object
  14. Hibernate 中Criteria Query查询详解【转】
  15. C#封装程序集属性方法注释说明
  16. Spring中Bean的生命周期讨论
  17. AI - TensorFlow - 起步(Start)
  18. 老代码多=过度耦合=if else?阿里巴巴工程师这样捋直老代码
  19. 使用MySQL workbench 和Excel表之间的数据互相导出
  20. 软件测试-homework3

热门文章

  1. SQLite的使用
  2. 零基础逆向工程37_Win32_11_事件_线程同步
  3. python+selenium第一个脚本
  4. selenium Element not found in the cache - perhaps the page has changed since it was looked up接解决
  5. Quartz .Net(定时框架):
  6. April 14 2017 Week 15 Friday
  7. Django QuestSet API (官方文档)
  8. python 3+djanjo 2.0.7简单学习(二)--创建数据库和模型
  9. CSS 负边距读后感
  10. override与重载的区别