View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one or more
3    *  contributor license agreements.  See the NOTICE file distributed with
4    *  this work for additional information regarding copyright ownership.
5    *  The ASF licenses this file to You under the Apache License, Version 2.0
6    *  (the "License"); you may not use this file except in compliance with
7    *  the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
16   *
17   */
18  
19  package org.apache.commons.compress.archivers.zip;
20  
21  import static org.apache.commons.compress.AbstractTestCase.getFile;
22  import static org.junit.Assert.*;
23  
24  import java.io.File;
25  import java.io.FileInputStream;
26  import java.io.IOException;
27  
28  import org.apache.commons.compress.archivers.ArchiveEntry;
29  import org.junit.Test;
30  
31  /**
32   * JUnit testcase for a multi-volume zip file.
33   *
34   * Some tools (like 7-zip) allow users to split a large archives into 'volumes'
35   * with a given size to fit them into multiple cds, usb drives, or emails with
36   * an attachment size limit. It's basically the same file split into chunks of
37   * exactly 65536 bytes length. Concatenating volumes yields exactly the original
38   * file. There is no mechanism in the ZIP algorithm to accommodate for this.
39   * Before commons-compress used to enter an infinite loop on the last entry for
40   * such a file. This test is intended to prove that this error doesn't occur
41   * anymore. All entries but the last one are returned correctly, the last entry
42   * yields an exception.
43   *
44   */
45  public class Maven221MultiVolumeTest {
46  
47      private static final String [] ENTRIES = new String [] {
48          "apache-maven-2.2.1/",
49          "apache-maven-2.2.1/LICENSE.txt",
50          "apache-maven-2.2.1/NOTICE.txt",
51          "apache-maven-2.2.1/README.txt",
52          "apache-maven-2.2.1/bin/",
53          "apache-maven-2.2.1/bin/m2.conf",
54          "apache-maven-2.2.1/bin/mvn",
55          "apache-maven-2.2.1/bin/mvn.bat",
56          "apache-maven-2.2.1/bin/mvnDebug",
57          "apache-maven-2.2.1/bin/mvnDebug.bat",
58          "apache-maven-2.2.1/boot/",
59          "apache-maven-2.2.1/boot/classworlds-1.1.jar",
60          "apache-maven-2.2.1/conf/",
61          "apache-maven-2.2.1/conf/settings.xml",
62          "apache-maven-2.2.1/lib/"
63      };
64  
65      private static final String LAST_ENTRY_NAME =
66          "apache-maven-2.2.1/lib/maven-2.2.1-uber.jar";
67  
68      @Test
69      public void testRead7ZipMultiVolumeArchiveForStream() throws IOException {
70  
71          FileInputStream archive =
72              new FileInputStream(getFile("apache-maven-2.2.1.zip.001"));
73          ZipArchiveInputStream zi = null;
74          try {
75              zi = new ZipArchiveInputStream(archive,null,false);
76  
77              // these are the entries that are supposed to be processed
78              // correctly without any problems
79              for (String element : ENTRIES) {
80                  assertEquals(element, zi.getNextEntry().getName());
81              }
82  
83              // this is the last entry that is truncated
84              ArchiveEntry lastEntry = zi.getNextEntry();
85              assertEquals(LAST_ENTRY_NAME, lastEntry.getName());
86              byte [] buffer = new byte [4096];
87  
88              // before the fix, we'd get 0 bytes on this read and all
89              // subsequent reads thus a client application might enter
90              // an infinite loop after the fix, we should get an
91              // exception
92              try {
93                  while (zi.read(buffer) > 0) { }
94                  fail("shouldn't be able to read from truncated entry");
95              } catch (IOException e) {
96                  assertEquals("Truncated ZIP file", e.getMessage());
97              }
98  
99              // and now we get another entry, which should also yield
100             // an exception
101             try {
102                 zi.getNextEntry();
103                 fail("shouldn't be able to read another entry from truncated"
104                      + " file");
105             } catch (IOException e) {
106                 // this is to be expected
107             }
108         } finally {
109             if (zi != null) {
110                 zi.close();
111             }
112         }
113     }
114 
115     @Test
116     public void testRead7ZipMultiVolumeArchiveForFile() throws IOException {
117         File file = getFile("apache-maven-2.2.1.zip.001");
118         try {
119             new ZipFile(file);
120             fail("Expected ZipFile to fail");
121         } catch (IOException ex) {
122             // expected
123         }
124     }
125 }