summaryrefslogtreecommitdiff
path: root/src/test/java/com/android/apkzlib/zip/ZFileTest.java
blob: bce02866347c81a4a935b25e395fb31234cb79b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.apkzlib.zip;

import static com.android.apkzlib.utils.ApkZFileTestUtils.readSegment;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import com.android.apkzlib.zip.compress.DeflateExecutionCompressor;
import com.android.apkzlib.zip.utils.CloseableByteSource;
import com.android.apkzlib.zip.utils.RandomAccessFileUtils;
import com.google.common.base.Charsets;
import com.google.common.base.Strings;
import com.google.common.hash.Hashing;
import com.google.common.io.ByteStreams;
import com.google.common.io.Closer;
import com.google.common.io.Files;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.util.Locale;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.zip.Deflater;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
import javax.annotation.Nonnull;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

public class ZFileTest {
    @Rule
    public TemporaryFolder mTemporaryFolder = new TemporaryFolder();

    @Test
    public void getZipPath() throws Exception {
        File temporaryDir = mTemporaryFolder.getRoot();
        File zpath = new File(temporaryDir, "a");
        try (ZFile zf = new ZFile(zpath)) {
            assertEquals(zpath, zf.getFile());
        }
    }

    @Test
    public void readNonExistingFile() throws Exception {
        File temporaryDir = mTemporaryFolder.getRoot();
        File zf = new File(temporaryDir, "a");
        try (ZFile azf = new ZFile(zf)) {
            azf.touch();
        }

        assertTrue(zf.exists());
    }

    @Test(expected = IOException.class)
    public void readExistingEmptyFile() throws Exception {
        File temporaryDir = mTemporaryFolder.getRoot();
        File zf = new File(temporaryDir, "a");
        Files.write(new byte[0], zf);
        try (ZFile azf = new ZFile(zf)) {
            /*
             * Just open and close.
             */
        }
    }

    @Test
    public void readAlmostEmptyZip() throws Exception {
        File zf = ZipTestUtils.cloneRsrc("empty-zip.zip", mTemporaryFolder);

        try (ZFile azf = new ZFile(zf)) {
            assertEquals(1, azf.entries().size());

            StoredEntry z = azf.get("z/");
            assertNotNull(z);
            assertSame(StoredEntryType.DIRECTORY, z.getType());
        }
    }

    @Test
    public void readZipWithTwoFilesOneDirectory() throws Exception {
        File zf = ZipTestUtils.cloneRsrc("simple-zip.zip", mTemporaryFolder);

        try (ZFile azf = new ZFile(zf)) {
            assertEquals(3, azf.entries().size());

            StoredEntry e0 = azf.get("dir/");
            assertNotNull(e0);
            assertSame(StoredEntryType.DIRECTORY, e0.getType());

            StoredEntry e1 = azf.get("dir/inside");
            assertNotNull(e1);
            assertSame(StoredEntryType.FILE, e1.getType());
            ByteArrayOutputStream e1BytesOut = new ByteArrayOutputStream();
            ByteStreams.copy(e1.open(), e1BytesOut);
            byte e1Bytes[] = e1BytesOut.toByteArray();
            String e1Txt = new String(e1Bytes, Charsets.US_ASCII);
            assertEquals("inside", e1Txt);

            StoredEntry e2 = azf.get("file.txt");
            assertNotNull(e2);
            assertSame(StoredEntryType.FILE, e2.getType());
            ByteArrayOutputStream e2BytesOut = new ByteArrayOutputStream();
            ByteStreams.copy(e2.open(), e2BytesOut);
            byte e2Bytes[] = e2BytesOut.toByteArray();
            String e2Txt = new String(e2Bytes, Charsets.US_ASCII);
            assertEquals("file with more text to allow deflating to be useful", e2Txt);
        }
    }

    @Test
    public void readOnlyZipSupport() throws Exception {
        File testZip = ZipTestUtils.cloneRsrc("empty-zip.zip", mTemporaryFolder);

        assertTrue(testZip.setWritable(false));

        try (ZFile zf = new ZFile(testZip)) {
            assertEquals(1, zf.entries().size());
            assertTrue(zf.getCentralDirectoryOffset() > 0);
            assertTrue(zf.getEocdOffset() > 0);
        }
    }

    @Test
    public void readOnlyV2SignedApkSupport() throws Exception {
        File testZip = ZipTestUtils.cloneRsrc("v2-signed.apk", mTemporaryFolder);

        assertTrue(testZip.setWritable(false));

        try (ZFile zf = new ZFile(testZip)) {
            assertEquals(416, zf.entries().size());
            assertTrue(zf.getCentralDirectoryOffset() > 0);
            assertTrue(zf.getEocdOffset() > 0);
        }
    }

    @Test
    public void compressedFilesReadableByJavaZip() throws Exception {
        File testZip = new File(mTemporaryFolder.getRoot(), "t.zip");

        File wiki = ZipTestUtils
                .cloneRsrc("text-files/wikipedia.html", mTemporaryFolder, "wiki");
        File rfc = ZipTestUtils.cloneRsrc("text-files/rfc2460.txt", mTemporaryFolder, "rfc");
        File lena = ZipTestUtils.cloneRsrc("images/lena.png", mTemporaryFolder, "lena");
        byte[] wikiData = Files.toByteArray(wiki);
        byte[] rfcData = Files.toByteArray(rfc);
        byte[] lenaData = Files.toByteArray(lena);

        try (ZFile zf = new ZFile(testZip)) {
            zf.add("wiki", new ByteArrayInputStream(wikiData));
            zf.add("rfc", new ByteArrayInputStream(rfcData));
            zf.add("lena", new ByteArrayInputStream(lenaData));
        }

        try(ZipFile jz = new ZipFile(testZip)) {
            ZipEntry ze = jz.getEntry("wiki");
            assertNotNull(ze);
            assertEquals(ZipEntry.DEFLATED, ze.getMethod());
            assertTrue(ze.getCompressedSize() < wikiData.length);
            InputStream zeis = jz.getInputStream(ze);
            assertArrayEquals(wikiData, ByteStreams.toByteArray(zeis));
            zeis.close();

            ze = jz.getEntry("rfc");
            assertNotNull(ze);
            assertEquals(ZipEntry.DEFLATED, ze.getMethod());
            assertTrue(ze.getCompressedSize() < rfcData.length);
            zeis = jz.getInputStream(ze);
            assertArrayEquals(rfcData, ByteStreams.toByteArray(zeis));
            zeis.close();

            ze = jz.getEntry("lena");
            assertNotNull(ze);
            assertEquals(ZipEntry.STORED, ze.getMethod());
            assertTrue(ze.getCompressedSize() == lenaData.length);
            zeis = jz.getInputStream(ze);
            assertArrayEquals(lenaData, ByteStreams.toByteArray(zeis));
            zeis.close();
        }
    }

