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   *   https://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      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              // file 1
43              os.putArchiveEntry(new ZipArchiveEntry("testdata/test1.xml"));
44              os.write(file1);
45              os.closeArchiveEntry();
46              // file 2
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  }