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.FileInputStream;
23  import java.io.FileOutputStream;
24  import java.io.OutputStream;
25  import java.util.HashMap;
26  
27  import org.apache.commons.compress.AbstractTestCase;
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.apache.commons.compress.utils.IOUtils;
32  import org.junit.Test;
33  
34  public final class Pack200UtilsTest extends AbstractTestCase {
35  
36      @Test
37      public void testNormalize() throws Throwable {
38          final File input = getFile("bla.jar");
39          final File[] output = createTempDirAndFile();
40          try {
41              Pack200Utils.normalize(input, output[1],
42                                     new HashMap<String, String>());
43              try (FileInputStream is = new FileInputStream(output[1])) {
44                  final ArchiveInputStream in = new ArchiveStreamFactory()
45                          .createArchiveInputStream("jar", is);
46  
47                  ArchiveEntry entry = in.getNextEntry();
48                  while (entry != null) {
49                      final File archiveEntry = new File(dir, entry.getName());
50                      archiveEntry.getParentFile().mkdirs();
51                      if (entry.isDirectory()) {
52                          archiveEntry.mkdir();
53                          entry = in.getNextEntry();
54                          continue;
55                      }
56                      final OutputStream out = new FileOutputStream(archiveEntry);
57                      IOUtils.copy(in, out);
58                      out.close();
59                      entry = in.getNextEntry();
60                  }
61  
62                  in.close();
63              }
64          } finally {
65              output[1].delete();
66              output[0].delete();
67          }
68      }
69  
70      @Test
71      public void testNormalizeInPlace() throws Throwable {
72          final File input = getFile("bla.jar");
73          final File[] output = createTempDirAndFile();
74          try {
75              FileInputStream is = new FileInputStream(input);
76              OutputStream os = null;
77              try {
78                  os = new FileOutputStream(output[1]);
79                  IOUtils.copy(is, os);
80              } finally {
81                  is.close();
82                  if (os != null) {
83                      os.close();
84                  }
85              }
86  
87              Pack200Utils.normalize(output[1]);
88              is = new FileInputStream(output[1]);
89              try {
90                  final ArchiveInputStream in = new ArchiveStreamFactory()
91                      .createArchiveInputStream("jar", is);
92  
93                  ArchiveEntry entry = in.getNextEntry();
94                  while (entry != null) {
95                      final File archiveEntry = new File(dir, entry.getName());
96                      archiveEntry.getParentFile().mkdirs();
97                      if (entry.isDirectory()) {
98                          archiveEntry.mkdir();
99                          entry = in.getNextEntry();
100                         continue;
101                     }
102                     final OutputStream out = new FileOutputStream(archiveEntry);
103                     IOUtils.copy(in, out);
104                     out.close();
105                     entry = in.getNextEntry();
106                 }
107 
108                 in.close();
109             } finally {
110                 is.close();
111             }
112         } finally {
113             output[1].delete();
114             output[0].delete();
115         }
116     }
117 
118 }