Search in sources :

Example 56 with ZipEntry

use of java.util.zip.ZipEntry in project j2objc by google.

the class AbstractZipFileTest method writeEntries.

/**
     * Compresses the given number of files, each of the given size, into a .zip archive.
     */
protected void writeEntries(ZipOutputStream out, int entryCount, long entrySize, boolean setEntrySize) throws IOException {
    byte[] writeBuffer = new byte[8192];
    Random random = new Random();
    try {
        for (int entry = 0; entry < entryCount; ++entry) {
            ZipEntry ze = new ZipEntry(Integer.toHexString(entry));
            if (setEntrySize) {
                ze.setSize(entrySize);
            }
            out.putNextEntry(ze);
            for (long i = 0; i < entrySize; i += writeBuffer.length) {
                random.nextBytes(writeBuffer);
                int byteCount = (int) Math.min(writeBuffer.length, entrySize - i);
                out.write(writeBuffer, 0, byteCount);
            }
            out.closeEntry();
        }
    } finally {
        out.close();
    }
}
Also used : Random(java.util.Random) ZipEntry(java.util.zip.ZipEntry)

Example 57 with ZipEntry

use of java.util.zip.ZipEntry in project j2objc by google.

the class AbstractZipFileTest method testSTORED.

public void testSTORED() throws IOException {
    ZipOutputStream out = createZipOutputStream(createTemporaryZipFile());
    CRC32 crc = new CRC32();
    // Missing CRC, size, and compressed size => failure.
    try {
        ZipEntry ze = new ZipEntry("a");
        ze.setMethod(ZipEntry.STORED);
        out.putNextEntry(ze);
        fail();
    } catch (ZipException expected) {
    }
    // Missing CRC and compressed size => failure.
    try {
        ZipEntry ze = new ZipEntry("a");
        ze.setMethod(ZipEntry.STORED);
        ze.setSize(0);
        out.putNextEntry(ze);
        fail();
    } catch (ZipException expected) {
    }
    // Missing CRC and size => failure.
    try {
        ZipEntry ze = new ZipEntry("a");
        ze.setMethod(ZipEntry.STORED);
        ze.setSize(0);
        ze.setCompressedSize(0);
        out.putNextEntry(ze);
        fail();
    } catch (ZipException expected) {
    }
    // Missing size and compressed size => failure.
    try {
        ZipEntry ze = new ZipEntry("a");
        ze.setMethod(ZipEntry.STORED);
        ze.setCrc(crc.getValue());
        out.putNextEntry(ze);
        fail();
    } catch (ZipException expected) {
    }
    // Missing size is copied from compressed size.
    {
        ZipEntry ze = new ZipEntry("okay1");
        ze.setMethod(ZipEntry.STORED);
        ze.setCrc(crc.getValue());
        assertEquals(-1, ze.getSize());
        assertEquals(-1, ze.getCompressedSize());
        ze.setCompressedSize(0);
        assertEquals(-1, ze.getSize());
        assertEquals(0, ze.getCompressedSize());
        out.putNextEntry(ze);
        assertEquals(0, ze.getSize());
        assertEquals(0, ze.getCompressedSize());
    }
    // Missing compressed size is copied from size.
    {
        ZipEntry ze = new ZipEntry("okay2");
        ze.setMethod(ZipEntry.STORED);
        ze.setCrc(crc.getValue());
        assertEquals(-1, ze.getSize());
        assertEquals(-1, ze.getCompressedSize());
        ze.setSize(0);
        assertEquals(0, ze.getSize());
        assertEquals(-1, ze.getCompressedSize());
        out.putNextEntry(ze);
        assertEquals(0, ze.getSize());
        assertEquals(0, ze.getCompressedSize());
    }
    // Mismatched size and compressed size => failure.
    try {
        ZipEntry ze = new ZipEntry("a");
        ze.setMethod(ZipEntry.STORED);
        ze.setCrc(crc.getValue());
        ze.setCompressedSize(1);
        ze.setSize(0);
        out.putNextEntry(ze);
        fail();
    } catch (ZipException expected) {
    }
    // Everything present => success.
    ZipEntry ze = new ZipEntry("okay");
    ze.setMethod(ZipEntry.STORED);
    ze.setCrc(crc.getValue());
    ze.setSize(0);
    ze.setCompressedSize(0);
    out.putNextEntry(ze);
    out.close();
}
Also used : CRC32(java.util.zip.CRC32) ZipOutputStream(java.util.zip.ZipOutputStream) ZipEntry(java.util.zip.ZipEntry) ZipException(java.util.zip.ZipException)

