Search in sources :

Example 56 with ZipOutputStream

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

the class TextIOTest method createZipFile.

/**
   * Create a zip file with the given lines.
   *
   * @param expected A list of expected lines, populated in the zip file.
   * @param filename Optionally zip file name (can be null).
   * @param fieldsEntries Fields to write in zip entries.
   * @return The zip filename.
   * @throws Exception In case of a failure during zip file creation.
   */
private String createZipFile(List<String> expected, String filename, String[]... fieldsEntries) throws Exception {
    File tmpFile = tempFolder.resolve(filename).toFile();
    String tmpFileName = tmpFile.getPath();
    ZipOutputStream out = new ZipOutputStream(new FileOutputStream(tmpFile));
    PrintStream writer = new PrintStream(out, true);
    int index = 0;
    for (String[] entry : fieldsEntries) {
        out.putNextEntry(new ZipEntry(Integer.toString(index)));
        for (String field : entry) {
            writer.println(field);
            expected.add(field);
        }
        out.closeEntry();
        index++;
    }
    writer.close();
    out.close();
    return tmpFileName;
}
Also used : PrintStream(java.io.PrintStream) ZipOutputStream(java.util.zip.ZipOutputStream) FileOutputStream(java.io.FileOutputStream) ZipEntry(java.util.zip.ZipEntry) File(java.io.File)

Example 57 with ZipOutputStream

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

the class SXSSFWorkbook method injectData.

protected void injectData(ZipEntrySource zipEntrySource, OutputStream out) throws IOException {
    try {
        ZipOutputStream zos = new ZipOutputStream(out);
        try {
            Enumeration<? extends ZipEntry> en = zipEntrySource.getEntries();
            while (en.hasMoreElements()) {
                ZipEntry ze = en.nextElement();
                zos.putNextEntry(new ZipEntry(ze.getName()));
                InputStream is = zipEntrySource.getInputStream(ze);
                XSSFSheet xSheet = getSheetFromZipEntryName(ze.getName());
                if (xSheet != null) {
                    SXSSFSheet sxSheet = getSXSSFSheet(xSheet);
                    InputStream xis = sxSheet.getWorksheetXMLInputStream();
                    try {
                        copyStreamAndInjectWorksheet(is, zos, xis);
                    } finally {
                        xis.close();
                    }
                } else {
                    IOUtils.copy(is, zos);
                }
                is.close();
            }
        } finally {
            zos.close();
        }
    } finally {
        zipEntrySource.close();
    }
}
Also used : XSSFSheet(org.apache.poi.xssf.usermodel.XSSFSheet) ZipOutputStream(java.util.zip.ZipOutputStream) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry)

Example 58 with ZipOutputStream

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

the class AesZipFileZipEntrySource method copyToFile.

private static void copyToFile(InputStream is, File tmpFile, CipherAlgorithm cipherAlgorithm, byte[] keyBytes, byte[] ivBytes) throws IOException, GeneralSecurityException {
    SecretKeySpec skeySpec = new SecretKeySpec(keyBytes, cipherAlgorithm.jceId);
    Cipher ciEnc = CryptoFunctions.getCipher(skeySpec, cipherAlgorithm, ChainingMode.cbc, ivBytes, Cipher.ENCRYPT_MODE, "PKCS5Padding");
    ZipInputStream zis = new ZipInputStream(is);
    FileOutputStream fos = new FileOutputStream(tmpFile);
    ZipOutputStream zos = new ZipOutputStream(fos);
    ZipEntry ze;
    while ((ze = zis.getNextEntry()) != null) {
        // the cipher output stream pads the data, therefore we can't reuse the ZipEntry with set sizes
        // as those will be validated upon close()
        ZipEntry zeNew = new ZipEntry(ze.getName());
        zeNew.setComment(ze.getComment());
        zeNew.setExtra(ze.getExtra());
        zeNew.setTime(ze.getTime());
        // zeNew.setMethod(ze.getMethod());
        zos.putNextEntry(zeNew);
        FilterOutputStream fos2 = new FilterOutputStream(zos) {

            // don't close underlying ZipOutputStream
            @Override
            public void close() {
            }
        };
        CipherOutputStream cos = new CipherOutputStream(fos2, ciEnc);
        IOUtils.copy(zis, cos);
        cos.close();
        fos2.close();
        zos.closeEntry();
        zis.closeEntry();
    }
    zos.close();
    fos.close();
    zis.close();
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) CipherOutputStream(javax.crypto.CipherOutputStream) SecretKeySpec(javax.crypto.spec.SecretKeySpec) ZipOutputStream(java.util.zip.ZipOutputStream) FileOutputStream(java.io.FileOutputStream) ZipEntry(java.util.zip.ZipEntry) Cipher(javax.crypto.Cipher) FilterOutputStream(java.io.FilterOutputStream)