    @Test
    public void removeFileFromZip() throws Exception {
        File zipFile = mTemporaryFolder.newFile("test.zip");

        try(ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFile))) {
            ZipEntry entry = new ZipEntry("foo/");
            entry.setMethod(ZipEntry.STORED);
            entry.setSize(0);
            entry.setCompressedSize(0);
            entry.setCrc(0);
            zos.putNextEntry(entry);
            zos.putNextEntry(new ZipEntry("foo/bar"));
            zos.write(new byte[] { 1, 2, 3, 4 });
            zos.closeEntry();
        }

        try (ZFile zf = new ZFile(zipFile)) {
            assertEquals(2, zf.entries().size());
            for (StoredEntry e : zf.entries()) {
                if (e.getType() == StoredEntryType.FILE) {
                    e.delete();
                }
            }

            zf.update();

            try (ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile))) {
                ZipEntry e1 = zis.getNextEntry();
                assertNotNull(e1);

                assertEquals("foo/", e1.getName());

                ZipEntry e2 = zis.getNextEntry();
                assertNull(e2);
            }
        }
    }

    @Test
    public void addFileToZip() throws Exception {
        File zipFile = mTemporaryFolder.newFile("test.zip");

        try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFile))) {
            ZipEntry fooDir = new ZipEntry("foo/");
            fooDir.setCrc(0);
            fooDir.setCompressedSize(0);
            fooDir.setSize(0);
            fooDir.setMethod(ZipEntry.STORED);
            zos.putNextEntry(fooDir);
            zos.closeEntry();
        }

        ZFile zf = new ZFile(zipFile);
        assertEquals(1, zf.entries().size());

        zf.update();

        try (ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile))) {
            ZipEntry e1 = zis.getNextEntry();
            assertNotNull(e1);

            assertEquals("foo/", e1.getName());

            ZipEntry e2 = zis.getNextEntry();
            assertNull(e2);
        }
    }

    @Test
    public void createNewZip() throws Exception {
        File zipFile = new File(mTemporaryFolder.getRoot(), "test.zip");

        ZFile zf = new ZFile(zipFile);
        zf.add("foo", new ByteArrayInputStream(new byte[] { 0, 1 }));
        zf.close();

        try (ZipFile jzf = new ZipFile(zipFile)) {
            assertEquals(1, jzf.size());

            ZipEntry fooEntry = jzf.getEntry("foo");
            assertNotNull(fooEntry);
            assertEquals("foo", fooEntry.getName());
            assertEquals(2, fooEntry.getSize());

            InputStream is = jzf.getInputStream(fooEntry);
            assertEquals(0, is.read());
            assertEquals(1, is.read());
            assertEquals(-1, is.read());

            is.close();
        }
    }

    @Test
    public void replaceFileWithSmallerInMiddle() throws Exception {
        File zipFile = new File(mTemporaryFolder.getRoot(), "test.zip");

        try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFile))) {
            zos.putNextEntry(new ZipEntry("file1"));
            zos.write(new byte[] { 1, 2, 3, 4, 5 });
            zos.putNextEntry(new ZipEntry("file2"));
            zos.write(new byte[] { 6, 7, 8 });
            zos.putNextEntry(new ZipEntry("file3"));
            zos.write(new byte[] { 9, 0, 1, 2, 3, 4 });
        }

        int totalSize = (int) zipFile.length();

        try (ZFile zf = new ZFile(zipFile)) {
            assertEquals(3, zf.entries().size());

            StoredEntry file2 = zf.get("file2");
            assertNotNull(file2);
            assertEquals(3, file2.getCentralDirectoryHeader().getUncompressedSize());

            assertArrayEquals(new byte[] { 6, 7, 8 }, file2.read());

            zf.add("file2", new ByteArrayInputStream(new byte[] { 11, 12 }));

            int newTotalSize = (int) zipFile.length();
            assertTrue(newTotalSize + " == " + totalSize, newTotalSize == totalSize);

            file2 = zf.get("file2");
            assertNotNull(file2);
            assertArrayEquals(new byte[] { 11, 12 }, file2.read());
        }

        try (ZFile zf2 = new ZFile(zipFile)) {
            StoredEntry file2 = zf2.get("file2");
            assertNotNull(file2);
            assertArrayEquals(new byte[] { 11, 12 }, file2.read());
        }
    }

    @Test
    public void replaceFileWithSmallerAtEnd() throws Exception {
        File zipFile = new File(mTemporaryFolder.getRoot(), "test.zip");
        try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFile))) {
            zos.putNextEntry(new ZipEntry("file1"));
            zos.write(new byte[]{1, 2, 3, 4, 5});
            zos.putNextEntry(new ZipEntry("file2"));
            zos.write(new byte[]{6, 7, 8});
            zos.putNextEntry(new ZipEntry("file3"));
            zos.write(new byte[]{9, 0, 1, 2, 3, 4});
        }

        int totalSize = (int) zipFile.length();

        try (ZFile zf = new ZFile(zipFile)) {
            assertEquals(3, zf.entries().size());

            StoredEntry file3 = zf.get("file3");
            assertNotNull(file3);
            assertEquals(6, file3.getCentralDirectoryHeader().getUncompressedSize());

            assertArrayEquals(new byte[]{9, 0, 1, 2, 3, 4}, file3.read());

            zf.add("file3", new ByteArrayInputStream(new byte[]{11, 12}));
            zf.close();

            int newTotalSize = (int) zipFile.length();
            assertTrue(newTotalSize + " < " + totalSize, newTotalSize < totalSize);

            file3 = zf.get("file3");
            assertNotNull(file3);
            assertArrayEquals(new byte[]{11, 12,}, file3.read());
        }

        try (ZFile zf2 = new ZFile(zipFile)) {
            StoredEntry file3 = zf2.get("file3");
            assertNotNull(file3);
            assertArrayEquals(new byte[]{11, 12,}, file3.read());
        }
    }

    @Test
    public void replaceFileWithBiggerAtBegin() throws Exception {
        File zipFile = new File(mTemporaryFolder.getRoot(), "test.zip");

        try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFile))) {
            zos.putNextEntry(new ZipEntry("file1"));
            zos.write(new byte[]{1, 2, 3, 4, 5});
            zos.putNextEntry(new ZipEntry("file2"));
            zos.write(new byte[]{6, 7, 8});
            zos.putNextEntry(new ZipEntry("file3"));
            zos.write(new byte[]{9, 0, 1, 2, 3, 4});
        }

        int totalSize = (int) zipFile.length();
        byte[] newData = new byte[100];

        try (ZFile zf = new ZFile(zipFile)) {
            assertEquals(3, zf.entries().size());

            StoredEntry file1 = zf.get("file1");
            assertNotNull(file1);
            assertEquals(5, file1.getCentralDirectoryHeader().getUncompressedSize());

            assertArrayEquals(new byte[]{1, 2, 3, 4, 5}, file1.read());

            /*
             * Need some data because java zip API uses data descriptors which we don't and makes
             * the entries bigger (meaning just adding a couple of bytes would still fit in the
             * same place).
             */
            Random r = new Random();
            r.nextBytes(newData);

            zf.add("file1", new ByteArrayInputStream(newData));
            zf.close();

            int newTotalSize = (int) zipFile.length();
            assertTrue(newTotalSize + " > " + totalSize, newTotalSize > totalSize);

            file1 = zf.get("file1");
            assertNotNull(file1);
            assertArrayEquals(newData, file1.read());
        }

        try (ZFile zf2 = new ZFile(zipFile)) {
            StoredEntry file1 = zf2.get("file1");
            assertNotNull(file1);
            assertArrayEquals(newData, file1.read());
        }
    }

    @Test
    public void replaceFileWithBiggerAtEnd() throws Exception {
        File zipFile = new File(mTemporaryFolder.getRoot(), "test.zip");

        try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFile))) {
            zos.putNextEntry(new ZipEntry("file1"));
            zos.write(new byte[]{1, 2, 3, 4, 5});
            zos.putNextEntry(new ZipEntry("file2"));
            zos.write(new byte[]{6, 7, 8});
            zos.putNextEntry(new ZipEntry("file3"));
            zos.write(new byte[]{9, 0, 1, 2, 3, 4});
        }

        int totalSize = (int) zipFile.length();
        byte[] newData = new byte[100];

        try (ZFile zf = new ZFile(zipFile)) {
            assertEquals(3, zf.entries().size());

            StoredEntry file3 = zf.get("file3");
            assertNotNull(file3);
            assertEquals(6, file3.getCentralDirectoryHeader().getUncompressedSize());

            assertArrayEquals(new byte[]{9, 0, 1, 2, 3, 4}, file3.read());

            /*
             * Need some data because java zip API uses data descriptors which we don't and makes
             * the entries bigger (meaning just adding a couple of bytes would still fit in the
             * same place).
             */
            Random r = new Random();
            r.nextBytes(newData);

            zf.add("file3", new ByteArrayInputStream(newData));
            zf.close();

            int newTotalSize = (int) zipFile.length();
            assertTrue(newTotalSize + " > " + totalSize, newTotalSize > totalSize);

            file3 = zf.get("file3");
            assertNotNull(file3);
            assertArrayEquals(newData, file3.read());
        }

        try (ZFile zf2 = new ZFile(zipFile)) {
            StoredEntry file3 = zf2.get("file3");
            assertNotNull(file3);
            assertArrayEquals(newData, file3.read());
        }
    }

    @Test
    public void ignoredFilesDuringMerge() throws Exception {
        File zip1 = mTemporaryFolder.newFile("t1.zip");

        try (ZipOutputStream zos1 = new ZipOutputStream(new FileOutputStream(zip1))) {
            zos1.putNextEntry(new ZipEntry("only_in_1"));
            zos1.write(new byte[] { 1, 2 });
            zos1.putNextEntry(new ZipEntry("overridden_by_2"));
            zos1.write(new byte[] { 2, 3 });
            zos1.putNextEntry(new ZipEntry("not_overridden_by_2"));
            zos1.write(new byte[] { 3, 4 });
        }

        File zip2 = mTemporaryFolder.newFile("t2.zip");
        try (ZipOutputStream zos2 = new ZipOutputStream(new FileOutputStream(zip2))) {
            zos2.putNextEntry(new ZipEntry("only_in_2"));
            zos2.write(new byte[] { 4, 5 });
            zos2.putNextEntry(new ZipEntry("overridden_by_2"));
            zos2.write(new byte[] { 5, 6 });
            zos2.putNextEntry(new ZipEntry("not_overridden_by_2"));
            zos2.write(new byte[] { 6, 7 });
            zos2.putNextEntry(new ZipEntry("ignored_in_2"));
            zos2.write(new byte[] { 7, 8 });
        }

        try (
                ZFile zf1 = new ZFile(zip1);
                ZFile zf2 = new ZFile(zip2)) {
            zf1.mergeFrom(zf2, (input) -> input.matches("not.*") || input.matches(".*gnored.*"));

            StoredEntry only_in_1 = zf1.get("only_in_1");
            assertNotNull(only_in_1);
            assertArrayEquals(new byte[]{1, 2}, only_in_1.read());

            StoredEntry overridden_by_2 = zf1.get("overridden_by_2");
            assertNotNull(overridden_by_2);
            assertArrayEquals(new byte[]{5, 6}, overridden_by_2.read());

            StoredEntry not_overridden_by_2 = zf1.get("not_overridden_by_2");
            assertNotNull(not_overridden_by_2);
            assertArrayEquals(new byte[]{3, 4}, not_overridden_by_2.read());

            StoredEntry only_in_2 = zf1.get("only_in_2");
            assertNotNull(only_in_2);
            assertArrayEquals(new byte[]{4, 5}, only_in_2.read());

            StoredEntry ignored_in_2 = zf1.get("ignored_in_2");
            assertNull(ignored_in_2);
        }
    }

    @Test
    public void addingFileDoesNotAddDirectoriesAutomatically() throws Exception {
        File zip = new File(mTemporaryFolder.getRoot(), "z.zip");
        try (ZFile zf = new ZFile(zip)) {
            zf.add("a/b/c", new ByteArrayInputStream(new byte[]{1, 2, 3}));
            zf.update();
            assertEquals(1, zf.entries().size());

            StoredEntry c = zf.get("a/b/c");
            assertNotNull(c);
            assertEquals(3, c.read().length);
        }
    }

    @Test
    public void zipFileWithEocdSignatureInComment() throws Exception {
        File zip = mTemporaryFolder.newFile("f.zip");
        try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zip))) {
            zos.putNextEntry(new ZipEntry("a"));
            zos.write(new byte[] { 1, 2, 3 });
            zos.setComment("Random comment with XXXX weird characters. There must be enough "
                    + "characters to survive skipping back the EOCD size.");
        }

        byte zipBytes[] = Files.toByteArray(zip);
        boolean didX4 = false;
        for (int i = 0; i < zipBytes.length - 3; i++) {
            boolean x4 = true;
            for (int j = 0; j < 4; j++) {
                if (zipBytes[i + j] != 'X') {
                    x4 = false;
                    break;
                }
            }

            if (x4) {
                zipBytes[i] = (byte) 0x50;
                zipBytes[i + 1] = (byte) 0x4b;
                zipBytes[i + 2] = (byte) 0x05;
                zipBytes[i + 3] = (byte) 0x06;
                didX4 = true;
                break;
            }
        }

        assertTrue(didX4);

        Files.write(zipBytes, zip);

        try (ZFile zf = new ZFile(zip)) {
            assertEquals(1, zf.entries().size());
            StoredEntry a = zf.get("a");
            assertNotNull(a);
            assertArrayEquals(new byte[]{1, 2, 3}, a.read());
        }
    }

    @Test
    public void addFileRecursively() throws Exception {
        File tdir = mTemporaryFolder.newFolder();
        File tfile = new File(tdir, "blah-blah");
        Files.write("blah", tfile, Charsets.US_ASCII);

        File zip = new File(tdir, "f.zip");
        try (ZFile zf = new ZFile(zip)) {
            zf.addAllRecursively(tfile);

            StoredEntry blahEntry = zf.get("blah-blah");
            assertNotNull(blahEntry);
            String contents = new String(blahEntry.read(), Charsets.US_ASCII);
            assertEquals("blah", contents);
        }
    }

    @Test
    public void addDirectoryRecursively() throws Exception {
        File tdir = mTemporaryFolder.newFolder();

        String boom = Strings.repeat("BOOM!", 100);
        String kaboom = Strings.repeat("KABOOM!", 100);

        Files.write(boom, new File(tdir, "danger"), Charsets.US_ASCII);
        Files.write(kaboom, new File(tdir, "do not touch"), Charsets.US_ASCII);
        File safeDir = new File(tdir, "safe");
        assertTrue(safeDir.mkdir());

        String iLoveChocolate = Strings.repeat("I love chocolate! ", 200);
        String iLoveOrange = Strings.repeat("I love orange! ", 50);
        String loremIpsum = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean vitae "
                + "turpis quis justo scelerisque vulputate in et magna. Suspendisse eleifend "
                + "ultricies nisi, placerat consequat risus accumsan et. Pellentesque habitant "
                + "morbi tristique senectus et netus et malesuada fames ac turpis egestas. "
                + "Integer vitae leo purus. Nulla facilisi. Duis ligula libero, lacinia a "
                + "malesuada a, viverra tempor sapien. Donec eget consequat sapien, ultrices"
                + "interdum diam. Maecenas ipsum erat, suscipit at iaculis a, mollis nec risus. "
                + "Quisque tristique ac velit sed auctor. Nulla lacus diam, tristique id sem non, "
                + "pellentesque commodo mauris.";

        Files.write(iLoveChocolate, new File(safeDir, "eat.sweet"), Charsets.US_ASCII);
        Files.write(iLoveOrange, new File(safeDir, "eat.fruit"), Charsets.US_ASCII);
        Files.write(loremIpsum, new File(safeDir, "bedtime.reading.txt"), Charsets.US_ASCII);

        File zip = new File(tdir, "f.zip");
        try (ZFile zf = new ZFile(zip)) {
            zf.addAllRecursively(tdir, (f) -> !f.getName().startsWith("eat."));

            assertEquals(6, zf.entries().size());

            StoredEntry boomEntry = zf.get("danger");
            assertNotNull(boomEntry);
            assertEquals(CompressionMethod.DEFLATE,
                    boomEntry.getCentralDirectoryHeader().getCompressionInfoWithWait().getMethod());
            assertEquals(boom, new String(boomEntry.read(), Charsets.US_ASCII));

            StoredEntry kaboomEntry = zf.get("do not touch");
            assertNotNull(kaboomEntry);
            assertEquals(CompressionMethod.DEFLATE,
                    kaboomEntry
                            .getCentralDirectoryHeader()
                            .getCompressionInfoWithWait()
                            .getMethod());
            assertEquals(kaboom, new String(kaboomEntry.read(), Charsets.US_ASCII));

            StoredEntry safeEntry = zf.get("safe/");
            assertNotNull(safeEntry);
            assertEquals(0, safeEntry.read().length);

            StoredEntry choc = zf.get("safe/eat.sweet");
            assertNotNull(choc);
            assertEquals(CompressionMethod.STORE,
                    choc.getCentralDirectoryHeader().getCompressionInfoWithWait().getMethod());
            assertEquals(iLoveChocolate, new String(choc.read(), Charsets.US_ASCII));

            StoredEntry orangeEntry = zf.get("safe/eat.fruit");
            assertNotNull(orangeEntry);
            assertEquals(CompressionMethod.STORE,
                    orangeEntry
                            .getCentralDirectoryHeader()
                            .getCompressionInfoWithWait()
                            .getMethod());
            assertEquals(iLoveOrange, new String(orangeEntry.read(), Charsets.US_ASCII));

            StoredEntry loremEntry = zf.get("safe/bedtime.reading.txt");
            assertNotNull(loremEntry);
            assertEquals(CompressionMethod.DEFLATE,
                    loremEntry
                            .getCentralDirectoryHeader()
                            .getCompressionInfoWithWait()
                            .getMethod());
            assertEquals(loremIpsum, new String(loremEntry.read(), Charsets.US_ASCII));
        }
    }

    @Test
    public void extraDirectoryOffsetEmptyFile() throws Exception {
        File zipNoOffsetFile = new File(mTemporaryFolder.getRoot(), "a.zip");
        File zipWithOffsetFile = new File(mTemporaryFolder.getRoot(), "b.zip");

        int offset = 31;

        long zipNoOffsetSize;
        try (
                ZFile zipNoOffset = new ZFile(zipNoOffsetFile);
                ZFile zipWithOffset = new ZFile(zipWithOffsetFile)) {
            zipWithOffset.setExtraDirectoryOffset(offset);

            zipNoOffset.close();
            zipWithOffset.close();

            zipNoOffsetSize = zipNoOffsetFile.length();
            long zipWithOffsetSize = zipWithOffsetFile.length();

            assertEquals(zipNoOffsetSize + offset, zipWithOffsetSize);

            /*
             * EOCD with no comment has 22 bytes.
             */
            assertEquals(0, zipNoOffset.getCentralDirectoryOffset());
            assertEquals(0, zipNoOffset.getCentralDirectorySize());
            assertEquals(0, zipNoOffset.getEocdOffset());
            assertEquals(ZFileTestConstants.EOCD_SIZE, zipNoOffset.getEocdSize());
            assertEquals(offset, zipWithOffset.getCentralDirectoryOffset());
            assertEquals(0, zipWithOffset.getCentralDirectorySize());
            assertEquals(offset, zipWithOffset.getEocdOffset());
            assertEquals(ZFileTestConstants.EOCD_SIZE, zipWithOffset.getEocdSize());
        }

        /*
         * The EOCDs should not differ up until the end of the Central Directory size and should
         * not differ after the offset
         */
        int p1Start = 0;
        int p1Size = Eocd.F_CD_SIZE.endOffset();
        int p2Start = Eocd.F_CD_OFFSET.endOffset();
        int p2Size = (int) zipNoOffsetSize - p2Start;

        byte[] noOffsetData1 = readSegment(zipNoOffsetFile, p1Start, p1Size);
        byte[] noOffsetData2 = readSegment(zipNoOffsetFile, p2Start, p2Size);
        byte[] withOffsetData1 = readSegment(zipWithOffsetFile, offset, p1Size);
        byte[] withOffsetData2 = readSegment(zipWithOffsetFile, offset + p2Start, p2Size);

        assertArrayEquals(noOffsetData1, withOffsetData1);
        assertArrayEquals(noOffsetData2, withOffsetData2);

        try (ZFile readWithOffset = new ZFile(zipWithOffsetFile)) {
            assertEquals(0, readWithOffset.entries().size());
        }
    }

    @Test
    public void extraDirectoryOffsetNonEmptyFile() throws Exception {
        File zipNoOffsetFile = new File(mTemporaryFolder.getRoot(), "a.zip");
        File zipWithOffsetFile = new File(mTemporaryFolder.getRoot(), "b.zip");

        int cdSize;

        // The byte arrays below are larger when compressed, so we end up storing them uncompressed,
        // which would normally cause them to be 4-aligned. Disable that, to make calculations
        // easier.
        ZFileOptions options = new ZFileOptions();
        options.setAlignmentRule(AlignmentRules.constant(AlignmentRule.NO_ALIGNMENT));

        try (ZFile zipNoOffset = new ZFile(zipNoOffsetFile, options);
                ZFile zipWithOffset = new ZFile(zipWithOffsetFile, options)) {
            zipWithOffset.setExtraDirectoryOffset(37);

            zipNoOffset.add("x", new ByteArrayInputStream(new byte[]{1, 2}));
            zipWithOffset.add("x", new ByteArrayInputStream(new byte[]{1, 2}));

            zipNoOffset.close();
            zipWithOffset.close();

            long zipNoOffsetSize = zipNoOffsetFile.length();
            long zipWithOffsetSize = zipWithOffsetFile.length();

            assertEquals(zipNoOffsetSize + 37, zipWithOffsetSize);

            /*
             * Local file header has 30 bytes + name.
             * Central directory entry has 46 bytes + name
             * EOCD with no comment has 22 bytes.
             */
            assertEquals(ZFileTestConstants.LOCAL_HEADER_SIZE + 1 + 2,
                    zipNoOffset.getCentralDirectoryOffset());
            cdSize = (int) zipNoOffset.getCentralDirectorySize();
            assertEquals(ZFileTestConstants.CENTRAL_DIRECTORY_ENTRY_SIZE + 1, cdSize);
            assertEquals(ZFileTestConstants.LOCAL_HEADER_SIZE + 1 + 2 + cdSize,
                    zipNoOffset.getEocdOffset());
            assertEquals(ZFileTestConstants.EOCD_SIZE, zipNoOffset.getEocdSize());
            assertEquals(ZFileTestConstants.LOCAL_HEADER_SIZE + 1 + 2 + 37,
                    zipWithOffset.getCentralDirectoryOffset());
            assertEquals(cdSize, zipWithOffset.getCentralDirectorySize());
            assertEquals(ZFileTestConstants.LOCAL_HEADER_SIZE + 1 + 2 + 37 + cdSize,
                    zipWithOffset.getEocdOffset());
            assertEquals(ZFileTestConstants.EOCD_SIZE, zipWithOffset.getEocdSize());
        }

        /*
         * The files should be equal: until the end of the first entry, from the beginning of the
         * central directory until the offset field in the EOCD and after the offset field.
         */
        int p1Start = 0;
        int p1Size = ZFileTestConstants.LOCAL_HEADER_SIZE + 1 + 2;
        int p2Start = ZFileTestConstants.LOCAL_HEADER_SIZE + 1 + 2;
        int p2Size = cdSize + Eocd.F_CD_SIZE.endOffset();
        int p3Start = p2Start + cdSize + Eocd.F_CD_OFFSET.endOffset();
        int p3Size = ZFileTestConstants.EOCD_SIZE - Eocd.F_CD_OFFSET.endOffset();

        byte[] noOffsetData1 = readSegment(zipNoOffsetFile, p1Start, p1Size);
        byte[] noOffsetData2 = readSegment(zipNoOffsetFile, p2Start, p2Size);
        byte[] noOffsetData3 = readSegment(zipNoOffsetFile, p3Start, p3Size);
        byte[] withOffsetData1 = readSegment(zipWithOffsetFile, p1Start, p1Size);
        byte[] withOffsetData2 = readSegment(zipWithOffsetFile, 37 + p2Start, p2Size);
        byte[] withOffsetData3 = readSegment(zipWithOffsetFile, 37 + p3Start, p3Size);

        assertArrayEquals(noOffsetData1, withOffsetData1);
        assertArrayEquals(noOffsetData2, withOffsetData2);
        assertArrayEquals(noOffsetData3, withOffsetData3);

        try (ZFile readWithOffset = new ZFile(zipWithOffsetFile)) {
            assertEquals(1, readWithOffset.entries().size());
        }
    }

    @Test
    public void changeExtraDirectoryOffset() throws Exception {
        File zipFile = new File(mTemporaryFolder.getRoot(), "a.zip");

        try (ZFile zip = new ZFile(zipFile)) {
            zip.add("x", new ByteArrayInputStream(new byte[]{1, 2}));
            zip.close();

            long noOffsetSize = zipFile.length();

            zip.setExtraDirectoryOffset(177);
            zip.close();

            long withOffsetSize = zipFile.length();

            assertEquals(noOffsetSize + 177, withOffsetSize);
        }
    }

    @Test
    public void computeOffsetWhenReadingEmptyFile() throws Exception {
        File zipFile = new File(mTemporaryFolder.getRoot(), "a.zip");

        try (ZFile zip = new ZFile(zipFile)) {
            zip.setExtraDirectoryOffset(18);
        }

        try (ZFile zip = new ZFile(zipFile)) {
            assertEquals(18, zip.getExtraDirectoryOffset());
        }
    }

    @Test
    public void computeOffsetWhenReadingNonEmptyFile() throws Exception {
        File zipFile = new File(mTemporaryFolder.getRoot(), "a.zip");

        try (ZFile zip = new ZFile(zipFile)) {
            zip.setExtraDirectoryOffset(287);
            zip.add("x", new ByteArrayInputStream(new byte[]{1, 2}));
        }

        try (ZFile zip = new ZFile(zipFile)) {
            assertEquals(287, zip.getExtraDirectoryOffset());
        }
    }

    @Test
    public void obtainingCDAndEocdWhenEntriesWrittenOnEmptyZip() throws Exception {
        File zipFile = new File(mTemporaryFolder.getRoot(), "a.zip");

        byte[][] cd = new byte[1][];
        byte[][] eocd = new byte[1][];

        try (ZFile zip = new ZFile(zipFile)) {
            zip.addZFileExtension(new ZFileExtension() {
                @Override
                public void entriesWritten() throws IOException {
                    cd[0] = zip.getCentralDirectoryBytes();
                    eocd[0] = zip.getEocdBytes();
                }
            });
        }

        assertNotNull(cd[0]);
        assertEquals(0, cd[0].length);
        assertNotNull(eocd[0]);
        assertEquals(22, eocd[0].length);
    }

    @Test
    public void obtainingCDAndEocdWhenEntriesWrittenOnNonEmptyZip() throws Exception {
        File zipFile = new File(mTemporaryFolder.getRoot(), "a.zip");

        byte[][] cd = new byte[1][];
        byte[][] eocd = new byte[1][];

        try (ZFile zip = new ZFile(zipFile)) {
            zip.add("foo", new ByteArrayInputStream(new byte[0]));
            zip.addZFileExtension(new ZFileExtension() {
                @Override
                public void entriesWritten() throws IOException {
                    cd[0] = zip.getCentralDirectoryBytes();
                    eocd[0] = zip.getEocdBytes();
                }
            });
        }

        /*
         * Central directory entry has 46 bytes + name
         * EOCD with no comment has 22 bytes.
         */
        assertNotNull(cd[0]);
        assertEquals(46 + 3, cd[0].length);
        assertNotNull(eocd[0]);
        assertEquals(22, eocd[0].length);
    }

    @Test
    public void java7JarSupported() throws Exception {
        File jar = ZipTestUtils.cloneRsrc("j7.jar", mTemporaryFolder);

        try (ZFile j = new ZFile(jar)) {
            assertEquals(8, j.entries().size());
        }
    }

    @Test
    public void java8JarSupported() throws Exception {
        File jar = ZipTestUtils.cloneRsrc("j8.jar", mTemporaryFolder);

        try (ZFile j = new ZFile(jar)) {
            assertEquals(8, j.entries().size());
        }
    }

    @Test
    public void utf8NamesSupportedOnReading() throws Exception {
        File zip = ZipTestUtils.cloneRsrc("zip-with-utf8-filename.zip", mTemporaryFolder);

        try (ZFile f = new ZFile(zip)) {
            assertEquals(1, f.entries().size());

            StoredEntry entry = f.entries().iterator().next();
            String filetMignonKorean = "\uc548\uc2eC \uc694\ub9ac";
            String isGoodJapanese = "\u3068\u3066\u3082\u826f\u3044";

            assertEquals(
                    filetMignonKorean + " " + isGoodJapanese,
                    entry.getCentralDirectoryHeader().getName());
            assertArrayEquals(
                    "Stuff about food is good.\n".getBytes(Charsets.US_ASCII), entry.read());
        }
    }

    @Test
    public void utf8NamesSupportedOnReadingWithoutUtf8Flag() throws Exception {
        File zip = ZipTestUtils.cloneRsrc("zip-with-utf8-filename.zip", mTemporaryFolder);

        // Reset bytes 7 and 122 that have the flag in the local header and central directory.
        byte[] data = Files.toByteArray(zip);
        data[7] = 0;
        data[122] = 0;
        Files.write(data, zip);

        try (ZFile f = new ZFile(zip)) {
            assertEquals(1, f.entries().size());

            StoredEntry entry = f.entries().iterator().next();
            String filetMignonKorean = "\uc548\uc2eC \uc694\ub9ac";
            String isGoodJapanese = "\u3068\u3066\u3082\u826f\u3044";

            assertEquals(
                    filetMignonKorean + " " + isGoodJapanese,
                    entry.getCentralDirectoryHeader().getName());
            assertArrayEquals(
                    "Stuff about food is good.\n".getBytes(Charsets.US_ASCII),
                    entry.read());
        }
    }

    @Test
    public void utf8NamesSupportedOnWriting() throws Exception {
        File zipFile = new File(mTemporaryFolder.getRoot(), "a.zip");
        String lettuceIsHealthyArmenian = "\u0533\u0561\u0566\u0561\u0580\u0020\u0561\u057C"
                + "\u0578\u0572\u057B";

        try (ZFile zip = new ZFile(zipFile)) {
            zip.add(lettuceIsHealthyArmenian, new ByteArrayInputStream(new byte[]{0}));
        }

        try (ZFile zip2 = new ZFile(zipFile)) {
            assertEquals(1, zip2.entries().size());
            StoredEntry entry = zip2.entries().iterator().next();
            assertEquals(lettuceIsHealthyArmenian, entry.getCentralDirectoryHeader().getName());
        }
    }

    @Test
    public void zipMemoryUsageIsZeroAfterClose() throws Exception {
        File zipFile = new File(mTemporaryFolder.getRoot(), "a.zip");

        ZFileOptions options = new ZFileOptions();
        long used;
        try (ZFile zip = new ZFile(zipFile, options)) {

            assertEquals(0, options.getTracker().getBytesUsed());
            assertEquals(0, options.getTracker().getMaxBytesUsed());

            zip.add("Blah", new ByteArrayInputStream(new byte[500]));
            used = options.getTracker().getBytesUsed();
            assertTrue(used > 500);
            assertEquals(used, options.getTracker().getMaxBytesUsed());
        }

        assertEquals(0, options.getTracker().getBytesUsed());
        assertEquals(used, options.getTracker().getMaxBytesUsed());
    }

    @Test
    public void unusedZipAreasAreClearedOnWrite() throws Exception {
        File zipFile = new File(mTemporaryFolder.getRoot(), "a.zip");
        ZFileOptions options = new ZFileOptions();
        options.setAlignmentRule(AlignmentRules.constantForSuffix(".txt", 1000));
        try (ZFile zf = new ZFile(zipFile, options)) {
            zf.add("test1.txt", new ByteArrayInputStream(new byte[]{1}), false);
        }

        /*
         * Write dummy data in some unused portion of the file.
         */
        try (RandomAccessFile raf = new RandomAccessFile(zipFile, "rw")) {

            raf.seek(500);
            byte[] dummyData = "Dummy".getBytes(Charsets.US_ASCII);
            raf.write(dummyData);
        }

        try (ZFile zf = new ZFile(zipFile)) {
            zf.touch();
        }

        try (RandomAccessFile raf = new RandomAccessFile(zipFile, "r")) {

            /*
             * test1.txt won't take more than 200 bytes. Additionally, the header for
             */
            byte[] data = new byte[900];
            RandomAccessFileUtils.fullyRead(raf, data);

            byte[] zeroData = new byte[data.length];
            assertArrayEquals(zeroData, data);
        }
    }

    @Test
    public void deferredCompression() throws Exception {
        File zipFile = new File(mTemporaryFolder.getRoot(), "a.zip");

        ExecutorService executor = Executors.newSingleThreadExecutor();

        ZFileOptions options = new ZFileOptions();
        boolean[] done = new boolean[1];
        options.setCompressor(new DeflateExecutionCompressor(executor, options.getTracker(),
                Deflater.BEST_COMPRESSION) {
            @Nonnull
            @Override
            protected CompressionResult immediateCompress(@Nonnull CloseableByteSource source)
                    throws Exception {
                Thread.sleep(500);
                CompressionResult cr = super.immediateCompress(source);
                done[0] = true;
                return cr;
            }
        });

        try (ZFile zip = new ZFile(zipFile, options)) {
            byte sequences = 100;
            int seqCount = 1000;
            byte[] compressableData = new byte[sequences * seqCount];
            for (byte i = 0; i < sequences; i++) {
                for (int j = 0; j < seqCount; j++) {
                    compressableData[i * seqCount + j] = i;
                }
            }

            zip.add("compressedFile", new ByteArrayInputStream(compressableData));
            assertFalse(done[0]);

            /*
             * Even before closing, eventually all the stream will be read.
             */
            long tooLong = System.currentTimeMillis() + 10000;
            while (!done[0] && System.currentTimeMillis() < tooLong) {
                Thread.sleep(10);
            }

            assertTrue(done[0]);
        }

        executor.shutdownNow();
    }

    @Test
    public void zipFileWithEocdMarkerInComment() throws Exception {
        File zipFile = new File(mTemporaryFolder.getRoot(), "x");

        try (Closer closer = Closer.create()) {
            ZipOutputStream zos = closer.register(
                    new ZipOutputStream(new FileOutputStream(zipFile)));
            zos.setComment("\u0065\u4b50");
            zos.putNextEntry(new ZipEntry("foo"));
            zos.write(new byte[] { 1, 2, 3, 4 });
            zos.close();

            ZFile zf = closer.register(new ZFile(zipFile));
            StoredEntry entry = zf.get("foo");
            assertNotNull(entry);
            assertEquals(4, entry.getCentralDirectoryHeader().getUncompressedSize());
        }
    }

    @Test
    public void zipFileWithEocdMarkerInFileName() throws Exception {
        File zipFile = new File(mTemporaryFolder.getRoot(), "x");

        String fname = "tricky-\u0050\u004b\u0005\u0006";
        byte[] bytes = new byte[] { 1, 2, 3, 4 };

        try (Closer closer = Closer.create()) {
            ZipOutputStream zos = closer.register(
                    new ZipOutputStream(new FileOutputStream(zipFile)));
            zos.putNextEntry(new ZipEntry(fname));
            zos.write(bytes);
            zos.close();

            ZFile zf = closer.register(new ZFile(zipFile));
            StoredEntry entry = zf.get(fname);
            assertNotNull(entry);
            assertEquals(4, entry.getCentralDirectoryHeader().getUncompressedSize());
        }
    }

    @Test
    public void zipFileWithEocdMarkerInFileContents() throws Exception {
        File zipFile = new File(mTemporaryFolder.getRoot(), "x");

        byte[] bytes = new byte[] { 0x50, 0x4b, 0x05, 0x06 };

        try (Closer closer = Closer.create()) {
            ZipOutputStream zos = closer.register(
                    new ZipOutputStream(new FileOutputStream(zipFile)));
            ZipEntry zipEntry = new ZipEntry("file");
            zipEntry.setMethod(ZipEntry.STORED);
            zipEntry.setCompressedSize(4);
            zipEntry.setSize(4);
            zipEntry.setCrc(Hashing.crc32().hashBytes(bytes).padToLong());
            zos.putNextEntry(zipEntry);
            zos.write(bytes);
            zos.close();

            ZFile zf = closer.register(new ZFile(zipFile));
            StoredEntry entry = zf.get("file");
            assertNotNull(entry);
            assertEquals(4, entry.getCentralDirectoryHeader().getUncompressedSize());
        }
    }

    @Test
    public void replaceVeryLargeFileWithBiggerInMiddleOfZip() throws Exception {
        File zipFile = new File(mTemporaryFolder.getRoot(), "x");

        long small1Offset;
        long small2Offset;
        ZFileOptions coverOptions = new ZFileOptions();
        coverOptions.setCoverEmptySpaceUsingExtraField(true);
        try (ZFile zf = new ZFile(zipFile, coverOptions)) {
            zf.add("small1", new ByteArrayInputStream(new byte[] { 0, 1 }));
        }

        try (ZFile zf = new ZFile(zipFile, coverOptions)) {
            zf.add("verybig", new ByteArrayInputStream(new byte[100_000]), false);
        }

        try (ZFile zf = new ZFile(zipFile, coverOptions)) {
            zf.add("small2", new ByteArrayInputStream(new byte[] { 0, 1 }));
        }

        try (ZFile zf = new ZFile(zipFile, coverOptions)) {
            StoredEntry se = zf.get("small1");
            assertNotNull(se);
            small1Offset = se.getCentralDirectoryHeader().getOffset();

            se = zf.get("small2");
            assertNotNull(se);
            small2Offset = se.getCentralDirectoryHeader().getOffset();

            se = zf.get("verybig");
            assertNotNull(se);
            se.delete();

            zf.add("evenbigger", new ByteArrayInputStream(new byte[110_000]), false);
        }

        try (ZFile zf = new ZFile(zipFile, coverOptions)) {
            StoredEntry se = zf.get("small1");
            assertNotNull(se);
            assertEquals(se.getCentralDirectoryHeader().getOffset(), small1Offset);

            se = zf.get("small2");
            assertNotNull(se);
            assertNotEquals(se.getCentralDirectoryHeader().getOffset(), small2Offset);
        }
    }

    @Test
    public void regressionRepackingDoesNotFail() throws Exception {
        File zipFile = new File(mTemporaryFolder.getRoot(), "x");

        ZFileOptions coverOptions = new ZFileOptions();
        coverOptions.setCoverEmptySpaceUsingExtraField(true);
        try (ZFile zf = new ZFile(zipFile, coverOptions)) {
            zf.add("small_1", new ByteArrayInputStream(new byte[] { 0, 1 }));
            zf.add("very_big", new ByteArrayInputStream(new byte[100_000]), false);
            zf.add("small_2", new ByteArrayInputStream(new byte[] { 0, 1 }));
            zf.add("big", new ByteArrayInputStream(new byte[10_000]), false);
            zf.add("small_3", new ByteArrayInputStream(new byte[] { 0, 1 }));
        }

        /*
         * Regression we're covering is that small_2 cannot be extended to cover up for the space
         * taken by very_big and needs to be repositioned. However, the algorithm to reposition
         * will put it in the best-fitting block, which is the one in "big", failing to actually
         * move it backwards in the file.
         */
        try (ZFile zf = new ZFile(zipFile, coverOptions)) {
            StoredEntry se = zf.get("big");
            assertNotNull(se);
            se.delete();

            se = zf.get("very_big");
            assertNotNull(se);
            se.delete();
        }
    }

    @Test
    public void cannotAddMoreThan0x7fffExtraField() throws Exception {
        File zipFile = new File(mTemporaryFolder.getRoot(), "a.zip");

        ZFileOptions zfo = new ZFileOptions();
        zfo.setCoverEmptySpaceUsingExtraField(true);

        /*
         * Create a zip file with:
         *
         * [small file][large file with exactly 0x8000 bytes][small file 2]
         */
        long smallFile1Offset;
        long smallFile2Offset;
        long largeFileOffset;
        String largeFileName = "Large file";
        try (ZFile zf = new ZFile(zipFile, zfo)) {
            zf.add("Small file", new ByteArrayInputStream(new byte[] { 0, 1 }));

            int largeFileTotalSize = 0x8000;
            int largeFileContentsSize =
                    largeFileTotalSize
                            - ZFileTestConstants.LOCAL_HEADER_SIZE
                            - largeFileName.length();

            zf.add(largeFileName, new ByteArrayInputStream(new byte[largeFileContentsSize]), false);
            zf.add("Small file 2", new ByteArrayInputStream(new byte[] { 0, 1 }));

            zf.update();

            StoredEntry sfEntry = zf.get("Small file");
            assertNotNull(sfEntry);
            smallFile1Offset = sfEntry.getCentralDirectoryHeader().getOffset();
            assertEquals(0, smallFile1Offset);

            StoredEntry lfEntry = zf.get(largeFileName);
            assertNotNull(lfEntry);
            largeFileOffset = lfEntry.getCentralDirectoryHeader().getOffset();

            StoredEntry sf2Entry = zf.get("Small file 2");
            assertNotNull(sf2Entry);
            smallFile2Offset = sf2Entry.getCentralDirectoryHeader().getOffset();

            assertEquals(largeFileTotalSize, smallFile2Offset - largeFileOffset);
        }

        /*
         * Remove the large file from the zip file and check that small file 2 has been moved, but
         * no extra field has been added.
         */
        try (ZFile zf = new ZFile(zipFile, zfo)) {
            StoredEntry lfEntry = zf.get(largeFileName);
            assertNotNull(lfEntry);
            lfEntry.delete();

            zf.update();

            StoredEntry sfEntry = zf.get("Small file");
            assertNotNull(sfEntry);
            smallFile1Offset = sfEntry.getCentralDirectoryHeader().getOffset();
            assertEquals(0, smallFile1Offset);

            StoredEntry sf2Entry = zf.get("Small file 2");
            assertNotNull(sf2Entry);
            long newSmallFile2Offset = sf2Entry.getCentralDirectoryHeader().getOffset();
            assertEquals(largeFileOffset, newSmallFile2Offset);

            assertEquals(0, sf2Entry.getLocalExtra().size());
        }
    }

    @Test
    public void canAddMoreThan0x7fffExtraField() throws Exception {
        File zipFile = new File(mTemporaryFolder.getRoot(), "a.zip");

        ZFileOptions zfo = new ZFileOptions();
        zfo.setCoverEmptySpaceUsingExtraField(true);

        /*
         * Create a zip file with:
         *
         * [small file][large file with exactly 0x7fff bytes][small file 2]
         */
        long smallFile1Offset;
        long smallFile2Offset;
        long largeFileOffset;
        String largeFileName = "Large file";
        int largeFileTotalSize = 0x7fff;
        try (ZFile zf = new ZFile(zipFile, zfo)) {
            zf.add("Small file", new ByteArrayInputStream(new byte[] { 0, 1 }));

            int largeFileContentsSize =
                    largeFileTotalSize
                            - ZFileTestConstants.LOCAL_HEADER_SIZE
                            - largeFileName.length();

            zf.add(largeFileName, new ByteArrayInputStream(new byte[largeFileContentsSize]), false);
            zf.add("Small file 2", new ByteArrayInputStream(new byte[] { 0, 1 }));

            zf.update();

            StoredEntry sfEntry = zf.get("Small file");
            assertNotNull(sfEntry);
            smallFile1Offset = sfEntry.getCentralDirectoryHeader().getOffset();
            assertEquals(0, smallFile1Offset);

            StoredEntry lfEntry = zf.get(largeFileName);
            assertNotNull(lfEntry);
            largeFileOffset = lfEntry.getCentralDirectoryHeader().getOffset();

            StoredEntry sf2Entry = zf.get("Small file 2");
            assertNotNull(sf2Entry);
            smallFile2Offset = sf2Entry.getCentralDirectoryHeader().getOffset();

            assertEquals(largeFileTotalSize, smallFile2Offset - largeFileOffset);
        }

        /*
         * Remove the large file from the zip file and check that small file 2 has been moved back
         * but with 0x7fff extra space added.
         */
        try (ZFile zf = new ZFile(zipFile, zfo)) {
            StoredEntry lfEntry = zf.get(largeFileName);
            assertNotNull(lfEntry);
            lfEntry.delete();

            zf.update();

            StoredEntry sfEntry = zf.get("Small file");
            assertNotNull(sfEntry);
            smallFile1Offset = sfEntry.getCentralDirectoryHeader().getOffset();
            assertEquals(0, smallFile1Offset);

            StoredEntry sf2Entry = zf.get("Small file 2");
            assertNotNull(sf2Entry);
            long newSmallFile2Offset = sf2Entry.getCentralDirectoryHeader().getOffset();

            assertEquals(largeFileOffset, newSmallFile2Offset);
            assertEquals(largeFileTotalSize, sf2Entry.getLocalExtra().size());
        }
    }

    @Test
    public void detectIncorrectCRC32InLocalHeader() throws Exception {
        File zipFile = new File(mTemporaryFolder.getRoot(), "a.zip");

        /*
         * Zip files created by ZFile never have data descriptors so we need to create one using
         * java's zip.
         */
        try (
                FileOutputStream fos = new FileOutputStream(zipFile);
                ZipOutputStream zos = new ZipOutputStream(fos)) {
            ZipEntry ze = new ZipEntry("foo");
            zos.putNextEntry(ze);
            byte[] randomBytes = new byte[512];
            new Random().nextBytes(randomBytes);
            zos.write(randomBytes);
        }

        /*
         * Open the zip file and compute where the local header CRC32 is.
         */
        long crcOffset;
        try (ZFile zf = new ZFile(zipFile)) {
            StoredEntry se = zf.get("foo");
            assertNotNull(se);
            long cdOffset = zf.getCentralDirectoryOffset();

            /*
             * Twelve bytes from the CD offset, we have the start of the CRC32 of the zip entry.
             */
            crcOffset = cdOffset - 12;
        }

        /*
         * Corrupt the CRC32.
         */
        byte[] crc = readSegment(zipFile, crcOffset, 4);
        crc[0]++;
        try (RandomAccessFile raf = new RandomAccessFile(zipFile, "rw")) {
            raf.seek(crcOffset);
            raf.write(crc);
        }

        /*
         * Now open the zip file and it should write a message in the log.
         */
        ZFileOptions options = new ZFileOptions();
        options.setVerifyLogFactory(VerifyLogs::unlimited);
        try (ZFile zf = new ZFile(zipFile, options)) {
            VerifyLog vl = zf.getVerifyLog();
            assertTrue(vl.getLogs().isEmpty());
            StoredEntry fooEntry = zf.get("foo");
            vl = fooEntry.getVerifyLog();
            assertEquals(1, vl.getLogs().size());
            assertTrue(vl.getLogs().get(0).contains("CRC32"));
        }
    }

    @Test
    public void detectIncorrectVersionToExtractInCentralDirectory() throws Exception {
        File zipFile = new File(mTemporaryFolder.getRoot(), "a.zip");

        /*
         * Create a valid zip file.
         */
        try (ZFile zf = new ZFile(zipFile)) {
            zf.add("foo", new ByteArrayInputStream(new byte[0]));
        }

        /*
         * Change the "version to extract" in the central directory to 0x7777.
         */
        int versionToExtractOffset =
                ZFileTestConstants.LOCAL_HEADER_SIZE
                        + 3
                        + CentralDirectory.F_VERSION_EXTRACT.offset();
        byte[] allZipBytes = Files.toByteArray(zipFile);
        allZipBytes[versionToExtractOffset] = 0x77;
        allZipBytes[versionToExtractOffset + 1] = 0x77;
        Files.write(allZipBytes, zipFile);

        /*
         * Opening the file and it should write a message in the log. The entry has the right
         * version to extract (20), but it issues a warning because it is not equal to the one
         * in the central directory.
         */
        ZFileOptions options = new ZFileOptions();
        options.setVerifyLogFactory(VerifyLogs::unlimited);
        try (ZFile zf = new ZFile(zipFile, options)) {
            VerifyLog vl = zf.getVerifyLog();
            assertEquals(1, vl.getLogs().size());
            assertTrue(vl.getLogs().get(0).toLowerCase(Locale.US).contains("version"));
            assertTrue(vl.getLogs().get(0).toLowerCase(Locale.US).contains("extract"));
            StoredEntry fooEntry = zf.get("foo");
            vl = fooEntry.getVerifyLog();
            assertEquals(1, vl.getLogs().size());
            assertTrue(vl.getLogs().get(0).toLowerCase(Locale.US).contains("version"));
            assertTrue(vl.getLogs().get(0).toLowerCase(Locale.US).contains("extract"));
        }
    }

    @Test
    public void detectIncorrectVersionToExtractInLocalHeader() throws Exception {
        File zipFile = new File(mTemporaryFolder.getRoot(), "a.zip");

        /*
         * Create a valid zip file.
         */
        try (ZFile zf = new ZFile(zipFile)) {
            zf.add("foo", new ByteArrayInputStream(new byte[0]));
        }

        /*
         * Change the "version to extract" in the local header to 0x7777.
         */
        int versionToExtractOffset = StoredEntry.F_VERSION_EXTRACT.offset();
        byte[] allZipBytes = Files.toByteArray(zipFile);
        allZipBytes[versionToExtractOffset] = 0x77;
        allZipBytes[versionToExtractOffset + 1] = 0x77;
        Files.write(allZipBytes, zipFile);

        /*
         * Opening the file should log an error message.
         */
        ZFileOptions options = new ZFileOptions();
        options.setVerifyLogFactory(VerifyLogs::unlimited);
        try (ZFile zf = new ZFile(zipFile, options)) {
            VerifyLog vl = zf.getVerifyLog();
            assertTrue(vl.getLogs().isEmpty());
            StoredEntry fooEntry = zf.get("foo");
            vl = fooEntry.getVerifyLog();
            assertEquals(1, vl.getLogs().size());
            assertTrue(vl.getLogs().get(0).toLowerCase(Locale.US).contains("version"));
            assertTrue(vl.getLogs().get(0).toLowerCase(Locale.US).contains("extract"));
        }
    }

    @Test
    public void sortZipContentsWithDeferredCrc32() throws Exception {
        /*
         * Create a zip file with deferred CRC32 and files in non-alphabetical order.
         * ZipOutputStream always creates deferred CRC32 entries.
         */
        File zipFile = new File(mTemporaryFolder.getRoot(), "a.zip");
        try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFile))) {
            zos.putNextEntry(new ZipEntry("b"));
            zos.write(new byte[1000]);
            zos.putNextEntry(new ZipEntry("a"));
            zos.write(new byte[1000]);
        }

        /*
         * Now open the zip using a ZFile and sort the contents and check that the deferred CRC32
         * bits were reset.
         */
        try (ZFile zf = new ZFile(zipFile)) {
            StoredEntry a = zf.get("a");
            assertNotNull(a);
            assertNotSame(DataDescriptorType.NO_DATA_DESCRIPTOR, a.getDataDescriptorType());
            StoredEntry b = zf.get("b");
            assertNotNull(b);
            assertNotSame(DataDescriptorType.NO_DATA_DESCRIPTOR, b.getDataDescriptorType());
            assertTrue(
                    a.getCentralDirectoryHeader().getOffset()
                            > b.getCentralDirectoryHeader().getOffset());

            zf.sortZipContents();
            zf.update();

            a = zf.get("a");
            assertNotNull(a);
            assertSame(DataDescriptorType.NO_DATA_DESCRIPTOR, a.getDataDescriptorType());
            b = zf.get("b");
            assertNotNull(b);
            assertSame(DataDescriptorType.NO_DATA_DESCRIPTOR, b.getDataDescriptorType());

            assertTrue(
                    a.getCentralDirectoryHeader().getOffset()
                            < b.getCentralDirectoryHeader().getOffset());
        }

        /*
         * Open the file again and check there are no warnings.
         */
        try (ZFile zf = new ZFile(zipFile)) {
            VerifyLog vl = zf.getVerifyLog();
            assertEquals(0, vl.getLogs().size());

            StoredEntry a = zf.get("a");
            assertNotNull(a);
            vl = a.getVerifyLog();
            assertEquals(0, vl.getLogs().size());

            StoredEntry b = zf.get("b");
            assertNotNull(b);
            vl = b.getVerifyLog();
            assertEquals(0, vl.getLogs().size());
        }
    }

    @Test
    public void alignZipContentsWithDeferredCrc32() throws Exception {
        /*
         * Create an unaligned zip file with deferred CRC32 and files in non-alphabetical order.
         * We need an uncompressed file to make realigning have any effect.
         */
        File zipFile = new File(mTemporaryFolder.getRoot(), "a.zip");
        try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFile))) {
            zos.putNextEntry(new ZipEntry("x"));
            zos.write(new byte[1000]);
            zos.putNextEntry(new ZipEntry("y"));
            zos.write(new byte[1000]);
            ZipEntry zEntry = new ZipEntry("z");
            zEntry.setSize(1000);
            zEntry.setMethod(ZipEntry.STORED);
            zEntry.setCrc(Hashing.crc32().hashBytes(new byte[1000]).asInt());
            zos.putNextEntry(zEntry);
            zos.write(new byte[1000]);
        }

        /*
         * Now open the zip using a ZFile and realign the contents and check that the deferred CRC32
         * bits were reset.
         */
        ZFileOptions options = new ZFileOptions();
        options.setAlignmentRule(AlignmentRules.constant(2000));
        try (ZFile zf = new ZFile(zipFile, options)) {
            StoredEntry x = zf.get("x");
            assertNotNull(x);
            assertNotSame(DataDescriptorType.NO_DATA_DESCRIPTOR, x.getDataDescriptorType());
            StoredEntry y = zf.get("y");
            assertNotNull(y);
            assertNotSame(DataDescriptorType.NO_DATA_DESCRIPTOR, y.getDataDescriptorType());
            StoredEntry z = zf.get("z");
            assertNotNull(z);
            assertSame(DataDescriptorType.NO_DATA_DESCRIPTOR, z.getDataDescriptorType());

            zf.realign();
            zf.update();

            x = zf.get("x");
            assertNotNull(x);
            assertSame(DataDescriptorType.NO_DATA_DESCRIPTOR, x.getDataDescriptorType());
            y = zf.get("y");
            assertNotNull(y);
            assertSame(DataDescriptorType.NO_DATA_DESCRIPTOR, y.getDataDescriptorType());
            z = zf.get("z");
            assertNotNull(z);
            assertSame(DataDescriptorType.NO_DATA_DESCRIPTOR, z.getDataDescriptorType());
        }
    }

    @Test
    public void openingZFileDoesNotRemoveDataDescriptors() throws Exception {
        /*
         * Create a zip file with deferred CRC32.
         */
        File zipFile = new File(mTemporaryFolder.getRoot(), "a.zip");
        try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFile))) {
            zos.putNextEntry(new ZipEntry("a"));
            zos.write(new byte[1000]);
        }

        /*
         * Open using ZFile and check that the deferred CRC32 is there.
         */
        try (ZFile zf = new ZFile(zipFile)) {
            StoredEntry se = zf.get("a");
            assertNotNull(se);
            assertNotEquals(DataDescriptorType.NO_DATA_DESCRIPTOR, se.getDataDescriptorType());
        }

        /*
         * Open using ZFile (again) and check that the deferred CRC32 is there.
         */
        try (ZFile zf = new ZFile(zipFile)) {
            StoredEntry se = zf.get("a");
            assertNotNull(se);
            assertNotEquals(DataDescriptorType.NO_DATA_DESCRIPTOR, se.getDataDescriptorType());
        }
    }

    @Test
    public void zipCommentsAreSaved() throws Exception {
        File zipFileWithComments = new File(mTemporaryFolder.getRoot(), "a.zip");
        try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFileWithComments))) {
            zos.setComment("foo");
        }

        /*
         * Open the zip file and check the comment is there.
         */
        try (ZFile zf = new ZFile(zipFileWithComments)) {
            byte[] comment = zf.getEocdComment();
            assertArrayEquals(new byte[] { 'f', 'o', 'o' }, comment);

            /*
             * Modify the comment and write the file.
             */
            zf.setEocdComment(new byte[] { 'b', 'a', 'r', 'r' });
        }

        /*
         * Open the file and see that the comment is there (both with java and zfile).
         */
        try (ZipFile zf2 = new ZipFile(zipFileWithComments)) {
            assertEquals("barr", zf2.getComment());
        }

        try (ZFile zf3 = new ZFile(zipFileWithComments)) {
            assertArrayEquals(new byte[] { 'b', 'a', 'r', 'r' }, zf3.getEocdComment());
        }
    }

    @Test
    public void eocdCommentsWithMoreThan64kNotAllowed() throws Exception {
        File zipFileWithComments = new File(mTemporaryFolder.getRoot(), "a.zip");
        try (ZFile zf = new ZFile(zipFileWithComments)) {
            try {
                zf.setEocdComment(new byte[65536]);
                fail();
            } catch (IllegalArgumentException e) {
                // Expected.
            }

            zf.setEocdComment(new byte[65535]);
        }
    }

    @Test
    public void eocdCommentsWithTheEocdMarkerAreAllowed() throws Exception {
        File zipFileWithComments = new File(mTemporaryFolder.getRoot(), "a.zip");
        byte[] data = new byte[100];
        data[50] = 0x50; // Signature
        data[51] = 0x4b;
        data[52] = 0x05;
        data[53] = 0x06;
        data[54] = 0x00; // Number of disk
        data[55] = 0x00;
        data[56] = 0x00; // Disk CD start
        data[57] = 0x00;
        data[54] = 0x01; // Total records 1
        data[55] = 0x00;
        data[56] = 0x02; // Total records 2, must be = to total records 1
        data[57] = 0x00;

        try (ZFile zf = new ZFile(zipFileWithComments)) {
            zf.setEocdComment(data);
        }

        try (ZFile zf = new ZFile(zipFileWithComments)) {
            assertArrayEquals(data, zf.getEocdComment());
        }
    }

    @Test
    public void eocdCommentsWithTheEocdMarkerThatAreInvalidAreNotAllowed() throws Exception {
        File zipFileWithComments = new File(mTemporaryFolder.getRoot(), "a.zip");
        byte[] data = new byte[100];
        data[50] = 0x50;
        data[51] = 0x4b;
        data[52] = 0x05;
        data[53] = 0x06;
        data[67] = 0x00;

        try (ZFile zf = new ZFile(zipFileWithComments)) {
            try {
                zf.setEocdComment(data);
                fail();
            } catch (IllegalArgumentException e) {
                // Expected.
            }
        }
    }

    @Test
    public void zipCommentsArePreservedWithFileChanges() throws Exception {
        File zipFileWithComments = new File(mTemporaryFolder.getRoot(), "a.zip");
        byte[] comment = new byte[] { 1, 3, 4 };
        try (ZFile zf = new ZFile(zipFileWithComments)) {
            zf.add("foo", new ByteArrayInputStream(new byte[50]));
            zf.setEocdComment(comment);
        }

        try (ZFile zf = new ZFile(zipFileWithComments)) {
            assertArrayEquals(comment, zf.getEocdComment());
            zf.add("bar", new ByteArrayInputStream(new byte[100]));
        }

        try (ZFile zf = new ZFile(zipFileWithComments)) {
            assertArrayEquals(comment, zf.getEocdComment());
        }
    }
}