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
23 import java.io.File;
24 import java.io.FileInputStream;
25 import java.io.IOException;
26
27 import junit.framework.TestCase;
28
29 import org.apache.commons.compress.archivers.ArchiveEntry;
30
31 /**
32 * JUnit 3 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 extends TestCase {
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 public void testRead7ZipMultiVolumeArchiveForStream() throws IOException {
69
70 FileInputStream archive =
71 new FileInputStream(getFile("apache-maven-2.2.1.zip.001"));
72 ZipArchiveInputStream zi = null;
73 try {
74 zi = new ZipArchiveInputStream(archive,null,false);
75
76 // these are the entries that are supposed to be processed
77 // correctly without any problems
78 for (String element : ENTRIES) {
79 assertEquals(element, zi.getNextEntry().getName());
80 }
81
82 // this is the last entry that is truncated
83 ArchiveEntry lastEntry = zi.getNextEntry();
84 assertEquals(LAST_ENTRY_NAME, lastEntry.getName());
85 byte [] buffer = new byte [4096];
86
87 // before the fix, we'd get 0 bytes on this read and all
88 // subsequent reads thus a client application might enter
89 // an infinite loop after the fix, we should get an
90 // exception
91 try {
92 while (zi.read(buffer) > 0) { }
93 fail("shouldn't be able to read from truncated entry");
94 } catch (IOException e) {
95 assertEquals("Truncated ZIP file", e.getMessage());
96 }
97
98 // and now we get another entry, which should also yield
99 // an exception
100 try {
101 zi.getNextEntry();
102 fail("shouldn't be able to read another entry from truncated"
103 + " file");
104 } catch (IOException e) {
105 // this is to be expected
106 }
107 } finally {
108 if (zi != null) {
109 zi.close();
110 }
111 }
112 }
113
114 public void testRead7ZipMultiVolumeArchiveForFile() throws IOException {
115 File file = getFile("apache-maven-2.2.1.zip.001");
116 try {
117 new ZipFile(file);
118 fail("Expected ZipFile to fail");
119 } catch (IOException ex) {
120 // expected
121 }
122 }
123 }