Skip to content

Commit 41a6b7e

Browse files
committed
💫 👽 🗑️
1 parent ac440c7 commit 41a6b7e

File tree

1 file changed

+37
-2
lines changed

1 file changed

+37
-2
lines changed

toolTest/common/src/test/java/tool/apache/common/CompressTest.java

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@
1616
public class CompressTest extends GroovyTestCase {
1717

1818

19-
public void test() {
19+
/**
20+
* 压缩
21+
*/
22+
public void testYaSuo() {
2023

2124
String userHomePath = System.getProperty("user.home");
2225
System.out.println(userHomePath);
@@ -44,9 +47,42 @@ public void test() {
4447
e.printStackTrace();
4548
}
4649

50+
}
51+
52+
// todo zsx 目录压缩
53+
public void testYaSuoDir() {
54+
String userHomePath = System.getProperty("user.home");
55+
System.out.println(userHomePath);
56+
File outputFile = new File(userHomePath + "/tmp/testZip.zip");
57+
File inputFile = new File(userHomePath + "/tmp/1");
58+
if (!inputFile.exists()) {
59+
inputFile.mkdir();
60+
}
4761

62+
try (
63+
ZipArchiveOutputStream zipOutput = new ZipArchiveOutputStream(new FileOutputStream(outputFile));
64+
) {
65+
File[] files = inputFile.listFiles();
66+
for (File file : files) {
67+
ZipArchiveEntry entry = new ZipArchiveEntry(file.getName());
68+
zipOutput.putArchiveEntry(entry);
69+
try (FileInputStream fis = new FileInputStream(file)) {
70+
byte[] buffer = new byte[1024];
71+
int len;
72+
while ((len = fis.read(buffer)) != -1) {
73+
zipOutput.write(buffer, 0, len);
74+
}
75+
}
76+
zipOutput.closeArchiveEntry();
77+
}
78+
} catch (IOException e) {
79+
e.printStackTrace();
80+
}
4881
}
4982

83+
/**
84+
* 解压
85+
*/
5086
public void testJieYa() {
5187
String userHomePath = System.getProperty("user.home");
5288
System.out.println(userHomePath);
@@ -78,7 +114,6 @@ public void testJieYa() {
78114

79115
}
80116

81-
82117
}
83118
} catch (IOException e) {
84119
throw new RuntimeException(e);

0 commit comments

Comments
 (0)