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