1/* 2 * Copyright (C) 2008 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package libcore.java.util.zip; 18 19import java.io.File; 20import java.io.FileOutputStream; 21import java.io.IOException; 22import java.io.InputStream; 23import java.io.OutputStream; 24import java.util.Enumeration; 25import java.util.zip.ZipEntry; 26import java.util.zip.ZipFile; 27import java.util.zip.ZipOutputStream; 28import junit.framework.TestCase; 29 30 31/** 32 * Basic tests for ZipFile. 33 */ 34public class OldAndroidZipFileTest extends TestCase { 35 private static final int SAMPLE_SIZE = 128 * 1024; 36 37 public void testZipFile() throws Exception { 38 39 File file = File.createTempFile("ZipFileTest", ".zip"); 40 try { 41 // create a test file; assume it's not going to collide w/anything 42 FileOutputStream outStream = new FileOutputStream(file); 43 createCompressedZip(outStream); 44 45 scanZip(file.getPath()); 46 read2(file.getPath()); 47 } finally { 48 file.delete(); 49 } 50 } 51 52 /* 53 * stepStep == 0 --> >99% compression 54 * stepStep == 1 --> ~30% compression 55 * stepStep == 2 --> no compression 56 */ 57 static byte[] makeSampleFile(int stepStep) throws IOException { 58 byte[] sample = new byte[SAMPLE_SIZE]; 59 byte val, step; 60 int i, j, offset; 61 62 val = 0; 63 step = 1; 64 offset = 0; 65 for (i = 0; i < SAMPLE_SIZE / 256; i++) { 66 for (j = 0; j < 256; j++) { 67 sample[offset++] = val; 68 val += step; 69 } 70 71 step += stepStep; 72 } 73 74 return sample; 75 } 76 77 static void createCompressedZip(OutputStream bytesOut) throws IOException { 78 ZipOutputStream out = new ZipOutputStream(bytesOut); 79 try { 80 int i; 81 82 for (i = 0; i < 3; i++) { 83 byte[] input = makeSampleFile(i); 84 ZipEntry newEntry = new ZipEntry("file-" + i); 85 86 if (i != 1) { 87 newEntry.setComment("this is file " + i); 88 } 89 out.putNextEntry(newEntry); 90 out.write(input, 0, input.length); 91 out.closeEntry(); 92 } 93 94 out.setComment("This is a lovely compressed archive!"); 95 } finally { 96 out.close(); 97 } 98 } 99 100 static void scanZip(String fileName) throws IOException { 101 ZipFile zipFile = new ZipFile(fileName); 102 Enumeration fileList; 103 int idx = 0; 104 105// System.out.println("Contents of " + zipFile + ":"); 106 for (fileList = zipFile.entries(); fileList.hasMoreElements();) { 107 ZipEntry entry = (ZipEntry) fileList.nextElement(); 108// System.out.println(" " + entry.getName()); 109 assertEquals(entry.getName(), "file-" + idx); 110 idx++; 111 } 112 113 zipFile.close(); 114 } 115 116 /* 117 * Read compressed data from two different entries at the same time, 118 * to verify that the streams aren't getting confused. If we do 119 * something wrong, the inflater will choke and throw a ZipException. 120 * 121 * This doesn't test synchronization in multi-threaded use. 122 */ 123 static void read2(String fileName) throws IOException { 124 ZipFile zipFile; 125 ZipEntry entry1, entry2; 126 byte buf[] = new byte[16384]; 127 InputStream stream1, stream2; 128 int len, totalLen1, totalLen2; 129 130 /* use file-1 and file-2 because the compressed data is large */ 131 zipFile = new ZipFile(fileName); 132 entry1 = zipFile.getEntry("file-1"); 133 entry2 = zipFile.getEntry("file-2"); 134 135 /* make sure we got the right thing */ 136 assertEquals("file-1", entry1.getName()); 137 assertEquals("file-2", entry2.getName()); 138 139 /* create streams */ 140 stream1 = zipFile.getInputStream(entry1); 141 stream2 = zipFile.getInputStream(entry2); 142 143 /* 144 * Read a piece of file #1. 145 */ 146 totalLen1 = stream1.read(buf); 147 assertTrue("initial read failed on #1", totalLen1 >= 0); 148 149 /* 150 * Read a piece of file #2. 151 */ 152 totalLen2 = stream2.read(buf); 153 assertTrue("initial read failed on #2", totalLen2 >= 0); 154 155 /* 156 * Read the rest of file #1, and close the stream. 157 * 158 * If our streams are crossed up, we'll fail here. 159 */ 160 while ((len = stream1.read(buf)) > 0) { 161 totalLen1 += len; 162 } 163 assertEquals(SAMPLE_SIZE, totalLen1); 164 stream1.close(); 165 166 /* 167 * Read the rest of file #2, and close the stream. 168 */ 169 while ((len = stream2.read(buf)) > 0) { 170 totalLen2 += len; 171 } 172 assertEquals(SAMPLE_SIZE, totalLen2); 173 stream2.close(); 174 175 /* 176 * Open a new one. 177 */ 178 stream1 = zipFile.getInputStream(zipFile.getEntry("file-0")); 179 180 /* 181 * Close the ZipFile. According to the RI, none if its InputStreams can 182 * be read after this point. 183 */ 184 zipFile.close(); 185 186 Exception error = null; 187 try { 188 stream1.read(buf); 189 } catch (Exception ex) { 190 error = ex; 191 } 192 193 assertNotNull("ZipFile shouldn't allow reading of closed files.", error); 194 } 195} 196 197