1 /* 2 * Copyright (C) 2015 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 17 package libcore.java.util.zip; 18 19 import libcore.io.Streams; 20 21 import java.io.BufferedOutputStream; 22 import java.io.File; 23 import java.io.FileOutputStream; 24 import java.io.IOException; 25 import java.io.InputStream; 26 import java.io.OutputStream; 27 import java.util.Enumeration; 28 import java.util.zip.ZipEntry; 29 import java.util.zip.ZipFile; 30 import java.util.zip.ZipOutputStream; 31 32 public final class Zip64FileTest extends AbstractZipFileTest { 33 @Override createZipOutputStream(OutputStream wrapped)34 protected ZipOutputStream createZipOutputStream(OutputStream wrapped) { 35 return new ZipOutputStream(wrapped, true /* forceZip64 */); 36 } 37 testZip64Support_largeNumberOfEntries()38 public void testZip64Support_largeNumberOfEntries() throws IOException { 39 final File file = createZipFile(65550, 2, false /* setEntrySize */); 40 ZipFile zf = null; 41 try { 42 zf = new ZipFile(file); 43 assertEquals(65550, zf.size()); 44 45 Enumeration<? extends ZipEntry> entries = zf.entries(); 46 assertTrue(entries.hasMoreElements()); 47 ZipEntry ze = entries.nextElement(); 48 assertEquals(2, ze.getSize()); 49 InputStream is = null; 50 try { 51 is = zf.getInputStream(ze); 52 byte[] uncompressed = Streams.readFully(is); 53 assertEquals(2, uncompressed.length); 54 } finally { 55 if (is != null) { 56 is.close(); 57 } 58 } 59 } finally { 60 if (zf != null) { 61 zf.close(); 62 } 63 } 64 } 65 testZip64Support_totalLargerThan4G()66 public void testZip64Support_totalLargerThan4G() throws IOException { 67 final File file = createZipFile(5, 1073741824L, false /* setEntrySize */); 68 ZipFile zf = null; 69 try { 70 zf = new ZipFile(file); 71 assertEquals(5, zf.size()); 72 Enumeration<? extends ZipEntry> entries = zf.entries(); 73 assertTrue(entries.hasMoreElements()); 74 ZipEntry ze = entries.nextElement(); 75 assertEquals(1073741824L, ze.getSize()); 76 } finally { 77 if (zf != null) { 78 zf.close(); 79 } 80 } 81 } 82 testZip64Support_hugeEntry()83 public void testZip64Support_hugeEntry() throws IOException { 84 try { 85 createZipFile(1, 4294967410L, false /* setEntrySize */); 86 fail(); 87 } catch (IOException expected) { 88 } 89 90 final File file = createZipFile(1, 4294967410L, true /* setEntrySize */); 91 ZipFile zf = null; 92 try { 93 zf = new ZipFile(file); 94 assertEquals(1, zf.size()); 95 Enumeration<? extends ZipEntry> entries = zf.entries(); 96 assertTrue(entries.hasMoreElements()); 97 ZipEntry ze = entries.nextElement(); 98 assertEquals(4294967410L, ze.getSize()); 99 } finally { 100 if (zf != null) { 101 zf.close(); 102 } 103 } 104 } 105 createZipFile(int numEntries, long entrySize, boolean setEntrySize)106 private File createZipFile(int numEntries, long entrySize, boolean setEntrySize) 107 throws IOException { 108 File file = createTemporaryZipFile(); 109 // Don't force a 64 bit zip file to test that our heuristics work. 110 ZipOutputStream os = new ZipOutputStream( 111 new BufferedOutputStream(new FileOutputStream(file))); 112 writeEntries(os, numEntries, entrySize, setEntrySize); 113 return file; 114 } 115 } 116