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              final FileInputStream is = new FileInputStream(output[1]);
44              try {
45                  final ArchiveInputStream in = new ArchiveStreamFactory()
46                      .createArchiveInputStream("jar", is);
47  
48                  ArchiveEntry entry = in.getNextEntry();
49                  while (entry != null) {
50                      File archiveEntry = new File(dir, entry.getName());
51                      archiveEntry.getParentFile().mkdirs();
52                      if (entry.isDirectory()) {
53                          archiveEntry.mkdir();
54                          entry = in.getNextEntry();
55                          continue;
56                      }
57                      OutputStream out = new FileOutputStream(archiveEntry);
58                      IOUtils.copy(in, out);
59                      out.close();
60                      entry = in.getNextEntry();
61                  }
62  
63                  in.close();
64              } finally {
65                  is.close();
66              }
67          } finally {
68              output[1].delete();
69              output[0].delete();
70          }
71      }
72  
73      @Test
74      public void testNormalizeInPlace() throws Throwable {
75          final File input = getFile("bla.jar");
76          final File[] output = createTempDirAndFile();
77          try {
78              FileInputStream is = new FileInputStream(input);
79              OutputStream os = null;
80              try {
81                  os = new FileOutputStream(output[1]);
82                  IOUtils.copy(is, os);
83              } finally {
84                  is.close();
85                  if (os != null) {
86                      os.close();
87                  }
88              }
89  
90              Pack200Utils.normalize(output[1]);
91              is = new FileInputStream(output[1]);
92              try {
93                  final ArchiveInputStream in = new ArchiveStreamFactory()
94                      .createArchiveInputStream("jar", is);
95  
96                  ArchiveEntry entry = in.getNextEntry();
97                  while (entry != null) {
98                      File archiveEntry = new File(dir, entry.getName());
99                      archiveEntry.getParentFile().mkdirs();
100                     if (entry.isDirectory()) {
101                         archiveEntry.mkdir();
102                         entry = in.getNextEntry();
103                         continue;
104                     }
105                     OutputStream out = new FileOutputStream(archiveEntry);
106                     IOUtils.copy(in, out);
107                     out.close();
108                     entry = in.getNextEntry();
109                 }
110 
111                 in.close();
112             } finally {
113                 is.close();
114             }
115         } finally {
116             output[1].delete();
117             output[0].delete();
118         }
119     }
120 
121 }