1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.commons.compress.archivers;
20
21 import java.io.File;
22 import java.io.InputStream;
23 import java.io.OutputStream;
24 import java.nio.file.Files;
25
26 import org.apache.commons.compress.AbstractTest;
27 import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
28 import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
29 import org.junit.jupiter.api.Test;
30
31 public final class JarTest extends AbstractTest {
32
33 @Test
34 void testJarArchiveCreation() throws Exception {
35 final File output = newTempFile("bla.jar");
36
37 final File file1 = getFile("test1.xml");
38 final File file2 = getFile("test2.xml");
39
40 try (OutputStream out = Files.newOutputStream(output.toPath());
41 ArchiveOutputStream<ZipArchiveEntry> os = ArchiveStreamFactory.DEFAULT.createArchiveOutputStream("jar", out)) {
42
43 os.putArchiveEntry(new ZipArchiveEntry("testdata/test1.xml"));
44 os.write(file1);
45 os.closeArchiveEntry();
46
47 os.putArchiveEntry(new ZipArchiveEntry("testdata/test2.xml"));
48 os.write(file2);
49 os.closeArchiveEntry();
50 }
51 }
52
53 @Test
54 void testJarUnarchive() throws Exception {
55 final File input = getFile("bla.jar");
56 try (InputStream is = Files.newInputStream(input.toPath());
57 ZipArchiveInputStream in = ArchiveStreamFactory.DEFAULT.createArchiveInputStream("jar", is)) {
58
59 ZipArchiveEntry entry = in.getNextEntry();
60 File o = newTempFile(entry.getName());
61 o.getParentFile().mkdirs();
62 Files.copy(in, o.toPath());
63
64 entry = in.getNextEntry();
65 o = newTempFile(entry.getName());
66 o.getParentFile().mkdirs();
67 Files.copy(in, o.toPath());
68
69 entry = in.getNextEntry();
70 o = newTempFile(entry.getName());
71 o.getParentFile().mkdirs();
72 Files.copy(in, o.toPath());
73 }
74 }
75
76 @Test
77 void testJarUnarchiveAll() throws Exception {
78 final File input = getFile("bla.jar");
79 try (InputStream is = Files.newInputStream(input.toPath());
80 ArchiveInputStream<?> in = ArchiveStreamFactory.DEFAULT.createArchiveInputStream("jar", is)) {
81 in.forEach(entry -> {
82 final File archiveEntry = newTempFile(entry.getName());
83 archiveEntry.getParentFile().mkdirs();
84 if (entry.isDirectory()) {
85 archiveEntry.mkdir();
86 } else {
87 Files.copy(in, archiveEntry.toPath());
88 }
89 });
90 }
91 }
92
93 }