|
16 | 16 | public class CompressTest extends GroovyTestCase { |
17 | 17 |
|
18 | 18 |
|
19 | | - public void test() { |
| 19 | + /** |
| 20 | + * 压缩 |
| 21 | + */ |
| 22 | + public void testYaSuo() { |
20 | 23 |
|
21 | 24 | String userHomePath = System.getProperty("user.home"); |
22 | 25 | System.out.println(userHomePath); |
@@ -44,9 +47,42 @@ public void test() { |
44 | 47 | e.printStackTrace(); |
45 | 48 | } |
46 | 49 |
|
| 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 | + } |
47 | 61 |
|
| 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 | + } |
48 | 81 | } |
49 | 82 |
|
| 83 | + /** |
| 84 | + * 解压 |
| 85 | + */ |
50 | 86 | public void testJieYa() { |
51 | 87 | String userHomePath = System.getProperty("user.home"); |
52 | 88 | System.out.println(userHomePath); |
@@ -78,7 +114,6 @@ public void testJieYa() { |
78 | 114 |
|
79 | 115 | } |
80 | 116 |
|
81 | | - |
82 | 117 | } |
83 | 118 | } catch (IOException e) { |
84 | 119 | throw new RuntimeException(e); |
|
0 commit comments