Search in sources :

Example 66 with ZipOutputStream

use of java.util.zip.ZipOutputStream in project robovm by robovm.

the class ZipFileTest 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 67 with ZipOutputStream

use of java.util.zip.ZipOutputStream in project robovm by robovm.

the class ZipFileTest method testDuplicateEntries.

/**
     * Make sure we don't fail silently for duplicate entries.
     * b/8219321
     */
public void testDuplicateEntries() throws Exception {
    String name1 = "test_file_name1";
    String name2 = "test_file_name2";
    // Create the good zip file.
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ZipOutputStream out = new ZipOutputStream(baos);
    out.putNextEntry(new ZipEntry(name2));
    out.closeEntry();
    out.putNextEntry(new ZipEntry(name1));
    out.closeEntry();
    out.close();
    // Rewrite one of the filenames.
    byte[] buffer = baos.toByteArray();
    replaceBytes(buffer, name2.getBytes(), name1.getBytes());
    // Write the result to a file.
    File badZip = createTemporaryZipFile();
    writeBytes(badZip, buffer);
    // Check that we refuse to load the modified file.
    try {
        ZipFile bad = new ZipFile(badZip);
        fail();
    } catch (ZipException expected) {
    }
}
Also used : ZipFile(java.util.zip.ZipFile) ZipOutputStream(java.util.zip.ZipOutputStream) ZipEntry(java.util.zip.ZipEntry) ZipException(java.util.zip.ZipException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) File(java.io.File) ZipFile(java.util.zip.ZipFile)

Example 68 with ZipOutputStream

use of java.util.zip.ZipOutputStream in project robovm by robovm.

the class ZipFileTest method testCrc.

public void testCrc() throws IOException {
    ZipEntry ze = new ZipEntry("test");
    ze.setMethod(ZipEntry.STORED);
    ze.setSize(4);
    // setCrc takes a long, not an int, so -1 isn't a valid CRC32 (because it's 64 bits).
    try {
        ze.setCrc(-1);
    } catch (IllegalArgumentException expected) {
    }
    // You can set the CRC32 to 0xffffffff if you're slightly more careful though...
    ze.setCrc(0xffffffffL);
    assertEquals(0xffffffffL, ze.getCrc());
    // And it actually works, even though we use -1L to mean "no CRC set"...
    ZipOutputStream out = createZipOutputStream(createTemporaryZipFile());
    out.putNextEntry(ze);
    out.write(-1);
    out.write(-1);
    out.write(-1);
    out.write(-1);
    out.closeEntry();
    out.close();
}
Also used : ZipOutputStream(java.util.zip.ZipOutputStream) ZipEntry(java.util.zip.ZipEntry)

Example 69 with ZipOutputStream

use of java.util.zip.ZipOutputStream in project robovm by robovm.

the class OldZipInputStreamTest method setUp.

@Override
protected void setUp() throws IOException {
    InputStream is = Support_Resources.getStream("hyts_ZipFile.zip");
    if (is == null) {
        System.out.println("file hyts_ZipFile.zip can not be found");
    }
    zis = new ZipInputStream(is);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ZipOutputStream zos = new ZipOutputStream(bos);
    ZipEntry entry = new ZipEntry("myFile");
    zos.putNextEntry(entry);
    zos.write(dataBytes);
    zos.closeEntry();
    zos.close();
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) ZipInputStream(java.util.zip.ZipInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ZipOutputStream(java.util.zip.ZipOutputStream) ZipEntry(java.util.zip.ZipEntry) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 70 with ZipOutputStream

use of java.util.zip.ZipOutputStream in project bazel by bazelbuild.

the class ZipFactory method toByteArray.

public byte[] toByteArray() {
    try {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ZipOutputStream zipper = new ZipOutputStream(out);
        for (Entry entry : entries) {
            ZipEntry zipEntry = new ZipEntry(entry.name);
            if (entry.compressed) {
                zipEntry.setMethod(ZipEntry.DEFLATED);
            } else {
                zipEntry.setMethod(ZipEntry.STORED);
                zipEntry.setSize(entry.content.length);
                zipEntry.setCrc(calculateCrc32(entry.content));
            }
            zipEntry.setTime(ZipCombiner.DOS_EPOCH.getTime());
            zipper.putNextEntry(zipEntry);
            zipper.write(entry.content);
            zipper.closeEntry();
        }
        zipper.close();
        return out.toByteArray();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : ZipEntry(java.util.zip.ZipEntry) ZipOutputStream(java.util.zip.ZipOutputStream) ZipEntry(java.util.zip.ZipEntry) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Aggregations

ZipOutputStream (java.util.zip.ZipOutputStream)1168 ZipEntry (java.util.zip.ZipEntry)745 FileOutputStream (java.io.FileOutputStream)561 File (java.io.File)486 IOException (java.io.IOException)393 FileInputStream (java.io.FileInputStream)193 ByteArrayOutputStream (java.io.ByteArrayOutputStream)186 BufferedOutputStream (java.io.BufferedOutputStream)177 ZipFile (java.util.zip.ZipFile)163 InputStream (java.io.InputStream)144 Test (org.junit.Test)128 OutputStream (java.io.OutputStream)109 ByteArrayInputStream (java.io.ByteArrayInputStream)94 ZipInputStream (java.util.zip.ZipInputStream)88 Path (java.nio.file.Path)82 BufferedInputStream (java.io.BufferedInputStream)61 ArrayList (java.util.ArrayList)55 FileNotFoundException (java.io.FileNotFoundException)51 Date (java.util.Date)44 HashMap (java.util.HashMap)44