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.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  }