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.FileInputStream;
23 import java.io.FileOutputStream;
24 import java.io.InputStream;
25 import java.io.OutputStream;
26
27 import org.apache.commons.compress.AbstractTestCase;
28 import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
29 import org.apache.commons.compress.utils.IOUtils;
30
31 public final class JarTestCase extends AbstractTestCase {
32 public void testJarArchiveCreation() throws Exception {
33 final File output = new File(dir, "bla.jar");
34
35 final File file1 = getFile("test1.xml");
36 final File file2 = getFile("test2.xml");
37
38 final OutputStream out = new FileOutputStream(output);
39
40 final ArchiveOutputStream os = new ArchiveStreamFactory().createArchiveOutputStream("jar", out);
41
42 os.putArchiveEntry(new ZipArchiveEntry("testdata/test1.xml"));
43 IOUtils.copy(new FileInputStream(file1), os);
44 os.closeArchiveEntry();
45
46 os.putArchiveEntry(new ZipArchiveEntry("testdata/test2.xml"));
47 IOUtils.copy(new FileInputStream(file2), os);
48 os.closeArchiveEntry();
49
50 os.close();
51 }
52
53
54 public void testJarUnarchive() throws Exception {
55 final File input = getFile("bla.jar");
56 final InputStream is = new FileInputStream(input);
57 final ArchiveInputStream in = new ArchiveStreamFactory().createArchiveInputStream("jar", is);
58
59 ZipArchiveEntry entry = (ZipArchiveEntry)in.getNextEntry();
60 File o = new File(dir, entry.getName());
61 o.getParentFile().mkdirs();
62 OutputStream out = new FileOutputStream(o);
63 IOUtils.copy(in, out);
64 out.close();
65
66 entry = (ZipArchiveEntry)in.getNextEntry();
67 o = new File(dir, entry.getName());
68 o.getParentFile().mkdirs();
69 out = new FileOutputStream(o);
70 IOUtils.copy(in, out);
71 out.close();
72
73 entry = (ZipArchiveEntry)in.getNextEntry();
74 o = new File(dir, entry.getName());
75 o.getParentFile().mkdirs();
76 out = new FileOutputStream(o);
77 IOUtils.copy(in, out);
78 out.close();
79
80 in.close();
81 is.close();
82 }
83
84 public void testJarUnarchiveAll() throws Exception {
85 final File input = getFile("bla.jar");
86 final InputStream is = new FileInputStream(input);
87 final ArchiveInputStream in = new ArchiveStreamFactory().createArchiveInputStream("jar", is);
88
89 ArchiveEntry entry = in.getNextEntry();
90 while (entry != null) {
91 File archiveEntry = new File(dir, entry.getName());
92 archiveEntry.getParentFile().mkdirs();
93 if(entry.isDirectory()){
94 archiveEntry.mkdir();
95 entry = in.getNextEntry();
96 continue;
97 }
98 OutputStream out = new FileOutputStream(archiveEntry);
99 IOUtils.copy(in, out);
100 out.close();
101 entry = in.getNextEntry();
102 }
103
104 in.close();
105 is.close();
106 }
107
108 }