Example 59 with ZipOutputStream

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

the class ZipPartMarshaller method marshall.

/**
	 * Save the specified part.
	 *
	 * @throws OpenXML4JException
	 *             Throws if an internal exception is thrown.
	 */
@Override
public boolean marshall(PackagePart part, OutputStream os) throws OpenXML4JException {
    if (!(os instanceof ZipOutputStream)) {
        logger.log(POILogger.ERROR, "Unexpected class " + os.getClass().getName());
        throw new OpenXML4JException("ZipOutputStream expected !");
    // Normally should happen only in developement phase, so just throw
    // exception
    }
    // might depend on empty parts being saved, e.g. some unit tests verify this currently.
    if (part.getSize() == 0 && part.getPartName().getName().equals(XSSFRelation.SHARED_STRINGS.getDefaultFileName())) {
        return true;
    }
    ZipOutputStream zos = (ZipOutputStream) os;
    ZipEntry partEntry = new ZipEntry(ZipHelper.getZipItemNameFromOPCName(part.getPartName().getURI().getPath()));
    try {
        // Create next zip entry
        zos.putNextEntry(partEntry);
        // Saving data in the ZIP file
        InputStream ins = part.getInputStream();
        byte[] buff = new byte[ZipHelper.READ_WRITE_FILE_BUFFER_SIZE];
        while (ins.available() > 0) {
            int resultRead = ins.read(buff);
            if (resultRead == -1) {
                // End of file reached
                break;
            }
            zos.write(buff, 0, resultRead);
        }
        zos.closeEntry();
    } catch (IOException ioe) {
        logger.log(POILogger.ERROR, "Cannot write: " + part.getPartName() + ": in ZIP", ioe);
        return false;
    }
    // Saving relationship part
    if (part.hasRelationships()) {
        PackagePartName relationshipPartName = PackagingURIHelper.getRelationshipPartName(part.getPartName());
        marshallRelationshipPart(part.getRelationships(), relationshipPartName, zos);
    }
    return true;
}
Also used : PackagePartName(org.apache.poi.openxml4j.opc.PackagePartName) OpenXML4JException(org.apache.poi.openxml4j.exceptions.OpenXML4JException) ZipOutputStream(java.util.zip.ZipOutputStream) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) IOException(java.io.IOException)

Example 60 with ZipOutputStream

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

the class OOXMLPrettyPrint method handleFile.

private static void handleFile(File file, File outFile) throws ZipException, IOException, TransformerException, ParserConfigurationException {
    System.out.println("Reading zip-file " + file + " and writing pretty-printed XML to " + outFile);
    ZipFile zipFile = ZipHelper.openZipFile(file);
    try {
        ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(outFile)));
        try {
            new OOXMLPrettyPrint().handle(zipFile, out);
        } finally {
            out.close();
        }
    } finally {
        zipFile.close();
        System.out.println();
    }
}
Also used : ZipFile(java.util.zip.ZipFile) ZipOutputStream(java.util.zip.ZipOutputStream) FileOutputStream(java.io.FileOutputStream) BufferedOutputStream(java.io.BufferedOutputStream)

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