Example 58 with ZipEntry

use of java.util.zip.ZipEntry in project j2objc by google.

the class AbstractZipFileTest method test_getComment_unset.

public void test_getComment_unset() throws Exception {
    File file = createTemporaryZipFile();
    ZipOutputStream out = createZipOutputStream(file);
    ZipEntry ze = new ZipEntry("test entry");
    ze.setComment("per-entry comment");
    out.putNextEntry(ze);
    out.close();
    ZipFile zipFile = new ZipFile(file);
    assertEquals(null, zipFile.getComment());
}
Also used : ZipFile(java.util.zip.ZipFile) ZipOutputStream(java.util.zip.ZipOutputStream) ZipEntry(java.util.zip.ZipEntry) File(java.io.File) ZipFile(java.util.zip.ZipFile)

Example 59 with ZipEntry

use of java.util.zip.ZipEntry in project j2objc by google.

the class AbstractZipFileTest method testZipFileErrorReadingData.

public void testZipFileErrorReadingData() throws IOException {
    File resources = Support_Resources.createTempFolder();
    File tempZipFile = Support_Resources.copyFile(resources, "java/util/zip", "ZipFileBreak.zip");
    String tempZipFilePath = tempZipFile.getAbsolutePath();
    try (ZipFile zipFile = new ZipFile(tempZipFilePath)) {
        ZipEntry entry = zipFile.getEntry("subdir/file.txt");
        assertNotNull(entry);
        byte[] content = toByteArray(zipFile.getInputStream(entry));
        assertNotNull(content);
        entry = zipFile.getEntry("subdir/file.pb");
        assertNotNull(entry);
        content = toByteArray(zipFile.getInputStream(entry));
        assertNotNull(content);
    }
}
Also used : ZipFile(java.util.zip.ZipFile) ZipEntry(java.util.zip.ZipEntry) File(java.io.File) ZipFile(java.util.zip.ZipFile)

Example 60 with ZipEntry

use of java.util.zip.ZipEntry in project j2objc by google.

the class OldAndroidZipStreamTest method createCompressedZip.

private static void createCompressedZip(ByteArrayOutputStream bytesOut) throws IOException {
    ZipOutputStream out = new ZipOutputStream(bytesOut);
    try {
        int i;
        for (i = 0; i < 3; i++) {
            byte[] input = makeSampleFile(i);
            ZipEntry newEntry = new ZipEntry("file-" + i);
            if (i != 1)
                newEntry.setComment("this is file " + i);
            out.putNextEntry(newEntry);
            out.write(input, 0, input.length);
            out.closeEntry();
        }
        out.setComment("This is a lovely compressed archive!");
    } finally {
        out.close();
    }
}
Also used : ZipOutputStream(java.util.zip.ZipOutputStream) ZipEntry(java.util.zip.ZipEntry)

Aggregations

ZipEntry (java.util.zip.ZipEntry)1570 File (java.io.File)526 ZipFile (java.util.zip.ZipFile)523 IOException (java.io.IOException)445 ZipInputStream (java.util.zip.ZipInputStream)361 ZipOutputStream (java.util.zip.ZipOutputStream)357 InputStream (java.io.InputStream)332 FileInputStream (java.io.FileInputStream)321 FileOutputStream (java.io.FileOutputStream)314 BufferedInputStream (java.io.BufferedInputStream)147 JarFile (java.util.jar.JarFile)132 Test (org.junit.Test)129 BufferedOutputStream (java.io.BufferedOutputStream)117 ByteArrayOutputStream (java.io.ByteArrayOutputStream)113 ByteArrayInputStream (java.io.ByteArrayInputStream)101 ArrayList (java.util.ArrayList)92 OutputStream (java.io.OutputStream)76 FileNotFoundException (java.io.FileNotFoundException)69 Enumeration (java.util.Enumeration)63 JarOutputStream (java.util.jar.JarOutputStream)63