java实现 zip解压缩
程序实现了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. }
java实现 zip解压缩的更多相关文章
- Java压缩技术(三) ZIP解压缩——Java原生实现
原文:http://snowolf.iteye.com/blog/642492 JavaEye的朋友跟我说:“你一口气把ZIP压缩和解压缩都写到一个帖子里,我看起来很累,不如分开好阅读”.ok,面向读 ...
- 利用Java进行zip文件压缩与解压缩
摘自: https://www.cnblogs.com/alphajuns/p/12442315.html 工具类: package com.alphajuns.util; import java.i ...
- [Java 基础] 使用java.util.zip包压缩和解压缩文件
reference : http://www.open-open.com/lib/view/open1381641653833.html Java API中的import java.util.zip ...
- Java操作zip压缩和解压缩文件工具类
需要用到ant.jar(这里使用的是ant-1.6.5.jar) import java.io.File; import java.io.FileInputStream; import java.io ...
- java.util.zip压缩打包文件总结二: ZIP解压技术
一.简述 解压技术和压缩技术正好相反,解压技术要用到的类:由ZipInputStream通过read方法对数据解压,同时需要通过CheckedInputStream设置冗余校验码,如: Checked ...
- Android中的Zip解压缩
import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import ...
- java.util.zip.Deflater 压缩 inflater解压 实例
原文:java压缩解压缩类实例[转] package com.example.helloworld; import java.io.ByteArrayOutputStream; import java ...
- Java用ZIP格式压缩和解压缩文件
转载:java jdk实例宝典 感觉讲的非常好就转载在这保存! java.util.zip包实现了Zip格式相关的类库,使用格式zip格式压缩和解压缩文件的时候,须要导入该包. 使用zipoutput ...
- android zip解压缩
android zip解压缩 public class ZipUtils { public ZipUtils() { } /* 以输入流的形式解压 */ public static void UnZ ...
随机推荐
- 学习Spring.Net_1
Spring.Net是一个轻量级的控制反转(IoC)和面向切面编程(AOP)技术的容器框架 一.控制反转(Inversion of Control,IoC),也叫依赖注入(Dependency In ...
- Oracle 数据库 导入导出空表解决办法!
expdp导出:(打开CMD) 先创建(任意盘符):\oracle_data 文件夹 1.sqlplus / as sysdba;2.create or replace directory d_nam ...
- yield关键字的使用
yield的中文是什么意思呢? 在金山词霸上面的翻译是: vt.屈服,投降: 生产: 获利: 不再反对 vi.放弃,屈服: 生利: 退让,退位 n.产量,产额: 投资的收益: 屈服,击穿: 产品 个人 ...
- Android基础Activity篇——Toast
1.Toast Toast在英文中有烤面包的意思,而在安卓开发中是用来提醒用户的消息显示.我猜这里之所以用Toast为该功能命名可能是因为消息的弹出方式就像面包烤好了从面包机中弹出来一样. 2.使用T ...
- 将零散文件使用ICSharpCode.SharpZipLib压缩打包后一次性下载
public static Stream CreateZip(List<string> listPath, int level = 5) { MemoryStream mstream = ...
- 新版mysql 5.7的group_by非常不和谐
sqlalchemy.exc.OperationalError OperationalError: (_mysql_exceptions.OperationalError) (1055, " ...
- vm中efi模式安装windows10
选择dvd: 界面出现“Press any key to boot from CD or DVD”时,再迅速按下任意键就OK了.
- UE4工具
COMMON CONTAINERS TARRAY (Engine\Source\Runtime\Core\Public\Containers\Array.h) TSET (Engine\Source\ ...
- leetcode:回溯——permutation-sequence,
1. permutation-sequence 顺序排列第k个序列 The set[1,2,3,…,n]contains a total of n! unique permutations. By l ...
- IOS 多线程-NSThread 和线程状态
@interface HMViewController () - (IBAction)btnClick; @end @implementation HMViewController - (void)v ...