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 public 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 os.putArchiveEntry(new ZipArchiveEntry("testdata/test1.xml"));
43 Files.copy(file1.toPath(), os);
44 os.closeArchiveEntry();
45
46 os.putArchiveEntry(new ZipArchiveEntry("testdata/test2.xml"));
47 Files.copy(file2.toPath(), os);
48 os.closeArchiveEntry();
49 }
50 }
51
52 @Test
53 public void testJarUnarchive() throws Exception {
54 final File input = getFile("bla.jar");
55 try (InputStream is = Files.newInputStream(input.toPath());
56 ZipArchiveInputStream in = ArchiveStreamFactory.DEFAULT.createArchiveInputStream("jar", is)) {
57
58 ZipArchiveEntry entry = in.getNextEntry();
59 File o = newTempFile(entry.getName());
60 o.getParentFile().mkdirs();
61 Files.copy(in, o.toPath());
62
63 entry = in.getNextEntry();
64 o = newTempFile(entry.getName());
65 o.getParentFile().mkdirs();
66 Files.copy(in, o.toPath());
67
68 entry = in.getNextEntry();
69 o = newTempFile(entry.getName());
70 o.getParentFile().mkdirs();
71 Files.copy(in, o.toPath());
72 }
73 }
74
75 @Test
76 public void testJarUnarchiveAll() throws Exception {
77 final File input = getFile("bla.jar");
78 try (InputStream is = Files.newInputStream(input.toPath());
79 ArchiveInputStream<?> in = ArchiveStreamFactory.DEFAULT.createArchiveInputStream("jar", is)) {
80
81 ArchiveEntry entry = in.getNextEntry();
82 while (entry != null) {
83 final File archiveEntry = newTempFile(entry.getName());
84 archiveEntry.getParentFile().mkdirs();
85 if (entry.isDirectory()) {
86 archiveEntry.mkdir();
87 entry = in.getNextEntry();
88 continue;
89 }
90 Files.copy(in, archiveEntry.toPath());
91 entry = in.getNextEntry();
92 }
93 }
94 }
95
96 }