1 7 22 23 package de.schlichtherle.util.zip; 24 25 import de.schlichtherle.util.Arrays; 26 27 import java.io.File ; 28 import java.io.FileOutputStream ; 29 import java.io.IOException ; 30 import java.io.InputStream ; 31 import java.util.Enumeration ; 32 import java.util.HashSet ; 33 import java.util.logging.Logger ; 34 35 import junit.framework.*; 36 37 42 public class ManySmallEntriesTest extends TestCase { 43 44 private static final Logger logger 45 = Logger.getLogger(ManySmallEntriesTest.class.getName()); 46 47 private static final byte[] data = "Hello World!".getBytes(); 48 49 public static Test suite() throws Exception { 50 TestSuite suite = new TestSuite(ManySmallEntriesTest.class); 51 52 return suite; 53 } 54 55 private File zip; 56 57 public ManySmallEntriesTest(String testName) { 58 super(testName); 59 } 60 61 protected void setUp() throws IOException { 62 zip = File.createTempFile("zip", null); 63 } 64 65 protected void tearDown() { 66 assertTrue(zip.delete()); 67 } 68 69 public void testManySmallEntries() throws IOException { 70 logger.fine("testManySmallEntries"); 71 72 final int n = 70000; 73 logger.finer("Compressing " + n + " ZIP file entries to: " + zip.getPath()); 74 logger.finer("Note that the max. number of entries supported by the ZIP File Format Spec. is 65535!"); 75 76 final HashSet set = new HashSet (); 77 78 final ZipOutputStream zipOut 79 = new ZipOutputStream(new FileOutputStream (zip)); 80 for (int i = 100000; i < 100000 + n; i++) { 81 String name = i + ".txt"; 82 zipOut.putNextEntry(new ZipEntry(name)); 83 zipOut.write(data); 84 assertTrue(set.add(name)); 85 } 86 zipOut.close(); 87 logger.finer("Compressed " + n + " ZIP file entries into " + zip.length() / 1024 + " KB ZIP file length."); 88 89 final ZipFile zipIn = new ZipFile(zip); 90 try { 91 final byte[] buf = new byte[data.length]; 92 for (Enumeration e = zipIn.entries(); e.hasMoreElements(); ) { 93 final ZipEntry entry = (ZipEntry) e.nextElement(); 94 95 assertEquals(data.length, entry.getSize()); 96 97 InputStream in = zipIn.getInputStream(entry); 98 int off = 0; 99 int read; 100 do { 101 read = in.read(buf); 102 if (read < 0) 103 break; 104 assertTrue(read > 0); 105 assertTrue(Arrays.equals(data, off, buf, 0, read)); 106 off += read; 107 } while (true); 108 assertEquals(-1, read); 109 assertEquals(off, data.length); 110 assertEquals(0, in.read(new byte[0])); 111 in.close(); 112 113 assertTrue(set.remove(entry.getName())); 114 } 115 } finally { 116 zipIn.close(); 117 } 118 119 assertTrue(set.isEmpty()); 120 logger.finer("Successfully decompressed the data in all entries."); 121 } 122 } 123 | Popular Tags |