1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.commons.compress.archivers.zip;
20
21 import static org.apache.commons.compress.AbstractTestCase.getFile;
22 import static org.junit.Assert.assertArrayEquals;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertNull;
27 import static org.junit.Assert.assertTrue;
28 import static org.junit.Assert.fail;
29 import static org.junit.Assume.assumeTrue;
30
31 import java.io.BufferedOutputStream;
32 import java.io.File;
33 import java.io.FileInputStream;
34 import java.io.FileOutputStream;
35 import java.io.IOException;
36 import java.io.InputStream;
37 import java.io.RandomAccessFile;
38 import java.util.Enumeration;
39 import java.util.Random;
40 import java.util.zip.ZipEntry;
41
42 import org.apache.commons.compress.AbstractTestCase;
43 import org.junit.Test;
44
45 public class Zip64SupportIT {
46
47 private static final long FIVE_BILLION = 5000000000l;
48 private static final int ONE_MILLION = 1000000;
49 private static final int ONE_HUNDRED_THOUSAND = 100000;
50
51 @Test public void read5GBOfZerosUsingInputStream() throws Throwable {
52 read5GBOfZerosImpl(get5GBZerosFile(), "5GB_of_Zeros");
53 }
54
55 @Test public void read5GBOfZerosGeneratedBy7ZIPUsingInputStream()
56 throws Throwable {
57 read5GBOfZerosImpl(get5GBZerosFileGeneratedBy7ZIP(), "5GB_of_Zeros");
58 }
59
60 @Test public void read5GBOfZerosGeneratedByJava7JarUsingInputStream()
61 throws Throwable {
62 read5GBOfZerosImpl(get5GBZerosFileGeneratedByJava7Jar(), "5GB_of_Zeros");
63 }
64
65 @Test public void read5GBOfZerosGeneratedByWinZIPUsingInputStream()
66 throws Throwable {
67 read5GBOfZerosImpl(get5GBZerosFileGeneratedByWinZIP(), "5GB_of_Zeros");
68 }
69
70 @Test public void read5GBOfZerosGeneratedByPKZipUsingInputStream()
71 throws Throwable {
72 read5GBOfZerosImpl(get5GBZerosFileGeneratedByPKZip(),
73 "zip6/5GB_of_Zeros");
74 }
75
76 @Test public void read100KFilesUsingInputStream() throws Throwable {
77 read100KFilesImpl(get100KFileFile());
78 }
79
80 @Test public void read100KFilesGeneratedBy7ZIPUsingInputStream()
81 throws Throwable {
82 read100KFilesImpl(get100KFileFileGeneratedBy7ZIP());
83 }
84
85 @Test public void read100KFilesGeneratedByWinCFUsingInputStream()
86 throws Throwable {
87 read100KFilesImpl(get100KFileFileGeneratedByWinCF());
88 }
89
90 @Test public void read100KFilesGeneratedByJava7JarUsingInputStream()
91 throws Throwable {
92 read100KFilesImpl(get100KFileFileGeneratedByJava7Jar());
93 }
94
95 @Test public void read100KFilesGeneratedByWinZIPUsingInputStream()
96 throws Throwable {
97 read100KFilesImpl(get100KFileFileGeneratedByWinZIP());
98 }
99
100 @Test public void read100KFilesGeneratedByPKZipUsingInputStream()
101 throws Throwable {
102 read100KFilesImpl(get100KFileFileGeneratedByPKZip());
103 }
104
105 @Test public void read5GBOfZerosUsingZipFile() throws Throwable {
106 read5GBOfZerosUsingZipFileImpl(get5GBZerosFile(), "5GB_of_Zeros");
107 }
108
109 @Test public void read5GBOfZerosGeneratedBy7ZIPUsingZipFile()
110 throws Throwable {
111 read5GBOfZerosUsingZipFileImpl(get5GBZerosFileGeneratedBy7ZIP(),
112 "5GB_of_Zeros");
113 }
114
115 @Test public void read5GBOfZerosGeneratedByJava7JarUsingZipFile()
116 throws Throwable {
117 read5GBOfZerosUsingZipFileImpl(get5GBZerosFileGeneratedByJava7Jar(),
118 "5GB_of_Zeros");
119 }
120
121 @Test public void read5GBOfZerosGeneratedByWinZIPUsingZipFile()
122 throws Throwable {
123 read5GBOfZerosUsingZipFileImpl(get5GBZerosFileGeneratedByWinZIP(),
124 "5GB_of_Zeros");
125 }
126
127 @Test public void read5GBOfZerosGeneratedByPKZipUsingZipFile()
128 throws Throwable {
129 read5GBOfZerosUsingZipFileImpl(get5GBZerosFileGeneratedByPKZip(),
130 "zip6/5GB_of_Zeros");
131 }
132
133 @Test public void read100KFilesUsingZipFile() throws Throwable {
134 read100KFilesUsingZipFileImpl(get100KFileFile());
135 }
136
137 @Test public void read100KFilesGeneratedBy7ZIPUsingZipFile()
138 throws Throwable {
139 read100KFilesUsingZipFileImpl(get100KFileFileGeneratedBy7ZIP());
140 }
141
142 @Test public void read100KFilesGeneratedByWinCFUsingZipFile()
143 throws Throwable {
144 read100KFilesUsingZipFileImpl(get100KFileFileGeneratedByWinCF());
145 }
146
147 @Test public void read100KFilesGeneratedByJava7JarUsingZipFile()
148 throws Throwable {
149 read100KFilesUsingZipFileImpl(get100KFileFileGeneratedByJava7Jar());
150 }
151
152 @Test public void read100KFilesGeneratedByWinZIPUsingZipFile()
153 throws Throwable {
154 read100KFilesUsingZipFileImpl(get100KFileFileGeneratedByWinZIP());
155 }
156
157 @Test public void read100KFilesGeneratedByPKZipUsingZipFile()
158 throws Throwable {
159 read100KFilesUsingZipFileImpl(get100KFileFileGeneratedByPKZip());
160 }
161
162 private static ZipOutputTest write100KFiles() {
163 return write100KFiles(Zip64Mode.AsNeeded);
164 }
165
166 private static ZipOutputTest write100KFiles(final Zip64Mode mode) {
167 return new ZipOutputTest() {
168 public void test(File f, ZipArchiveOutputStream zos)
169 throws IOException {
170 if (mode != Zip64Mode.AsNeeded) {
171 zos.setUseZip64(mode);
172 }
173 write100KFilesToStream(zos);
174 RandomAccessFile a = new RandomAccessFile(f, "r");
175 try {
176 final long end = a.length();
177
178
179
180
181 a.seek(end
182 - 22
183 byte[] eocd = new byte[12];
184 a.readFully(eocd);
185 assertArrayEquals(new byte[] {
186
187 (byte) 0x50, (byte) 0x4b, 5, 6,
188
189 0, 0, 0, 0,
190
191 (byte) 0xff, (byte) 0xff,
192 (byte) 0xff, (byte) 0xff,
193 }, eocd);
194
195
196
197
198
199 long expectedZ64EocdOffset = end - 22
200 - 20
201 - 56
202 byte[] loc =
203 ZipEightByteInteger.getBytes(expectedZ64EocdOffset);
204 a.seek(end - 22 - 20);
205 byte[] z64EocdLoc = new byte[20];
206 a.readFully(z64EocdLoc);
207 assertArrayEquals(new byte[] {
208
209 (byte) 0x50, (byte) 0x4b, 6, 7,
210
211 0, 0, 0, 0,
212
213 loc[0], loc[1], loc[2], loc[3],
214 loc[4], loc[5], loc[6], loc[7],
215
216 1, 0, 0, 0,
217 }, z64EocdLoc);
218
219
220
221
222
223 a.seek(expectedZ64EocdOffset);
224 byte[] z64EocdStart = new byte[40];
225 a.readFully(z64EocdStart);
226 assertArrayEquals(new byte[] {
227
228 (byte) 0x50, (byte) 0x4b, 6, 6,
229
230 44, 0, 0, 0,
231 0, 0, 0, 0,
232
233 45, 0,
234
235 45, 0,
236
237 0, 0, 0, 0,
238 0, 0, 0, 0,
239
240 (byte) 0xA0, (byte) 0x86, 1, 0,
241 0, 0, 0, 0,
242 (byte) 0xA0, (byte) 0x86, 1, 0,
243 0, 0, 0, 0,
244 }, z64EocdStart);
245 a.seek(expectedZ64EocdOffset + 48
246 byte[] cdOffset = new byte[8];
247 a.readFully(cdOffset);
248 long cdLoc = ZipEightByteInteger.getLongValue(cdOffset);
249
250
251
252 a.seek(cdLoc);
253 byte[] sig = new byte[4];
254 a.readFully(sig);
255 assertArrayEquals(new byte[] {
256 (byte) 0x50, (byte) 0x4b, 1, 2,
257 }, sig);
258 } finally {
259 a.close();
260 }
261 }
262 };
263 }
264
265 @Test public void write100KFilesFile() throws Throwable {
266 withTemporaryArchive("write100KFilesFile", write100KFiles(), true);
267 }
268
269 @Test public void write100KFilesStream() throws Throwable {
270 withTemporaryArchive("write100KFilesStream", write100KFiles(), false);
271 }
272
273 @Test public void write100KFilesFileModeAlways() throws Throwable {
274 withTemporaryArchive("write100KFilesFileModeAlways",
275 write100KFiles(Zip64Mode.Always), true);
276 }
277
278 @Test public void write100KFilesStreamModeAlways() throws Throwable {
279 withTemporaryArchive("write100KFilesStreamModeAlways",
280 write100KFiles(Zip64Mode.Always), false);
281 }
282
283 private static final ZipOutputTest write100KFilesModeNever =
284 new ZipOutputTest() {
285 public void test(File f, ZipArchiveOutputStream zos)
286 throws IOException {
287 zos.setUseZip64(Zip64Mode.Never);
288 try {
289 write100KFilesToStream(zos);
290 fail("expected a Zip64RequiredException");
291 } catch (Zip64RequiredException ex) {
292 assertEquals(Zip64RequiredException.TOO_MANY_ENTRIES_MESSAGE,
293 ex.getMessage());
294 }
295 }
296 };
297
298 @Test public void write100KFilesFileModeNever() throws Throwable {
299 withTemporaryArchive("write100KFilesFileModeNever",
300 write100KFilesModeNever, true);
301 }
302
303 @Test public void write100KFilesStreamModeNever() throws Throwable {
304 withTemporaryArchive("write100KFilesStreamModeNever",
305 write100KFilesModeNever, false);
306 }
307
308 @Test public void readSelfGenerated100KFilesUsingZipFile()
309 throws Throwable {
310 withTemporaryArchive("readSelfGenerated100KFilesUsingZipFile()",
311 new ZipOutputTest() {
312 public void test(File f,
313 ZipArchiveOutputStream zos)
314 throws IOException {
315 write100KFilesToStream(zos);
316 read100KFilesUsingZipFileImpl(f);
317 }
318 },
319 true);
320 }
321
322 private static ZipOutputTest write3EntriesCreatingBigArchive() {
323 return write3EntriesCreatingBigArchive(Zip64Mode.AsNeeded);
324 }
325
326
327
328
329
330
331
332
333 private static ZipOutputTest
334 write3EntriesCreatingBigArchive(final Zip64Mode mode) {
335 return new ZipOutputTest() {
336 public void test(File f, ZipArchiveOutputStream zos)
337 throws IOException {
338 if (mode != Zip64Mode.AsNeeded) {
339 zos.setUseZip64(mode);
340 }
341 write3EntriesCreatingBigArchiveToStream(zos);
342
343 RandomAccessFile a = new RandomAccessFile(f, "r");
344 try {
345 getLengthAndPositionAtCentralDirectory(a);
346
347 a.skipBytes(2 * 47
348
349
350 + 2 * (mode == Zip64Mode.Always ? 4 : 0)
351
352 );
353
354
355
356
357 byte[] header = new byte[12];
358 a.readFully(header);
359 assertArrayEquals(new byte[] {
360
361 (byte) 0x50, (byte) 0x4b, 1, 2,
362
363 45, 0,
364
365 45, 0,
366
367 0, 8,
368
369 0, 0
370 }, header);
371
372 a.skipBytes(12);
373 byte[] rest = new byte[23];
374 a.readFully(rest);
375 assertArrayEquals(new byte[] {
376
377 1, 0, 0, 0,
378
379 1, 0,
380
381 12, 0,
382
383 0, 0,
384
385 0, 0,
386
387 0, 0,
388 0, 0, 0, 0,
389
390 (byte) 0xFF, (byte) 0xFF,
391 (byte) 0xFF, (byte) 0xFF,
392
393 (byte) '2'
394 }, rest);
395 byte[] extra = new byte[4];
396 a.readFully(extra);
397 assertArrayEquals(new byte[] {
398
399 1, 0,
400
401 8, 0
402 }, extra);
403
404
405 byte[] offset = new byte[8];
406 a.readFully(offset);
407
408 a.seek(ZipEightByteInteger.getLongValue(offset));
409 byte[] sig = new byte[4];
410 a.readFully(sig);
411 assertArrayEquals(new byte[] {
412 (byte) 0x50, (byte) 0x4b, 3, 4,
413 }, sig);
414 } finally {
415 a.close();
416 }
417 }
418 };
419 }
420
421 @Test public void write3EntriesCreatingBigArchiveFile() throws Throwable {
422 withTemporaryArchive("write3EntriesCreatingBigArchiveFile",
423 write3EntriesCreatingBigArchive(),
424 true);
425 }
426
427 @Test public void write3EntriesCreatingBigArchiveStream() throws Throwable {
428 withTemporaryArchive("write3EntriesCreatingBigArchiveStream",
429 write3EntriesCreatingBigArchive(),
430 false);
431 }
432
433 @Test public void write3EntriesCreatingBigArchiveFileModeAlways()
434 throws Throwable {
435 withTemporaryArchive("write3EntriesCreatingBigArchiveFileModeAlways",
436 write3EntriesCreatingBigArchive(Zip64Mode.Always),
437 true);
438 }
439
440 @Test public void write3EntriesCreatingBigArchiveStreamModeAlways()
441 throws Throwable {
442 withTemporaryArchive("write3EntriesCreatingBigArchiveStreamModeAlways",
443 write3EntriesCreatingBigArchive(Zip64Mode.Always),
444 false);
445 }
446
447 private static final ZipOutputTest write3EntriesCreatingBigArchiveModeNever =
448 new ZipOutputTest() {
449 public void test(File f, ZipArchiveOutputStream zos)
450 throws IOException {
451 zos.setUseZip64(Zip64Mode.Never);
452 try {
453 write3EntriesCreatingBigArchiveToStream(zos);
454 fail("expected a Zip64RequiredException");
455 } catch (Zip64RequiredException ex) {
456 assertEquals(Zip64RequiredException.ARCHIVE_TOO_BIG_MESSAGE,
457 ex.getMessage());
458 }
459 }
460 };
461
462 @Test public void write3EntriesCreatingBigArchiveFileModeNever()
463 throws Throwable {
464 withTemporaryArchive("write3EntriesCreatingBigArchiveFileModeNever",
465 write3EntriesCreatingBigArchiveModeNever,
466 true);
467 }
468
469 @Test public void write3EntriesCreatingBigArchiveStreamModeNever()
470 throws Throwable {
471 withTemporaryArchive("write3EntriesCreatingBigArchiveStreamModeNever",
472 write3EntriesCreatingBigArchiveModeNever,
473 false);
474 }
475
476 @Test public void read3EntriesCreatingBigArchiveFileUsingZipFile()
477 throws Throwable {
478 withTemporaryArchive("read3EntriesCreatingBigArchiveFileUsingZipFile",
479 new ZipOutputTest() {
480 public void test(File f,
481 ZipArchiveOutputStream zos)
482 throws IOException {
483 write3EntriesCreatingBigArchiveToStream(zos);
484 ZipFile zf = null;
485 try {
486 zf = new ZipFile(f);
487 int idx = 0;
488 for (Enumeration<ZipArchiveEntry> e =
489 zf.getEntriesInPhysicalOrder();
490 e.hasMoreElements(); ) {
491 ZipArchiveEntry zae = e.nextElement();
492 assertEquals(String.valueOf(idx),
493 zae.getName());
494 if (idx++ < 2) {
495 assertEquals(FIVE_BILLION / 2,
496 zae.getSize());
497 } else {
498 assertEquals(1,
499 zae.getSize());
500 InputStream i =
501 zf.getInputStream(zae);
502 try {
503 assertNotNull(i);
504 assertEquals(42, i.read());
505 } finally {
506 i.close();
507 }
508 }
509 }
510 } finally {
511 ZipFile.closeQuietly(zf);
512 }
513 }
514 },
515 true);
516 }
517
518 private static ZipOutputTest writeBigStoredEntry(final boolean knownSize) {
519 return writeBigStoredEntry(knownSize, Zip64Mode.AsNeeded);
520 }
521
522
523
524
525
526
527
528
529
530
531 private static ZipOutputTest writeBigStoredEntry(final boolean knownSize,
532 final Zip64Mode mode) {
533 return new ZipOutputTest() {
534 public void test(File f, ZipArchiveOutputStream zos)
535 throws IOException {
536 if (mode != Zip64Mode.AsNeeded) {
537 zos.setUseZip64(mode);
538 }
539 byte[] buf = new byte[ONE_MILLION];
540 ZipArchiveEntry zae = new ZipArchiveEntry("0");
541 if (knownSize) {
542 zae.setSize(FIVE_BILLION);
543 zae.setCrc(0x5c316f50L);
544 }
545 zae.setMethod(ZipEntry.STORED);
546 zos.putArchiveEntry(zae);
547 for (int j = 0; j < FIVE_BILLION / 1000 / 1000; j++) {
548 zos.write(buf);
549 }
550 zos.closeArchiveEntry();
551 zos.close();
552
553 RandomAccessFile a = new RandomAccessFile(f, "r");
554 try {
555 getLengthAndPositionAtCentralDirectory(a);
556
557
558
559
560 byte[] header = new byte[12];
561 a.readFully(header);
562 assertArrayEquals(new byte[] {
563
564 (byte) 0x50, (byte) 0x4b, 1, 2,
565
566 45, 0,
567
568 45, 0,
569
570 0, 8,
571
572 0, 0
573 }, header);
574
575 a.skipBytes(4);
576 byte[] rest = new byte[31];
577 a.readFully(rest);
578 assertArrayEquals(new byte[] {
579
580 (byte) 0x50, (byte) 0x6F, (byte) 0x31, (byte) 0x5c,
581
582 (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
583
584 (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
585
586 1, 0,
587
588 20, 0,
589
590 0, 0,
591
592 0, 0,
593
594 0, 0,
595 0, 0, 0, 0,
596
597 0, 0, 0, 0,
598
599 (byte) '0'
600 }, rest);
601 byte[] extra = new byte[20];
602 a.readFully(extra);
603
604 assertArrayEquals(new byte[] {
605
606 1, 0,
607
608 16, 0,
609
610 0, (byte) 0xF2, 5, (byte) 0x2A,
611 1, 0, 0, 0,
612
613 0, (byte) 0xF2, 5, (byte) 0x2A,
614 1, 0, 0, 0,
615 }, extra);
616
617
618 a.seek(0);
619 header = new byte[10];
620 a.readFully(header);
621 assertArrayEquals(new byte[] {
622
623 (byte) 0x50, (byte) 0x4b, 3, 4,
624
625 45, 0,
626
627 0, 8,
628
629 0, 0
630 }, header);
631
632 a.skipBytes(4);
633 rest = new byte[17];
634 a.readFully(rest);
635 assertArrayEquals(new byte[] {
636
637 (byte) 0x50, (byte) 0x6F, (byte) 0x31, (byte) 0x5c,
638
639 (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
640
641 (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
642
643 1, 0,
644
645 20, 0,
646
647 (byte) '0'
648 }, rest);
649 a.readFully(extra);
650
651 assertArrayEquals(new byte[] {
652
653 1, 0,
654
655 16, 0,
656
657 0, (byte) 0xF2, 5, (byte) 0x2A,
658 1, 0, 0, 0,
659
660 0, (byte) 0xF2, 5, (byte) 0x2A,
661 1, 0, 0, 0,
662 }, extra);
663 } finally {
664 a.close();
665 }
666 }
667 };
668 }
669
670
671
672
673
674 @Test public void writeBigStoredEntryToStream() throws Throwable {
675 withTemporaryArchive("writeBigStoredEntryToStream",
676 writeBigStoredEntry(true),
677 false);
678 }
679
680 @Test public void writeBigStoredEntryKnownSizeToFile() throws Throwable {
681 withTemporaryArchive("writeBigStoredEntryKnownSizeToFile",
682 writeBigStoredEntry(true),
683 true);
684 }
685
686 @Test public void writeBigStoredEntryUnnownSizeToFile() throws Throwable {
687 withTemporaryArchive("writeBigStoredEntryUnknownSizeToFile",
688 writeBigStoredEntry(false),
689 true);
690 }
691
692 @Test public void writeBigStoredEntryToStreamModeAlways() throws Throwable {
693 withTemporaryArchive("writeBigStoredEntryToStreamModeAlways",
694 writeBigStoredEntry(true, Zip64Mode.Always),
695 false);
696 }
697
698 @Test public void writeBigStoredEntryKnownSizeToFileModeAlways()
699 throws Throwable {
700 withTemporaryArchive("writeBigStoredEntryKnownSizeToFileModeAlways",
701 writeBigStoredEntry(true, Zip64Mode.Always),
702 true);
703 }
704
705 @Test public void writeBigStoredEntryUnnownSizeToFileModeAlways()
706 throws Throwable {
707 withTemporaryArchive("writeBigStoredEntryUnknownSizeToFileModeAlways",
708 writeBigStoredEntry(false, Zip64Mode.Always),
709 true);
710 }
711
712 private static ZipOutputTest
713 writeBigStoredEntryModeNever(final boolean knownSize) {
714 return new ZipOutputTest() {
715 public void test(File f, ZipArchiveOutputStream zos)
716 throws IOException {
717 zos.setUseZip64(Zip64Mode.Never);
718 try {
719 byte[] buf = new byte[ONE_MILLION];
720 ZipArchiveEntry zae = new ZipArchiveEntry("0");
721 if (knownSize) {
722 zae.setSize(FIVE_BILLION);
723 zae.setCrc(0x5c316f50L);
724 }
725 zae.setMethod(ZipEntry.STORED);
726 zos.putArchiveEntry(zae);
727 for (int j = 0; j < FIVE_BILLION / 1000 / 1000; j++) {
728 zos.write(buf);
729 }
730 zos.closeArchiveEntry();
731 fail("expected a Zip64RequiredException");
732 } catch (Zip64RequiredException ex) {
733 assertTrue(ex.getMessage().startsWith("0's size"));
734 }
735 }
736 };
737 }
738
739 @Test public void writeBigStoredEntryToStreamModeNever() throws Throwable {
740 withTemporaryArchive("writeBigStoredEntryToStreamModeNever",
741 writeBigStoredEntryModeNever(true),
742 false);
743 }
744
745 @Test public void writeBigStoredEntryKnownSizeToFileModeNever()
746 throws Throwable {
747 withTemporaryArchive("writeBigStoredEntryKnownSizeToFileModeNever",
748 writeBigStoredEntryModeNever(true),
749 true);
750 }
751
752 @Test public void writeBigStoredEntryUnnownSizeToFileModeNever()
753 throws Throwable {
754 withTemporaryArchive("writeBigStoredEntryUnknownSizeToFileModeNever",
755 writeBigStoredEntryModeNever(false),
756 true);
757 }
758
759
760
761
762
763
764
765
766
767
768 private static ZipOutputTest
769 writeBigDeflatedEntryToStream(final boolean knownSize,
770 final Zip64Mode mode) {
771 return new ZipOutputTest() {
772 public void test(File f,
773 ZipArchiveOutputStream zos)
774 throws IOException {
775 if (mode != Zip64Mode.AsNeeded) {
776 zos.setUseZip64(mode);
777 }
778 byte[] buf = new byte[ONE_MILLION];
779 ZipArchiveEntry zae = new ZipArchiveEntry("0");
780 if (knownSize) {
781 zae.setSize(FIVE_BILLION);
782 }
783 zae.setMethod(ZipEntry.DEFLATED);
784 zos.putArchiveEntry(zae);
785 for (int j = 0; j < FIVE_BILLION / 1000 / 1000; j++) {
786 zos.write(buf);
787 }
788 zos.closeArchiveEntry();
789 zos.close();
790
791 RandomAccessFile a =
792 new RandomAccessFile(f, "r");
793 try {
794 getLengthAndPositionAtCentralDirectory(a);
795
796 long cfhPos = a.getFilePointer();
797
798
799
800
801 byte[] header = new byte[12];
802 a.readFully(header);
803 assertArrayEquals(new byte[] {
804
805 (byte) 0x50, (byte) 0x4b, 1, 2,
806
807 45, 0,
808
809 45, 0,
810
811 8, 8,
812
813 8, 0,
814 }, header);
815
816 a.skipBytes(4);
817 byte[] rest = new byte[31];
818 a.readFully(rest);
819 assertArrayEquals(new byte[] {
820
821 (byte) 0x50, (byte) 0x6F, (byte) 0x31, (byte) 0x5c,
822
823 (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
824
825 (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
826
827 1, 0,
828
829 20, 0,
830
831 0, 0,
832
833 0, 0,
834
835 0, 0,
836 0, 0, 0, 0,
837
838 0, 0, 0, 0,
839
840 (byte) '0'
841 }, rest);
842 byte[] extra = new byte[20];
843 a.readFully(extra);
844
845 assertArrayEquals(new byte[] {
846
847 1, 0,
848
849 16, 0,
850
851 0, (byte) 0xF2, 5, (byte) 0x2A,
852 1, 0, 0, 0,
853
854 (byte) 0x68, (byte) 0x27, (byte) 0x4A, 0,
855 0, 0, 0, 0,
856 }, extra);
857
858
859 a.seek(cfhPos - 24);
860 byte[] dd = new byte[8];
861 a.readFully(dd);
862 assertArrayEquals(new byte[] {
863
864 (byte) 0x50, (byte) 0x4b, 7, 8,
865
866 (byte) 0x50, (byte) 0x6F, (byte) 0x31, (byte) 0x5c,
867 }, dd);
868 dd = new byte[16];
869 a.readFully(dd);
870 assertArrayEquals(new byte[] {
871
872 (byte) 0x68, (byte) 0x27, (byte) 0x4A, 0,
873 0, 0, 0, 0,
874
875 0, (byte) 0xF2, 5, (byte) 0x2A,
876 1, 0, 0, 0,
877 }, dd);
878
879
880 a.seek(0);
881 header = new byte[10];
882 a.readFully(header);
883 assertArrayEquals(new byte[] {
884
885 (byte) 0x50, (byte) 0x4b, 3, 4,
886
887 45, 0,
888
889 8, 8,
890
891 8, 0,
892 }, header);
893
894 a.skipBytes(4);
895 rest = new byte[17];
896 a.readFully(rest);
897 assertArrayEquals(new byte[] {
898
899 0, 0, 0, 0,
900
901 (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
902
903 (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
904
905 1, 0,
906
907 20, 0,
908
909 (byte) '0'
910 }, rest);
911 a.readFully(extra);
912 assertArrayEquals(new byte[] {
913
914 1, 0,
915
916 16, 0,
917
918 0, 0, 0, 0,
919 0, 0, 0, 0,
920
921 0, 0, 0, 0,
922 0, 0, 0, 0,
923 }, extra);
924 } finally {
925 a.close();
926 }
927 }
928 };
929 }
930
931 @Test public void writeBigDeflatedEntryKnownSizeToStream()
932 throws Throwable {
933 withTemporaryArchive("writeBigDeflatedEntryKnownSizeToStream",
934 writeBigDeflatedEntryToStream(true,
935 Zip64Mode.AsNeeded),
936 false);
937 }
938
939 @Test public void writeBigDeflatedEntryKnownSizeToStreamModeAlways()
940 throws Throwable {
941 withTemporaryArchive("writeBigDeflatedEntryKnownSizeToStreamModeAlways",
942 writeBigDeflatedEntryToStream(true,
943 Zip64Mode.Always),
944 false);
945 }
946
947 @Test public void writeBigDeflatedEntryUnknownSizeToStreamModeAlways()
948 throws Throwable {
949 withTemporaryArchive("writeBigDeflatedEntryUnknownSizeToStreamModeAlways",
950 writeBigDeflatedEntryToStream(false,
951 Zip64Mode.Always),
952 false);
953 }
954
955 private static ZipOutputTest
956 writeBigDeflatedEntryUnknownSizeToStream(final Zip64Mode mode) {
957 return new ZipOutputTest() {
958 public void test(File f, ZipArchiveOutputStream zos)
959 throws IOException {
960 try {
961 if (mode != Zip64Mode.AsNeeded) {
962 zos.setUseZip64(mode);
963 }
964 byte[] buf = new byte[ONE_MILLION];
965 ZipArchiveEntry zae = new ZipArchiveEntry("0");
966 zae.setMethod(ZipEntry.DEFLATED);
967 zos.putArchiveEntry(zae);
968 for (int j = 0; j < FIVE_BILLION / 1000 / 1000; j++) {
969 zos.write(buf);
970 }
971 zos.closeArchiveEntry();
972 fail("expected a Zip64RequiredException");
973 } catch (Zip64RequiredException ex) {
974 assertTrue(ex.getMessage().startsWith("0's size"));
975 }
976 }
977 };
978 }
979
980 @Test public void writeBigDeflatedEntryUnknownSizeToStream()
981 throws Throwable {
982 withTemporaryArchive("writeBigDeflatedEntryUnknownSizeToStream",
983 writeBigDeflatedEntryUnknownSizeToStream(Zip64Mode
984 .AsNeeded),
985 false);
986 }
987
988 @Test public void writeBigDeflatedEntryUnknownSizeToStreamModeNever()
989 throws Throwable {
990 withTemporaryArchive("writeBigDeflatedEntryUnknownSizeToStreamModeNever",
991 writeBigDeflatedEntryUnknownSizeToStream(Zip64Mode
992 .Never),
993 false);
994 }
995
996 private static ZipOutputTest
997 writeBigDeflatedEntryToFile(final boolean knownSize) {
998 return writeBigDeflatedEntryToFile(knownSize, Zip64Mode.AsNeeded);
999 }
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010 private static ZipOutputTest
1011 writeBigDeflatedEntryToFile(final boolean knownSize,
1012 final Zip64Mode mode) {
1013 return new ZipOutputTest() {
1014 public void test(File f, ZipArchiveOutputStream zos)
1015 throws IOException {
1016 if (mode != Zip64Mode.AsNeeded) {
1017 zos.setUseZip64(mode);
1018 }
1019 byte[] buf = new byte[ONE_MILLION];
1020 ZipArchiveEntry zae = new ZipArchiveEntry("0");
1021 if (knownSize) {
1022 zae.setSize(FIVE_BILLION);
1023 }
1024 zae.setMethod(ZipEntry.DEFLATED);
1025 zos.putArchiveEntry(zae);
1026 for (int j = 0;
1027 j < FIVE_BILLION / 1000 / 1000;
1028 j++) {
1029 zos.write(buf);
1030 }
1031 zos.closeArchiveEntry();
1032 zos.close();
1033
1034 RandomAccessFile a = new RandomAccessFile(f, "r");
1035 try {
1036 getLengthAndPositionAtCentralDirectory(a);
1037
1038
1039
1040
1041
1042 byte[] header = new byte[12];
1043 a.readFully(header);
1044 assertArrayEquals(new byte[] {
1045
1046 (byte) 0x50, (byte) 0x4b, 1, 2,
1047
1048 45, 0,
1049
1050 45, 0,
1051
1052 0, 8,
1053
1054 8, 0,
1055 }, header);
1056
1057 a.skipBytes(4);
1058 byte[] rest = new byte[31];
1059 a.readFully(rest);
1060 assertArrayEquals(new byte[] {
1061
1062 (byte) 0x50, (byte) 0x6F, (byte) 0x31, (byte) 0x5c,
1063
1064 (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
1065
1066 (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
1067
1068 1, 0,
1069
1070 20, 0,
1071
1072 0, 0,
1073
1074 0, 0,
1075
1076 0, 0,
1077 0, 0, 0, 0,
1078
1079 0, 0, 0, 0,
1080
1081 (byte) '0'
1082 }, rest);
1083 byte[] extra = new byte[20];
1084 a.readFully(extra);
1085
1086 assertArrayEquals(new byte[] {
1087
1088 1, 0,
1089
1090 16, 0,
1091
1092 0, (byte) 0xF2, 5, (byte) 0x2A,
1093 1, 0, 0, 0,
1094
1095 (byte) 0x68, (byte) 0x27, (byte) 0x4A, 0,
1096 0, 0, 0, 0,
1097 }, extra);
1098
1099
1100 a.seek(0);
1101 header = new byte[10];
1102 a.readFully(header);
1103 assertArrayEquals(new byte[] {
1104
1105 (byte) 0x50, (byte) 0x4b, 3, 4,
1106
1107 45, 0,
1108
1109 0, 8,
1110
1111 8, 0,
1112 }, header);
1113
1114 a.skipBytes(4);
1115 rest = new byte[17];
1116 a.readFully(rest);
1117 assertArrayEquals(new byte[] {
1118
1119 (byte) 0x50, (byte) 0x6F, (byte) 0x31, (byte) 0x5c,
1120
1121 (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
1122
1123 (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
1124
1125 1, 0,
1126
1127 20, 0,
1128
1129 (byte) '0'
1130 }, rest);
1131 extra = new byte[20];
1132 a.readFully(extra);
1133 assertArrayEquals(new byte[] {
1134
1135 1, 0,
1136
1137 16, 0,
1138
1139 0, (byte) 0xF2, 5, (byte) 0x2A,
1140 1, 0, 0, 0,
1141
1142 (byte) 0x68, (byte) 0x27, (byte) 0x4A, 0,
1143 0, 0, 0, 0,
1144 }, extra);
1145 } finally {
1146 a.close();
1147 }
1148 }
1149 };
1150 }
1151
1152 @Test public void writeBigDeflatedEntryKnownSizeToFile()
1153 throws Throwable {
1154 withTemporaryArchive("writeBigDeflatedEntryKnownSizeToFile",
1155 writeBigDeflatedEntryToFile(true),
1156 true);
1157 }
1158
1159 @Test public void writeBigDeflatedEntryUnknownSizeToFile()
1160 throws Throwable {
1161 withTemporaryArchive("writeBigDeflatedEntryUnknownSizeToFile",
1162 writeBigDeflatedEntryToFile(false),
1163 true);
1164 }
1165
1166 @Test public void writeBigDeflatedEntryKnownSizeToFileModeAlways()
1167 throws Throwable {
1168 withTemporaryArchive("writeBigDeflatedEntryKnownSizeToFileModeAlways",
1169 writeBigDeflatedEntryToFile(true, Zip64Mode.Always),
1170 true);
1171 }
1172
1173 @Test public void writeBigDeflatedEntryUnknownSizeToFileModeAlways()
1174 throws Throwable {
1175 withTemporaryArchive("writeBigDeflatedEntryUnknownSizeToFileModeAlways",
1176 writeBigDeflatedEntryToFile(false,
1177 Zip64Mode.Always),
1178 true);
1179 }
1180
1181 @Test public void writeBigDeflatedEntryKnownSizeToStreamModeNever()
1182 throws Throwable {
1183 withTemporaryArchive("writeBigDeflatedEntryKnownSizeToStreamModeNever",
1184 new ZipOutputTest() {
1185 public void test(File f,
1186 ZipArchiveOutputStream zos)
1187 throws IOException {
1188 zos.setUseZip64(Zip64Mode.Never);
1189 try {
1190 ZipArchiveEntry zae =
1191 new ZipArchiveEntry("0");
1192 zae.setSize(FIVE_BILLION);
1193 zae.setMethod(ZipEntry.DEFLATED);
1194 zos.putArchiveEntry(zae);
1195 fail("expected a"
1196 + " Zip64RequiredException");
1197 } catch (Zip64RequiredException ex) {
1198 assertTrue(ex.getMessage()
1199 .startsWith("0's size"));
1200 }
1201 }
1202 },
1203 false);
1204 }
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215 private static ZipOutputTest
1216 writeBigDeflatedEntryToFileModeNever(final boolean knownSize) {
1217 return new ZipOutputTest() {
1218 public void test(File f, ZipArchiveOutputStream zos)
1219 throws IOException {
1220 zos.setUseZip64(Zip64Mode.Never);
1221 try {
1222 byte[] buf = new byte[ONE_MILLION];
1223 ZipArchiveEntry zae = new ZipArchiveEntry("0");
1224 if (knownSize) {
1225 zae.setSize(FIVE_BILLION);
1226 }
1227 zae.setMethod(ZipEntry.DEFLATED);
1228 zos.putArchiveEntry(zae);
1229 for (int j = 0;
1230 j < FIVE_BILLION / 1000 / 1000;
1231 j++) {
1232 zos.write(buf);
1233 }
1234 zos.closeArchiveEntry();
1235 fail("expected a Zip64RequiredException");
1236 } catch (Zip64RequiredException ex) {
1237 assertTrue(ex.getMessage().startsWith("0's size"));
1238 }
1239 }
1240 };
1241 }
1242
1243 @Test public void writeBigDeflatedEntryKnownSizeToFileModeNever()
1244 throws Throwable {
1245 withTemporaryArchive("writeBigDeflatedEntryKnownSizeToFileModeNever",
1246 writeBigDeflatedEntryToFileModeNever(true),
1247 true);
1248 }
1249
1250 @Test public void writeBigDeflatedEntryUnknownSizeToFileModeNever()
1251 throws Throwable {
1252 withTemporaryArchive("writeBigDeflatedEntryUnknownSizeToFileModeNever",
1253 writeBigDeflatedEntryToFileModeNever(false),
1254 true);
1255 }
1256
1257 private static ZipOutputTest writeSmallStoredEntry(final boolean knownSize) {
1258 return writeSmallStoredEntry(knownSize, Zip64Mode.AsNeeded);
1259 }
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270 private static ZipOutputTest writeSmallStoredEntry(final boolean knownSize,
1271 final Zip64Mode mode) {
1272 return new ZipOutputTest() {
1273 public void test(File f, ZipArchiveOutputStream zos)
1274 throws IOException {
1275 if (mode != Zip64Mode.AsNeeded) {
1276 zos.setUseZip64(mode);
1277 }
1278 byte[] buf = new byte[ONE_MILLION];
1279 ZipArchiveEntry zae = new ZipArchiveEntry("0");
1280 if (knownSize) {
1281 zae.setSize(ONE_MILLION);
1282 zae.setCrc(0x1279CB9EL);
1283 }
1284 zae.setMethod(ZipEntry.STORED);
1285 zos.putArchiveEntry(zae);
1286 zos.write(buf);
1287 zos.closeArchiveEntry();
1288 zos.close();
1289
1290 RandomAccessFile a = new RandomAccessFile(f, "r");
1291 try {
1292 getLengthAndPositionAtCentralDirectory(a);
1293
1294
1295
1296
1297 byte[] header = new byte[12];
1298 a.readFully(header);
1299 assertArrayEquals(new byte[] {
1300
1301 (byte) 0x50, (byte) 0x4b, 1, 2,
1302
1303 20, 0,
1304
1305 10, 0,
1306
1307 0, 8,
1308
1309 0, 0
1310 }, header);
1311
1312 a.skipBytes(4);
1313 byte[] rest = new byte[31];
1314 a.readFully(rest);
1315
1316 assertArrayEquals(new byte[] {
1317
1318 (byte) 0x9E, (byte) 0xCB, (byte) 0x79, (byte) 0x12,
1319
1320 (byte) 0x40, (byte) 0x42, (byte) 0x0F, 0,
1321
1322 (byte) 0x40, (byte) 0x42, (byte) 0x0F, 0,
1323
1324 1, 0,
1325
1326 0, 0,
1327
1328 0, 0,
1329
1330 0, 0,
1331
1332 0, 0,
1333 0, 0, 0, 0,
1334
1335 0, 0, 0, 0,
1336
1337 (byte) '0'
1338 }, rest);
1339
1340
1341
1342
1343
1344 boolean hasExtra = mode == Zip64Mode.Always
1345 || (mode == Zip64Mode.AsNeeded && !knownSize);
1346 a.seek(0);
1347 header = new byte[10];
1348 a.readFully(header);
1349 assertArrayEquals(new byte[] {
1350
1351 (byte) 0x50, (byte) 0x4b, 3, 4,
1352
1353 10, 0,
1354
1355 0, 8,
1356
1357 0, 0
1358 }, header);
1359
1360 a.skipBytes(4);
1361 rest = new byte[17];
1362 a.readFully(rest);
1363
1364 assertArrayEquals(new byte[] {
1365
1366 (byte) 0x9E, (byte) 0xCB, (byte) 0x79, (byte) 0x12,
1367
1368 (byte) 0x40, (byte) 0x42, (byte) 0x0F, 0,
1369
1370 (byte) 0x40, (byte) 0x42, (byte) 0x0F, 0,
1371
1372 1, 0,
1373
1374 (byte) (!hasExtra ? 0 : 20), 0,
1375
1376 (byte) '0'
1377 }, rest);
1378 if (hasExtra) {
1379 byte[] extra = new byte[20];
1380 a.readFully(extra);
1381 assertArrayEquals(new byte[] {
1382
1383 1, 0,
1384
1385 16, 0,
1386
1387 (byte) 0x40, (byte) 0x42, (byte) 0x0F, 0,
1388 0, 0, 0, 0,
1389
1390 (byte) 0x40, (byte) 0x42, (byte) 0x0F, 0,
1391 0, 0, 0, 0,
1392 }, extra);
1393 }
1394 } finally {
1395 a.close();
1396 }
1397 }
1398 };
1399 }
1400
1401 @Test public void writeSmallStoredEntryToStream() throws Throwable {
1402 withTemporaryArchive("writeSmallStoredEntryToStream",
1403 writeSmallStoredEntry(true),
1404 false);
1405 }
1406
1407 @Test public void writeSmallStoredEntryKnownSizeToFile() throws Throwable {
1408 withTemporaryArchive("writeSmallStoredEntryKnownSizeToFile",
1409 writeSmallStoredEntry(true),
1410 true);
1411 }
1412
1413 @Test public void writeSmallStoredEntryUnnownSizeToFile() throws Throwable {
1414 withTemporaryArchive("writeSmallStoredEntryUnknownSizeToFile",
1415 writeSmallStoredEntry(false),
1416 true);
1417 }
1418
1419 @Test public void writeSmallStoredEntryToStreamModeNever() throws Throwable {
1420 withTemporaryArchive("writeSmallStoredEntryToStreamModeNever",
1421 writeSmallStoredEntry(true, Zip64Mode.Never),
1422 false);
1423 }
1424
1425 @Test public void writeSmallStoredEntryKnownSizeToFileModeNever()
1426 throws Throwable {
1427 withTemporaryArchive("writeSmallStoredEntryKnownSizeToFileModeNever",
1428 writeSmallStoredEntry(true, Zip64Mode.Never),
1429 true);
1430 }
1431
1432 @Test public void writeSmallStoredEntryUnnownSizeToFileModeNever()
1433 throws Throwable {
1434 withTemporaryArchive("writeSmallStoredEntryUnknownSizeToFileModeNever",
1435 writeSmallStoredEntry(false, Zip64Mode.Never),
1436 true);
1437 }
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448 private static ZipOutputTest
1449 writeSmallStoredEntryModeAlways(final boolean knownSize) {
1450 return new ZipOutputTest() {
1451 public void test(File f, ZipArchiveOutputStream zos)
1452 throws IOException {
1453 zos.setUseZip64(Zip64Mode.Always);
1454 byte[] buf = new byte[ONE_MILLION];
1455 ZipArchiveEntry zae = new ZipArchiveEntry("0");
1456 if (knownSize) {
1457 zae.setSize(ONE_MILLION);
1458 zae.setCrc(0x1279CB9EL);
1459 }
1460 zae.setMethod(ZipEntry.STORED);
1461 zos.putArchiveEntry(zae);
1462 zos.write(buf);
1463 zos.closeArchiveEntry();
1464 zos.close();
1465
1466 RandomAccessFile a = new RandomAccessFile(f, "r");
1467 try {
1468 getLengthAndPositionAtCentralDirectory(a);
1469
1470
1471
1472 byte[] header = new byte[12];
1473 a.readFully(header);
1474 assertArrayEquals(new byte[] {
1475
1476 (byte) 0x50, (byte) 0x4b, 1, 2,
1477
1478 45, 0,
1479
1480 45, 0,
1481
1482 0, 8,
1483
1484 0, 0
1485 }, header);
1486
1487 a.skipBytes(4);
1488 byte[] rest = new byte[31];
1489 a.readFully(rest);
1490
1491 assertArrayEquals(new byte[] {
1492
1493 (byte) 0x9E, (byte) 0xCB, (byte) 0x79, (byte) 0x12,
1494
1495 (byte) 0x40, (byte) 0x42, (byte) 0x0F, 0,
1496
1497 (byte) 0x40, (byte) 0x42, (byte) 0x0F, 0,
1498
1499 1, 0,
1500
1501 4, 0,
1502
1503 0, 0,
1504
1505 0, 0,
1506
1507 0, 0,
1508 0, 0, 0, 0,
1509
1510 0, 0, 0, 0,
1511
1512 (byte) '0'
1513 }, rest);
1514
1515 byte[] extra = new byte[4];
1516 a.readFully(extra);
1517 assertArrayEquals(new byte[] {
1518
1519 1, 0,
1520
1521 0, 0,
1522 }, extra);
1523
1524
1525
1526
1527 a.seek(0);
1528 header = new byte[10];
1529 a.readFully(header);
1530 assertArrayEquals(new byte[] {
1531
1532 (byte) 0x50, (byte) 0x4b, 3, 4,
1533
1534 45, 0,
1535
1536 0, 8,
1537
1538 0, 0
1539 }, header);
1540
1541 a.skipBytes(4);
1542 rest = new byte[17];
1543 a.readFully(rest);
1544
1545 assertArrayEquals(new byte[] {
1546
1547 (byte) 0x9E, (byte) 0xCB, (byte) 0x79, (byte) 0x12,
1548
1549 (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
1550
1551 (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
1552
1553 1, 0,
1554
1555 20, 0,
1556
1557 (byte) '0'
1558 }, rest);
1559
1560 extra = new byte[20];
1561 a.readFully(extra);
1562 assertArrayEquals(new byte[] {
1563
1564 1, 0,
1565
1566 16, 0,
1567
1568 (byte) 0x40, (byte) 0x42, (byte) 0x0F, 0,
1569 0, 0, 0, 0,
1570
1571 (byte) 0x40, (byte) 0x42, (byte) 0x0F, 0,
1572 0, 0, 0, 0,
1573 }, extra);
1574 } finally {
1575 a.close();
1576 }
1577 }
1578 };
1579 }
1580
1581 @Test public void writeSmallStoredEntryToStreamModeAlways()
1582 throws Throwable {
1583 withTemporaryArchive("writeSmallStoredEntryToStreamModeAlways",
1584 writeSmallStoredEntryModeAlways(true),
1585 false);
1586 }
1587
1588 @Test public void writeSmallStoredEntryKnownSizeToFileModeAlways()
1589 throws Throwable {
1590 withTemporaryArchive("writeSmallStoredEntryKnownSizeToFileModeAlways",
1591 writeSmallStoredEntryModeAlways(true),
1592 true);
1593 }
1594
1595 @Test public void writeSmallStoredEntryUnnownSizeToFileModeAlways()
1596 throws Throwable {
1597 withTemporaryArchive("writeSmallStoredEntryUnknownSizeToFileModeAlways",
1598 writeSmallStoredEntryModeAlways(false),
1599 true);
1600 }
1601
1602
1603
1604
1605
1606
1607
1608
1609 private static ZipOutputTest
1610 writeSmallDeflatedEntryToStream(final boolean knownSize,
1611 final Zip64Mode mode) {
1612 return new ZipOutputTest() {
1613 public void test(File f, ZipArchiveOutputStream zos)
1614 throws IOException {
1615 if (mode != Zip64Mode.AsNeeded) {
1616 zos.setUseZip64(mode);
1617 }
1618 byte[] buf = new byte[ONE_MILLION];
1619 ZipArchiveEntry zae = new ZipArchiveEntry("0");
1620 if (knownSize) {
1621 zae.setSize(ONE_MILLION);
1622 }
1623 zae.setMethod(ZipEntry.DEFLATED);
1624 zos.putArchiveEntry(zae);
1625 zos.write(buf);
1626 zos.closeArchiveEntry();
1627 zos.close();
1628
1629 RandomAccessFile a = new RandomAccessFile(f, "r");
1630 try {
1631 getLengthAndPositionAtCentralDirectory(a);
1632
1633 long cfhPos = a.getFilePointer();
1634
1635
1636
1637 byte[] header = new byte[12];
1638 a.readFully(header);
1639 assertArrayEquals(new byte[] {
1640
1641 (byte) 0x50, (byte) 0x4b, 1, 2,
1642
1643 20, 0,
1644
1645 20, 0,
1646
1647 8, 8,
1648
1649 8, 0,
1650 }, header);
1651
1652 a.skipBytes(4);
1653 byte[] crc = new byte[4];
1654 a.readFully(crc);
1655 assertArrayEquals(new byte[] {
1656 (byte) 0x9E, (byte) 0xCB,
1657 (byte) 0x79, (byte) 0x12,
1658 }, crc);
1659
1660 a.skipBytes(4);
1661 byte[] rest = new byte[23];
1662 a.readFully(rest);
1663 assertArrayEquals(new byte[] {
1664
1665 (byte) 0x40, (byte) 0x42,
1666 (byte) 0x0F, 0,
1667
1668 1, 0,
1669
1670 0, 0,
1671
1672 0, 0,
1673
1674 0, 0,
1675
1676 0, 0,
1677 0, 0, 0, 0,
1678
1679 0, 0, 0, 0,
1680
1681 (byte) '0'
1682 }, rest);
1683
1684
1685 a.seek(cfhPos - 16);
1686 byte[] dd = new byte[8];
1687 a.readFully(dd);
1688 assertArrayEquals(new byte[] {
1689
1690 (byte) 0x50, (byte) 0x4b, 7, 8,
1691
1692 (byte) 0x9E, (byte) 0xCB, (byte) 0x79, (byte) 0x12,
1693 }, dd);
1694
1695 a.skipBytes(4);
1696 dd = new byte[4];
1697 a.readFully(dd);
1698 assertArrayEquals(new byte[] {
1699
1700 (byte) 0x40, (byte) 0x42, (byte) 0x0F, 0,
1701 }, dd);
1702
1703
1704 a.seek(0);
1705 header = new byte[10];
1706 a.readFully(header);
1707 assertArrayEquals(new byte[] {
1708
1709 (byte) 0x50, (byte) 0x4b, 3, 4,
1710
1711 20, 0,
1712
1713 8, 8,
1714
1715 8, 0,
1716 }, header);
1717
1718 a.skipBytes(4);
1719 rest = new byte[17];
1720 a.readFully(rest);
1721 assertArrayEquals(new byte[] {
1722
1723 0, 0, 0, 0,
1724
1725 0, 0, 0, 0,
1726
1727 0, 0, 0, 0,
1728
1729 1, 0,
1730
1731 0, 0,
1732
1733 (byte) '0'
1734 }, rest);
1735 } finally {
1736 a.close();
1737 }
1738 }
1739 };
1740
1741 }
1742
1743 @Test public void writeSmallDeflatedEntryKnownSizeToStream()
1744 throws Throwable {
1745 withTemporaryArchive("writeSmallDeflatedEntryKnownSizeToStream",
1746 writeSmallDeflatedEntryToStream(true,
1747 Zip64Mode.AsNeeded),
1748 false);
1749 }
1750
1751 @Test public void writeSmallDeflatedEntryKnownSizeToStreamModeNever()
1752 throws Throwable {
1753 withTemporaryArchive("writeSmallDeflatedEntryKnownSizeToStreamModeNever",
1754 writeSmallDeflatedEntryToStream(true,
1755 Zip64Mode.Never),
1756 false);
1757 }
1758
1759 @Test public void writeSmallDeflatedEntryUnknownSizeToStream()
1760 throws Throwable {
1761 withTemporaryArchive("writeSmallDeflatedEntryUnknownSizeToStream",
1762 writeSmallDeflatedEntryToStream(false,
1763 Zip64Mode.AsNeeded),
1764 false);
1765 }
1766
1767 @Test public void writeSmallDeflatedEntryUnknownSizeToStreamModeNever()
1768 throws Throwable {
1769 withTemporaryArchive("writeSmallDeflatedEntryUnknownSizeToStreamModeNever",
1770 writeSmallDeflatedEntryToStream(false,
1771 Zip64Mode.Never),
1772 false);
1773 }
1774
1775
1776
1777
1778
1779
1780
1781
1782 private static ZipOutputTest
1783 writeSmallDeflatedEntryToStreamModeAlways(final boolean knownSize) {
1784 return new ZipOutputTest() {
1785 public void test(File f, ZipArchiveOutputStream zos)
1786 throws IOException {
1787 zos.setUseZip64(Zip64Mode.Always);
1788 byte[] buf = new byte[ONE_MILLION];
1789 ZipArchiveEntry zae = new ZipArchiveEntry("0");
1790 if (knownSize) {
1791 zae.setSize(ONE_MILLION);
1792 }
1793 zae.setMethod(ZipEntry.DEFLATED);
1794 zos.putArchiveEntry(zae);
1795 zos.write(buf);
1796 zos.closeArchiveEntry();
1797 zos.close();
1798
1799 RandomAccessFile a = new RandomAccessFile(f, "r");
1800 try {
1801 getLengthAndPositionAtCentralDirectory(a);
1802
1803 long cfhPos = a.getFilePointer();
1804
1805
1806
1807 byte[] header = new byte[12];
1808 a.readFully(header);
1809 assertArrayEquals(new byte[] {
1810
1811 (byte) 0x50, (byte) 0x4b, 1, 2,
1812
1813 45, 0,
1814
1815 45, 0,
1816
1817 8, 8,
1818
1819 8, 0,
1820 }, header);
1821
1822 a.skipBytes(4);
1823 byte[] crc = new byte[4];
1824 a.readFully(crc);
1825 assertArrayEquals(new byte[] {
1826 (byte) 0x9E, (byte) 0xCB,
1827 (byte) 0x79, (byte) 0x12,
1828 }, crc);
1829
1830 a.skipBytes(4);
1831 byte[] rest = new byte[23];
1832 a.readFully(rest);
1833 assertArrayEquals(new byte[] {
1834
1835 (byte) 0x40, (byte) 0x42,
1836 (byte) 0x0F, 0,
1837
1838 1, 0,
1839
1840 4, 0,
1841
1842 0, 0,
1843
1844 0, 0,
1845
1846 0, 0,
1847 0, 0, 0, 0,
1848
1849 0, 0, 0, 0,
1850
1851 (byte) '0'
1852 }, rest);
1853 byte[] extra = new byte[4];
1854 a.readFully(extra);
1855 assertArrayEquals(new byte[] {
1856
1857 1, 0,
1858
1859 0, 0,
1860 }, extra);
1861
1862
1863 a.seek(cfhPos - 24);
1864 byte[] dd = new byte[8];
1865 a.readFully(dd);
1866 assertArrayEquals(new byte[] {
1867
1868 (byte) 0x50, (byte) 0x4b, 7, 8,
1869
1870 (byte) 0x9E, (byte) 0xCB, (byte) 0x79, (byte) 0x12,
1871 }, dd);
1872
1873 a.skipBytes(8);
1874 dd = new byte[8];
1875 a.readFully(dd);
1876 assertArrayEquals(new byte[] {
1877
1878 (byte) 0x40, (byte) 0x42, (byte) 0x0F, 0,
1879 0, 0, 0, 0
1880 }, dd);
1881
1882
1883 a.seek(0);
1884 header = new byte[10];
1885 a.readFully(header);
1886 assertArrayEquals(new byte[] {
1887
1888 (byte) 0x50, (byte) 0x4b, 3, 4,
1889
1890 45, 0,
1891
1892 8, 8,
1893
1894 8, 0,
1895 }, header);
1896
1897 a.skipBytes(4);
1898 rest = new byte[17];
1899 a.readFully(rest);
1900 assertArrayEquals(new byte[] {
1901
1902 0, 0, 0, 0,
1903
1904 (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
1905
1906 (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
1907
1908 1, 0,
1909
1910 20, 0,
1911
1912 (byte) '0'
1913 }, rest);
1914
1915 extra = new byte[20];
1916 a.readFully(extra);
1917 assertArrayEquals(new byte[] {
1918
1919 1, 0,
1920
1921 16, 0,
1922
1923 0, 0, 0, 0,
1924 0, 0, 0, 0,
1925
1926 0, 0, 0, 0,
1927 0, 0, 0, 0,
1928 }, extra);
1929 } finally {
1930 a.close();
1931 }
1932 }
1933 };
1934
1935 }
1936
1937 @Test public void writeSmallDeflatedEntryKnownSizeToStreamModeAlways()
1938 throws Throwable {
1939 withTemporaryArchive("writeSmallDeflatedEntryKnownSizeToStreamModeAlways",
1940 writeSmallDeflatedEntryToStreamModeAlways(true),
1941 false);
1942 }
1943
1944 @Test public void writeSmallDeflatedEntryUnknownSizeToStreamModeAlways()
1945 throws Throwable {
1946 withTemporaryArchive("writeSmallDeflatedEntryUnknownSizeToStreamModeAlways",
1947 writeSmallDeflatedEntryToStreamModeAlways(false),
1948 false);
1949 }
1950
1951 private static ZipOutputTest writeSmallDeflatedEntryToFile(final boolean knownSize) {
1952 return writeSmallDeflatedEntryToFile(knownSize, Zip64Mode.AsNeeded);
1953 }
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963 private static ZipOutputTest
1964 writeSmallDeflatedEntryToFile(final boolean knownSize,
1965 final Zip64Mode mode) {
1966 return new ZipOutputTest() {
1967 public void test(File f, ZipArchiveOutputStream zos)
1968 throws IOException {
1969 if (mode != Zip64Mode.AsNeeded) {
1970 zos.setUseZip64(mode);
1971 }
1972 byte[] buf = new byte[ONE_MILLION];
1973 ZipArchiveEntry zae = new ZipArchiveEntry("0");
1974 if (knownSize) {
1975 zae.setSize(ONE_MILLION);
1976 }
1977 zae.setMethod(ZipEntry.DEFLATED);
1978 zos.putArchiveEntry(zae);
1979 zos.write(buf);
1980 zos.closeArchiveEntry();
1981 zos.close();
1982
1983 RandomAccessFile a = new RandomAccessFile(f, "r");
1984 try {
1985 getLengthAndPositionAtCentralDirectory(a);
1986
1987
1988
1989
1990 byte[] header = new byte[12];
1991 a.readFully(header);
1992 assertArrayEquals(new byte[] {
1993
1994 (byte) 0x50, (byte) 0x4b, 1, 2,
1995
1996 20, 0,
1997
1998 10, 0,
1999
2000 0, 8,
2001
2002 8, 0,
2003 }, header);
2004
2005 a.skipBytes(4);
2006 byte[] crc = new byte[4];
2007 a.readFully(crc);
2008 assertArrayEquals(new byte[] {
2009 (byte) 0x9E, (byte) 0xCB,
2010 (byte) 0x79, (byte) 0x12,
2011 }, crc);
2012
2013 a.skipBytes(4);
2014 byte[] rest = new byte[23];
2015 a.readFully(rest);
2016 assertArrayEquals(new byte[] {
2017
2018 (byte) 0x40, (byte) 0x42, (byte) 0x0F, 0,
2019
2020 1, 0,
2021
2022 0, 0,
2023
2024 0, 0,
2025
2026 0, 0,
2027
2028 0, 0,
2029 0, 0, 0, 0,
2030
2031 0, 0, 0, 0,
2032
2033 (byte) '0'
2034 }, rest);
2035
2036
2037 a.seek(0);
2038 header = new byte[10];
2039 a.readFully(header);
2040 assertArrayEquals(new byte[] {
2041
2042 (byte) 0x50, (byte) 0x4b, 3, 4,
2043
2044 10, 0,
2045
2046 0, 8,
2047
2048 8, 0,
2049 }, header);
2050
2051 a.skipBytes(4);
2052 crc = new byte[4];
2053 a.readFully(crc);
2054 assertArrayEquals(new byte[] {
2055 (byte) 0x9E, (byte) 0xCB,
2056 (byte) 0x79, (byte) 0x12,
2057 }, crc);
2058
2059 a.skipBytes(4);
2060 rest = new byte[9];
2061 a.readFully(rest);
2062
2063 boolean hasExtra =
2064 mode == Zip64Mode.AsNeeded && !knownSize;
2065
2066 assertArrayEquals(new byte[] {
2067
2068 (byte) 0x40, (byte) 0x42, (byte) 0x0F, 0,
2069
2070 1, 0,
2071
2072 (byte) (!hasExtra ? 0 : 20), 0,
2073
2074 (byte) '0'
2075 }, rest);
2076 if (hasExtra) {
2077 byte[] extra = new byte[12];
2078 a.readFully(extra);
2079 assertArrayEquals(new byte[] {
2080
2081 1, 0,
2082
2083 16, 0,
2084
2085 (byte) 0x40, (byte) 0x42, (byte) 0x0F, 0,
2086 0, 0, 0, 0,
2087
2088
2089
2090
2091 }, extra);
2092 }
2093 } finally {
2094 a.close();
2095 }
2096 }
2097 };
2098 }
2099
2100 @Test public void writeSmallDeflatedEntryKnownSizeToFile()
2101 throws Throwable {
2102 withTemporaryArchive("writeSmallDeflatedEntryKnownSizeToFile",
2103 writeSmallDeflatedEntryToFile(true),
2104 true);
2105 }
2106
2107 @Test public void writeSmallDeflatedEntryUnknownSizeToFile()
2108 throws Throwable {
2109 withTemporaryArchive("writeSmallDeflatedEntryUnknownSizeToFile",
2110 writeSmallDeflatedEntryToFile(false),
2111 true);
2112 }
2113
2114 @Test public void writeSmallDeflatedEntryKnownSizeToFileModeNever()
2115 throws Throwable {
2116 withTemporaryArchive("writeSmallDeflatedEntryKnownSizeToFileModeNever",
2117 writeSmallDeflatedEntryToFile(true,
2118 Zip64Mode.Never),
2119 true);
2120 }
2121
2122 @Test public void writeSmallDeflatedEntryUnknownSizeToFileModeNever()
2123 throws Throwable {
2124 withTemporaryArchive("writeSmallDeflatedEntryUnknownSizeToFileModeNever",
2125 writeSmallDeflatedEntryToFile(false,
2126 Zip64Mode.Never),
2127 true);
2128 }
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138 private static ZipOutputTest
2139 writeSmallDeflatedEntryToFileModeAlways(final boolean knownSize) {
2140 return new ZipOutputTest() {
2141 public void test(File f, ZipArchiveOutputStream zos)
2142 throws IOException {
2143 zos.setUseZip64(Zip64Mode.Always);
2144 byte[] buf = new byte[ONE_MILLION];
2145 ZipArchiveEntry zae = new ZipArchiveEntry("0");
2146 if (knownSize) {
2147 zae.setSize(ONE_MILLION);
2148 }
2149 zae.setMethod(ZipEntry.DEFLATED);
2150 zos.putArchiveEntry(zae);
2151 zos.write(buf);
2152 zos.closeArchiveEntry();
2153 zos.close();
2154
2155 RandomAccessFile a = new RandomAccessFile(f, "r");
2156 try {
2157 getLengthAndPositionAtCentralDirectory(a);
2158
2159
2160
2161
2162 byte[] header = new byte[12];
2163 a.readFully(header);
2164 assertArrayEquals(new byte[] {
2165
2166 (byte) 0x50, (byte) 0x4b, 1, 2,
2167
2168 45, 0,
2169
2170 45, 0,
2171
2172 0, 8,
2173
2174 8, 0,
2175 }, header);
2176
2177 a.skipBytes(4);
2178 byte[] crc = new byte[4];
2179 a.readFully(crc);
2180 assertArrayEquals(new byte[] {
2181 (byte) 0x9E, (byte) 0xCB, (byte) 0x79, (byte) 0x12,
2182 }, crc);
2183
2184 a.skipBytes(4);
2185 byte[] rest = new byte[23];
2186 a.readFully(rest);
2187 assertArrayEquals(new byte[] {
2188
2189 (byte) 0x40, (byte) 0x42, (byte) 0x0F, 0,
2190
2191 1, 0,
2192
2193 4, 0,
2194
2195 0, 0,
2196
2197 0, 0,
2198
2199 0, 0,
2200 0, 0, 0, 0,
2201
2202 0, 0, 0, 0,
2203
2204 (byte) '0'
2205 }, rest);
2206 byte[] extra = new byte[4];
2207 a.readFully(extra);
2208 assertArrayEquals(new byte[] {
2209
2210 1, 0,
2211
2212 0, 0,
2213 }, extra);
2214
2215
2216 a.seek(0);
2217 header = new byte[10];
2218 a.readFully(header);
2219 assertArrayEquals(new byte[] {
2220
2221 (byte) 0x50, (byte) 0x4b, 3, 4,
2222
2223 45, 0,
2224
2225 0, 8,
2226
2227 8, 0,
2228 }, header);
2229
2230 a.skipBytes(4);
2231 crc = new byte[4];
2232 a.readFully(crc);
2233 assertArrayEquals(new byte[] {
2234 (byte) 0x9E, (byte) 0xCB,
2235 (byte) 0x79, (byte) 0x12,
2236 }, crc);
2237 rest = new byte[13];
2238 a.readFully(rest);
2239
2240 assertArrayEquals(new byte[] {
2241
2242 (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
2243
2244 (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
2245
2246 1, 0,
2247
2248 20, 0,
2249
2250 (byte) '0'
2251 }, rest);
2252
2253 extra = new byte[12];
2254 a.readFully(extra);
2255 assertArrayEquals(new byte[] {
2256
2257 1, 0,
2258
2259 16, 0,
2260
2261 (byte) 0x40, (byte) 0x42, (byte) 0x0F, 0,
2262 0, 0, 0, 0,
2263
2264
2265
2266
2267 }, extra);
2268 } finally {
2269 a.close();
2270 }
2271 }
2272 };
2273 }
2274
2275 @Test public void writeSmallDeflatedEntryKnownSizeToFileModeAlways()
2276 throws Throwable {
2277 withTemporaryArchive("writeSmallDeflatedEntryKnownSizeToFileModeAlways",
2278 writeSmallDeflatedEntryToFileModeAlways(true),
2279 true);
2280 }
2281
2282 @Test public void writeSmallDeflatedEntryUnknownSizeToFileModeAlways()
2283 throws Throwable {
2284 withTemporaryArchive("writeSmallDeflatedEntryUnknownSizeToFileModeAlways",
2285 writeSmallDeflatedEntryToFileModeAlways(false),
2286 true);
2287 }
2288
2289 static interface ZipOutputTest {
2290 void test(File f, ZipArchiveOutputStream zos) throws IOException;
2291 }
2292
2293 private static void withTemporaryArchive(String testName,
2294 ZipOutputTest test,
2295 boolean useRandomAccessFile)
2296 throws Throwable {
2297 File f = getTempFile(testName);
2298 BufferedOutputStream os = null;
2299 ZipArchiveOutputStream zos = useRandomAccessFile
2300 ? new ZipArchiveOutputStream(f)
2301 : new ZipArchiveOutputStream(os = new BufferedOutputStream(new FileOutputStream(f)));
2302 try {
2303 test.test(f, zos);
2304 } catch (IOException ex) {
2305 System.err.println("Failed to write archive because of: "
2306 + ex.getMessage()
2307 + " - likely not enough disk space.");
2308 assumeTrue(false);
2309 } finally {
2310 try {
2311 zos.destroy();
2312 } finally {
2313 if (os != null) {
2314 os.close();
2315 }
2316 AbstractTestCase.tryHardToDelete(f);
2317 }
2318 }
2319 }
2320
2321 private static File get5GBZerosFile() throws Throwable {
2322 return getFile("5GB_of_Zeros.zip");
2323 }
2324
2325 private static File get5GBZerosFileGeneratedBy7ZIP() throws Throwable {
2326 return getFile("5GB_of_Zeros_7ZIP.zip");
2327 }
2328
2329 private static File get5GBZerosFileGeneratedByJava7Jar() throws Throwable {
2330 return getFile("5GB_of_Zeros_jar.zip");
2331 }
2332
2333 private static File get5GBZerosFileGeneratedByWinZIP() throws Throwable {
2334 return getFile("5GB_of_Zeros_WinZip.zip");
2335 }
2336
2337 private static File get5GBZerosFileGeneratedByPKZip() throws Throwable {
2338 return getFile("5GB_of_Zeros_PKZip.zip");
2339 }
2340
2341 private static File get100KFileFile() throws Throwable {
2342 return getFile("100k_Files.zip");
2343 }
2344
2345 private static File get100KFileFileGeneratedBy7ZIP() throws Throwable {
2346 return getFile("100k_Files_7ZIP.zip");
2347 }
2348
2349 private static File get100KFileFileGeneratedByWinCF() throws Throwable {
2350 return getFile("100k_Files_WindowsCompressedFolders.zip");
2351 }
2352
2353 private static File get100KFileFileGeneratedByJava7Jar() throws Throwable {
2354 return getFile("100k_Files_jar.zip");
2355 }
2356
2357 private static File get100KFileFileGeneratedByWinZIP() throws Throwable {
2358 return getFile("100k_Files_WinZIP.zip");
2359 }
2360
2361 private static File get100KFileFileGeneratedByPKZip() throws Throwable {
2362 return getFile("100k_Files_PKZip.zip");
2363 }
2364
2365 private static File getTempFile(String testName) throws Throwable {
2366 File f = File.createTempFile("commons-compress-" + testName, ".zip");
2367 f.deleteOnExit();
2368 return f;
2369 }
2370
2371 private static void read5GBOfZerosImpl(File f, String expectedName)
2372 throws IOException {
2373 FileInputStream fin = new FileInputStream(f);
2374 ZipArchiveInputStream zin = null;
2375 try {
2376 zin = new ZipArchiveInputStream(fin);
2377 ZipArchiveEntry zae = zin.getNextZipEntry();
2378 while (zae.isDirectory()) {
2379 zae = zin.getNextZipEntry();
2380 }
2381 assertEquals(expectedName, zae.getName());
2382 byte[] buf = new byte[1024 * 1024];
2383 long read = 0;
2384 Random r = new Random(System.currentTimeMillis());
2385 int readNow;
2386 while ((readNow = zin.read(buf, 0, buf.length)) > 0) {
2387
2388
2389 for (int i = 0; i < 1024; i++) {
2390 int idx = r.nextInt(readNow);
2391 assertEquals("testing byte " + (read + idx), 0, buf[idx]);
2392 }
2393 read += readNow;
2394 }
2395 assertEquals(FIVE_BILLION, read);
2396 assertNull(zin.getNextZipEntry());
2397 assertEquals(FIVE_BILLION, zae.getSize());
2398 } finally {
2399 if (zin != null) {
2400 zin.close();
2401 }
2402 fin.close();
2403 }
2404 }
2405
2406 private static void read5GBOfZerosUsingZipFileImpl(File f,
2407 String expectedName)
2408 throws IOException {
2409 ZipFile zf = null;
2410 try {
2411 zf = new ZipFile(f);
2412 Enumeration<ZipArchiveEntry> e = zf.getEntries();
2413 assertTrue(e.hasMoreElements());
2414 ZipArchiveEntry zae = e.nextElement();
2415 while (zae.isDirectory()) {
2416 zae = e.nextElement();
2417 }
2418 assertEquals(expectedName, zae.getName());
2419 assertEquals(FIVE_BILLION, zae.getSize());
2420 byte[] buf = new byte[1024 * 1024];
2421 long read = 0;
2422 Random r = new Random(System.currentTimeMillis());
2423 int readNow;
2424 InputStream zin = zf.getInputStream(zae);
2425 try {
2426 while ((readNow = zin.read(buf, 0, buf.length)) > 0) {
2427
2428
2429 for (int i = 0; i < 1024; i++) {
2430 int idx = r.nextInt(readNow);
2431 assertEquals("testing byte " + (read + idx), 0, buf[idx]);
2432 }
2433 read += readNow;
2434 }
2435 } finally {
2436 zin.close();
2437 }
2438 assertEquals(FIVE_BILLION, read);
2439 assertFalse(e.hasMoreElements());
2440 } finally {
2441 ZipFile.closeQuietly(zf);
2442 }
2443 }
2444
2445 private static void read100KFilesImpl(File f) throws IOException {
2446 FileInputStream fin = new FileInputStream(f);
2447 ZipArchiveInputStream zin = null;
2448 try {
2449 zin = new ZipArchiveInputStream(fin);
2450 int files = 0;
2451 ZipArchiveEntry zae = null;
2452 while ((zae = zin.getNextZipEntry()) != null) {
2453 if (!zae.isDirectory()) {
2454 files++;
2455 assertEquals(0, zae.getSize());
2456 }
2457 }
2458 assertEquals(ONE_HUNDRED_THOUSAND, files);
2459 } finally {
2460 if (zin != null) {
2461 zin.close();
2462 }
2463 fin.close();
2464 }
2465 }
2466
2467 private static void read100KFilesUsingZipFileImpl(File f)
2468 throws IOException {
2469 ZipFile zf = null;
2470 try {
2471 zf = new ZipFile(f);
2472 int files = 0;
2473 for (Enumeration<ZipArchiveEntry> e = zf.getEntries(); e.hasMoreElements(); ) {
2474 ZipArchiveEntry zae = e.nextElement();
2475 if (!zae.isDirectory()) {
2476 files++;
2477 assertEquals(0, zae.getSize());
2478 }
2479 }
2480 assertEquals(ONE_HUNDRED_THOUSAND, files);
2481 } finally {
2482 ZipFile.closeQuietly(zf);
2483 }
2484 }
2485
2486 private static long getLengthAndPositionAtCentralDirectory(RandomAccessFile a)
2487 throws IOException {
2488 final long end = a.length();
2489 a.seek(end - 22 - 20);
2490 byte[] sig = new byte[4];
2491 a.readFully(sig);
2492 if (sig[0] != (byte) 0x50 || sig[1] != (byte) 0x4b
2493 || sig[2] != 6 || sig[3] != 7) {
2494
2495 return getLengthAndPositionAtCentralDirectory32(a, end);
2496 }
2497
2498 long cdOffsetLoc = end - 22 - 20 - 56 + 48;
2499
2500 a.seek(cdOffsetLoc);
2501 byte[] cdOffset = new byte[8];
2502 a.readFully(cdOffset);
2503 a.seek(ZipEightByteInteger.getLongValue(cdOffset));
2504 return end;
2505 }
2506
2507 private static long getLengthAndPositionAtCentralDirectory32(RandomAccessFile a, final long end)
2508 throws IOException {
2509 a.seek(end - 22 + 16);
2510 byte[] cdOffset = new byte[4];
2511 a.readFully(cdOffset);
2512 a.seek(ZipLong.getValue(cdOffset));
2513 return end;
2514 }
2515
2516 private static void write100KFilesToStream(ZipArchiveOutputStream zos)
2517 throws IOException {
2518 for (int i = 0; i < ONE_HUNDRED_THOUSAND; i++) {
2519 ZipArchiveEntry zae = new ZipArchiveEntry(String.valueOf(i));
2520 zae.setSize(0);
2521 zos.putArchiveEntry(zae);
2522 zos.closeArchiveEntry();
2523 }
2524 zos.close();
2525 }
2526
2527 private static void
2528 write3EntriesCreatingBigArchiveToStream(ZipArchiveOutputStream zos)
2529 throws IOException {
2530 byte[] buf = new byte[ONE_MILLION];
2531 ZipArchiveEntry zae = null;
2532 for (int i = 0; i < 2; i++) {
2533 zae = new ZipArchiveEntry(String.valueOf(i));
2534 zae.setSize(FIVE_BILLION / 2);
2535 zae.setMethod(ZipEntry.STORED);
2536 zae.setCrc(0x8a408f16L);
2537 zos.putArchiveEntry(zae);
2538 for (int j = 0; j < FIVE_BILLION / 2 / 1000 / 1000;
2539 j++) {
2540 zos.write(buf);
2541 }
2542 zos.closeArchiveEntry();
2543 }
2544 zae = new ZipArchiveEntry(String.valueOf(2));
2545 zae.setSize(1);
2546 zae.setMethod(ZipEntry.STORED);
2547 zae.setCrc(0x9b9265bL);
2548 zos.putArchiveEntry(zae);
2549 zos.write(new byte[] { 42 });
2550 zos.closeArchiveEntry();
2551 zos.close();
2552 }
2553 }