View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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 }