简单测试Demo:如何用Java压缩文件夹和文件
标签:private vat complete 目录结构 cep 运行 压缩包 leo public
一、直接贴出测试代码
1 package com.jason.zip;
2
3 import java.io.File;
4 import java.io.FileInputStream;
5 import java.io.FileOutputStream;
6 import java.io.IOException;
7 import java.io.OutputStream;
8 import java.util.ArrayList;
9 import java.util.List;
10 import java.util.zip.ZipEntry;
11 import java.util.zip.ZipOutputStream;
12 /**
13 * 压缩文件夹或批量压缩文件
14 * @function
15 * @author 小风微凉
16 * @time 2018-9-14 下午2:12:45
17 */
18 public class ZipAction {
19 private static final int BUFFER_SIZE = 2 * 1024;
20 /**
21 * 压缩成ZIP
22 * @param srcDir 压缩文件夹路径
23 * @param out 压缩文件输出流
24 * @param KeepDirStructure 是否保留原来的目录结构,true:保留目录结构;
25 * false:所有文件跑到压缩包根目录下(注意:不保留目录结构可能会出现同名文件,会压缩失败)
26 * @throws RuntimeException 压缩失败会抛出运行时异常
27 */
28 public static void toZip(String srcDir, OutputStream out, boolean KeepDirStructure)
29 throws RuntimeException{
30 long start = System.currentTimeMillis();
31 ZipOutputStream zos = null ;
32 try {
33 zos = new ZipOutputStream(out);
34 File sourceFile = new File(srcDir);
35 compress(sourceFile,zos,sourceFile.getName(),KeepDirStructure);
36 long end = System.currentTimeMillis();
37 System.out.println("压缩完成,耗时:" + (end - start) +" ms");
38 } catch (Exception e) {
39 throw new RuntimeException("zip error from ZipAction",e);
40 }finally{
41 if(zos != null){
42 try {
43 zos.close();
44 } catch (IOException e) {
45 e.printStackTrace();
46 }
47 }
48 }
49 }
50
51 /**
52 * 压缩成ZIP 方法2
53 * @param srcFiles 需要压缩的文件列表
54 * @param out 压缩文件输出流
55 * @throws RuntimeException 压缩失败会抛出运行时异常
56 */
57 public static void toZip(List srcFiles , OutputStream out)throws RuntimeException {
58 long start = System.currentTimeMillis();
59 ZipOutputStream zos = null ;
60 try {
61 zos = new ZipOutputStream(out);
62 for (File srcFile : srcFiles) {
63 byte[] buf = new byte[BUFFER_SIZE];
64 zos.putNextEntry(new ZipEntry(srcFile.getName()));
65 int len;
66 FileInputStream in = new FileInputStream(srcFile);
67 while ((len = in.read(buf)) != -1){
68 zos.write(buf, 0, len);
69 }
70 zos.closeEntry();
71 in.close();
72 }
73 long end = System.currentTimeMillis();
74 System.out.println("压缩完成,耗时:" + (end - start) +" ms");
75 } catch (Exception e) {
76 throw new RuntimeException("zip error from ZipAction",e);
77 }finally{
78 if(zos != null){
79 try {
80 zos.close();
81 } catch (IOException e) {
82 e.printStackTrace();
83 }
84 }
85 }
86 }
87 /**
88 * 递归压缩方法
89 * @param sourceFile 源文件
90 * @param zos zip输出流
91 * @param name 压缩后的名称
92 * @param KeepDirStructure 是否保留原来的目录结构,true:保留目录结构;
93 * false:所有文件跑到压缩包根目录下(注意:不保留目录结构可能会出现同名文件,会压缩失败)
94 * @throws Exception
95 */
96 private static void compress(File sourceFile, ZipOutputStream zos, String name,
97 boolean KeepDirStructure) throws Exception{
98 byte[] buf = new byte[BUFFER_SIZE];
99 if(sourceFile.isFile()){
100 // 向zip输出流中添加一个zip实体,构造器中name为zip实体的文件的名字
101 zos.putNextEntry(new ZipEntry(name));
102 // copy文件到zip输出流中
103 int len;
104 FileInputStream in = new FileInputStream(sourceFile);
105 while ((len = in.read(buf)) != -1){
106 zos.write(buf, 0, len);
107 }
108 // Complete the entry
109 zos.closeEntry();
110 in.close();
111 } else {
112 File[] listFiles = sourceFile.listFiles();
113 if(listFiles == null || listFiles.length == 0){
114 // 需要保留原来的文件结构时,需要对空文件夹进行处理
115 if(KeepDirStructure){
116 // 空文件夹的处理
117 zos.putNextEntry(new ZipEntry(name + "/"));
118 // 没有文件,不需要文件的copy
119 zos.closeEntry();
120 }
121 }else {
122 for (File file : listFiles) {
123 // 判断是否需要保留原来的文件结构
124 if (KeepDirStructure) {
125 // 注意:file.getName()前面需要带上父文件夹的名字加一斜杠,
126 // 不然最后压缩包中就不能保留原来的文件结构,即:所有文件都跑到压缩包根目录下了
127 compress(file, zos, name + "/" + file.getName(),KeepDirStructure);
128 } else {
129 compress(file, zos, file.getName(),KeepDirStructure);
130 }
131 }
132 }
133 }
134 }
135 public static void main(String[] args) throws Exception {
136 /** 测试压缩方法1 */
137 FileOutputStream fos1 = new FileOutputStream(new File("C:/Users/Jason/Desktop/log/innermanage.zip"));
138 ZipAction.toZip("C:/Users/Jason/Desktop/log", fos1,true);
139 /** 测试压缩方法2 */
140 List fileList = new ArrayList();
141 fileList.add(new File("C:/Users/Jason/Desktop/log/innermanage/TestAction - 副本.java"));
142 fileList.add(new File("C:/Users/Jason/Desktop/log/innermanage/TestAction.java"));
143 FileOutputStream fos2 = new FileOutputStream(new File("C:/Users/Jason/Desktop/log/test.zip"));
144 ZipAction.toZip(fileList, fos2);
145 }
146 }
简单测试Demo:如何用Java压缩文件夹和文件
标签:private vat complete 目录结构 cep 运行 压缩包 leo public
原文地址:https://www.cnblogs.com/newwind/p/9680253.html
评论