Search in sources :

Example 61 with ZipOutputStream

use of java.util.zip.ZipOutputStream in project poi by apache.

the class ZipContentTypeManager method saveImpl.

@SuppressWarnings("resource")
@Override
public boolean saveImpl(Document content, OutputStream out) {
    ZipOutputStream zos = null;
    if (out instanceof ZipOutputStream)
        zos = (ZipOutputStream) out;
    else
        zos = new ZipOutputStream(out);
    ZipEntry partEntry = new ZipEntry(CONTENT_TYPES_PART_NAME);
    try {
        // Referenced in ZIP
        zos.putNextEntry(partEntry);
        // Saving data in the ZIP file
        if (!StreamHelper.saveXmlInStream(content, zos)) {
            return false;
        }
        zos.closeEntry();
    } catch (IOException ioe) {
        logger.log(POILogger.ERROR, "Cannot write: " + CONTENT_TYPES_PART_NAME + " in Zip !", ioe);
        return false;
    }
    return true;
}
Also used : ZipOutputStream(java.util.zip.ZipOutputStream) ZipEntry(java.util.zip.ZipEntry) IOException(java.io.IOException)

Example 62 with ZipOutputStream

use of java.util.zip.ZipOutputStream in project intellij-community by JetBrains.

the class ArtifactBuilderOverwriteTest method createArchive.

private String createArchive(String relativeArchivePath, String fileNameInArchive, String text) {
    try {
        File file = new File(getOrCreateProjectDir(), relativeArchivePath);
        ZipOutputStream output = new ZipOutputStream(new FileOutputStream(file));
        try {
            output.putNextEntry(new ZipEntry(fileNameInArchive));
            output.write(text.getBytes(CharsetToolkit.UTF8));
            output.closeEntry();
        } finally {
            output.close();
        }
        return FileUtil.toSystemIndependentName(file.getAbsolutePath());
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : ZipOutputStream(java.util.zip.ZipOutputStream) FileOutputStream(java.io.FileOutputStream) ZipEntry(java.util.zip.ZipEntry) IOException(java.io.IOException) File(java.io.File)

Example 63 with ZipOutputStream

use of java.util.zip.ZipOutputStream in project asterixdb by apache.

the class AsterixEventServiceUtil method zipDir.

public static void zipDir(File sourceDir, File destFile) throws IOException {
    FileOutputStream fos = new FileOutputStream(destFile);
    ZipOutputStream zos = new ZipOutputStream(fos);
    zipDir(EMPTY_STRING, sourceDir, destFile, zos);
    zos.close();
}
Also used : ZipOutputStream(java.util.zip.ZipOutputStream) FileOutputStream(java.io.FileOutputStream)

Example 64 with ZipOutputStream

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

the class ZipFileTest method createZipFile.

/**
     * Compresses the given number of files, each of the given size, into a .zip archive.
     */
private File createZipFile(int entryCount, int entrySize) throws IOException {
    File result = createTemporaryZipFile();
    byte[] writeBuffer = new byte[8192];
    Random random = new Random();
    ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(result)));
    for (int entry = 0; entry < entryCount; ++entry) {
        ZipEntry ze = new ZipEntry(Integer.toHexString(entry));
        out.putNextEntry(ze);
        for (int i = 0; i < entrySize; i += writeBuffer.length) {
            random.nextBytes(writeBuffer);
            int byteCount = Math.min(writeBuffer.length, entrySize - i);
            out.write(writeBuffer, 0, byteCount);
        }
        out.closeEntry();
    }
    out.close();
    return result;
}
Also used : Random(java.util.Random) ZipOutputStream(java.util.zip.ZipOutputStream) FileOutputStream(java.io.FileOutputStream) ZipEntry(java.util.zip.ZipEntry) File(java.io.File) ZipFile(java.util.zip.ZipFile) BufferedOutputStream(java.io.BufferedOutputStream)

Example 65 with ZipOutputStream

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

the class ZipFileTest 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)

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