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.compressors.pack200;
20  
21  import java.io.File;
22  import java.io.InputStream;
23  import java.nio.file.Files;
24  import java.nio.file.StandardCopyOption;
25  import java.util.HashMap;
26  
27  import org.apache.commons.compress.AbstractTest;
28  import org.apache.commons.compress.archivers.ArchiveEntry;
29  import org.apache.commons.compress.archivers.ArchiveInputStream;
30  import org.apache.commons.compress.archivers.ArchiveStreamFactory;
31  import org.junit.jupiter.api.Test;
32  
33  public final class Pack200UtilsTest extends AbstractTest {
34  
35      @Test
36      public void testNormalize() throws Throwable {
37          final File input = getFile("bla.jar");
38          final File output = createTempFile();
39          Pack200Utils.normalize(input, output, new HashMap<>());
40          try (InputStream is = Files.newInputStream(output.toPath());
41                  ArchiveInputStream<?> in = ArchiveStreamFactory.DEFAULT.createArchiveInputStream("jar", is)) {
42              ArchiveEntry entry = in.getNextEntry();
43              while (entry != null) {
44                  final File archiveEntry = newTempFile(entry.getName());
45                  archiveEntry.getParentFile().mkdirs();
46                  if (entry.isDirectory()) {
47                      archiveEntry.mkdir();
48                      entry = in.getNextEntry();
49                      continue;
50                  }
51                  Files.copy(in, archiveEntry.toPath());
52                  entry = in.getNextEntry();
53              }
54          }
55      }
56  
57      @Test
58      public void testNormalizeInPlace() throws Throwable {
59          final File input = getFile("bla.jar");
60          final File output = createTempFile();
61          try (InputStream is = Files.newInputStream(input.toPath())) {
62              Files.copy(is, output.toPath(), StandardCopyOption.REPLACE_EXISTING);
63          }
64          Pack200Utils.normalize(output);
65          try (InputStream is = Files.newInputStream(output.toPath());
66                  ArchiveInputStream<?> in = ArchiveStreamFactory.DEFAULT.createArchiveInputStream("jar", is)) {
67              ArchiveEntry entry = in.getNextEntry();
68              while (entry != null) {
69                  final File archiveEntry = newTempFile(entry.getName());
70                  archiveEntry.getParentFile().mkdirs();
71                  if (entry.isDirectory()) {
72                      archiveEntry.mkdir();
73                      entry = in.getNextEntry();
74                      continue;
75                  }
76                  Files.copy(in, archiveEntry.toPath());
77                  entry = in.getNextEntry();
78              }
79          }
80      }
81  
82  }