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.ByteArrayInputStream;
20import java.io.ByteArrayOutputStream;
21import java.io.IOException;
22import java.util.zip.ZipEntry;
23import java.util.zip.ZipInputStream;
24import java.util.zip.ZipOutputStream;
25import junit.framework.TestCase;
26
27/**
28 * Basic tests for ZipStream
29 */
30public class OldAndroidZipStreamTest extends TestCase {
31
32    public void testZipStream() throws Exception {
33        ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
34        createCompressedZip(bytesOut);
35
36        byte[] zipData = bytesOut.toByteArray();
37
38        /*
39        FileOutputStream outFile = new FileOutputStream("/tmp/foo.zip");
40        outFile.write(zipData, 0, zipData.length);
41        outFile.close();
42        */
43
44        /*
45        FileInputStream inFile = new FileInputStream("/tmp/foo.zip");
46        int inputLength = inFile.available();
47        zipData = new byte[inputLength];
48        if (inFile.read(zipData) != inputLength)
49            throw new RuntimeException();
50        inFile.close();
51        */
52
53        ByteArrayInputStream bytesIn = new ByteArrayInputStream(zipData);
54        scanZip(bytesIn);
55
56        bytesOut = new ByteArrayOutputStream();
57        createUncompressedZip(bytesOut);
58
59        zipData = bytesOut.toByteArray();
60
61        bytesIn = new ByteArrayInputStream(zipData);
62        scanZip(bytesIn);
63    }
64
65    /*
66     * stepStep == 0 --> >99% compression
67     * stepStep == 1 --> ~30% compression
68     * stepStep == 2 --> no compression
69     */
70    private static byte[] makeSampleFile(int stepStep) throws IOException {
71        byte[] sample = new byte[128 * 1024];
72        byte val, step;
73        int i, j, offset;
74
75        val = 0;
76        step = 1;
77        offset = 0;
78        for (i = 0; i < (128 * 1024) / 256; i++) {
79            for (j = 0; j < 256; j++) {
80                sample[offset++] = val;
81                val += step;
82            }
83
84            step += stepStep;
85        }
86
87        return sample;
88    }
89
90    private static void createCompressedZip(ByteArrayOutputStream bytesOut) throws IOException {
91        ZipOutputStream out = new ZipOutputStream(bytesOut);
92        try {
93            int i;
94
95            for (i = 0; i < 3; i++) {
96                byte[] input = makeSampleFile(i);
97                ZipEntry newEntry = new ZipEntry("file-" + i);
98
99                if (i != 1)
100                    newEntry.setComment("this is file " + i);
101                out.putNextEntry(newEntry);
102                out.write(input, 0, input.length);
103                out.closeEntry();
104            }
105
106            out.setComment("This is a lovely compressed archive!");
107        } finally {
108            out.close();
109        }
110    }
111
112    private static void createUncompressedZip(ByteArrayOutputStream bytesOut) throws IOException {
113        ZipOutputStream out = new ZipOutputStream(bytesOut);
114        try {
115            long[] crcs = {0x205fbff3, 0x906fae57L, 0x2c235131};
116            int i;
117
118            for (i = 0; i < 3; i++) {
119                byte[] input = makeSampleFile(i);
120                ZipEntry newEntry = new ZipEntry("file-" + i);
121
122                if (i != 1)
123                    newEntry.setComment("this is file " + i);
124                newEntry.setMethod(ZipEntry.STORED);
125                newEntry.setSize(128 * 1024);
126                newEntry.setCrc(crcs[i]);
127                out.putNextEntry(newEntry);
128                out.write(input, 0, input.length);
129                out.closeEntry();
130            }
131
132            out.setComment("This is a lovely, but uncompressed, archive!");
133        } finally {
134            out.close();
135        }
136    }
137
138    private static void scanZip(ByteArrayInputStream bytesIn) throws IOException {
139        ZipInputStream in = new ZipInputStream(bytesIn);
140        try {
141            int i;
142
143            for (i = 0; i < 3; i++) {
144                ZipEntry entry = in.getNextEntry();
145                ByteArrayOutputStream contents = new ByteArrayOutputStream();
146                byte[] buf = new byte[4096];
147                int len, totalLen = 0;
148
149                while ((len = in.read(buf)) > 0) {
150                    contents.write(buf, 0, len);
151                    totalLen += len;
152                }
153
154                assertEquals(128 * 1024, totalLen);
155
156//                System.out.println("OldAndroidZipStreamTest: name='" + entry.getName()
157//                        + "', zero=" + contents.toByteArray()[0]
158//                        + ", tfs=" + contents.toByteArray()[257]
159//                        + ", crc=" + Long.toHexString(entry.getCrc()));
160            }
161
162            assertNull("should only be three entries", in.getNextEntry());
163        } finally {
164            in.close();
165        }
166    }
167}
168
169