Example usage for java.util.zip ZipOutputStream putNextEntry

List of usage examples for java.util.zip ZipOutputStream putNextEntry

Introduction

In this page you can find the example usage for java.util.zip ZipOutputStream putNextEntry.

Prototype

public void putNextEntry(ZipEntry e) throws IOException 

Source Link

Document

Begins writing a new ZIP file entry and positions the stream to the start of the entry data.

Usage

From source file:ZipTransformTest.java

public void testStreamTransformerIdentity() throws IOException {
    final String name = "foo";
    final byte[] contents = "bar".getBytes();

    File file1 = File.createTempFile("temp", null);
    File file2 = File.createTempFile("temp", null);
    try {/*from   ww  w  .  j a  va  2s . co m*/
        // Create the ZIP file
        ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(file1));
        try {
            zos.putNextEntry(new ZipEntry(name));
            zos.write(contents);
            zos.closeEntry();
        } finally {
            IOUtils.closeQuietly(zos);
        }

        // Transform the ZIP file
        ZipUtil.transformEntry(file1, name, new StreamZipEntryTransformer() {
            protected void transform(ZipEntry zipEntry, InputStream in, OutputStream out) throws IOException {
                IOUtils.copy(in, out);
            }
        }, file2);

        // Test the ZipUtil
        byte[] actual = ZipUtil.unpackEntry(file2, name);
        assertNotNull(actual);
        assertEquals(new String(contents), new String(actual));
    } finally {
        FileUtils.deleteQuietly(file1);
        FileUtils.deleteQuietly(file2);
    }
}

From source file:ZipTransformTest.java

public void testByteArrayTransformer() throws IOException {
    final String name = "foo";
    final byte[] contents = "bar".getBytes();

    File file1 = File.createTempFile("temp", null);
    File file2 = File.createTempFile("temp", null);
    try {/*from   w  w  w.  ja va2  s  .c o m*/
        // Create the ZIP file
        ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(file1));
        try {
            zos.putNextEntry(new ZipEntry(name));
            zos.write(contents);
            zos.closeEntry();
        } finally {
            IOUtils.closeQuietly(zos);
        }

        // Transform the ZIP file
        ZipUtil.transformEntry(file1, name, new ByteArrayZipEntryTransformer() {
            protected byte[] transform(ZipEntry zipEntry, byte[] input) throws IOException {
                String s = new String(input);
                assertEquals(new String(contents), s);
                return s.toUpperCase().getBytes();
            }
        }, file2);

        // Test the ZipUtil
        byte[] actual = ZipUtil.unpackEntry(file2, name);
        assertNotNull(actual);
        assertEquals(new String(contents).toUpperCase(), new String(actual));
    } finally {
        FileUtils.deleteQuietly(file1);
        FileUtils.deleteQuietly(file2);
    }
}

From source file:ZipTransformTest.java

public void testStreamTransformer() throws IOException {
    final String name = "foo";
    final byte[] contents = "bar".getBytes();
    final byte[] transformed = "cbs".getBytes();

    File file1 = File.createTempFile("temp", null);
    File file2 = File.createTempFile("temp", null);
    try {//  w w w  . j  a  va  2s  .c o m
        // Create the ZIP file
        ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(file1));
        try {
            zos.putNextEntry(new ZipEntry(name));
            zos.write(contents);
            zos.closeEntry();
        } finally {
            IOUtils.closeQuietly(zos);
        }

        // Transform the ZIP file
        ZipUtil.transformEntry(file1, name, new StreamZipEntryTransformer() {
            protected void transform(ZipEntry zipEntry, InputStream in, OutputStream out) throws IOException {
                int b;
                while ((b = in.read()) != -1)
                    out.write(b + 1);
            }
        }, file2);

        // Test the ZipUtil
        byte[] actual = ZipUtil.unpackEntry(file2, name);
        assertNotNull(actual);
        assertEquals(new String(transformed), new String(actual));
    } finally {
        FileUtils.deleteQuietly(file1);
        FileUtils.deleteQuietly(file2);
    }
}

From source file:com.serotonin.mango.rt.maint.work.ReportWorkItem.java

private void addFileAttachment(EmailContent emailContent, String name, File file) {
    if (file != null) {
        if (reportConfig.isZipData()) {
            try {
                File zipFile = File.createTempFile("tempZIP", ".zip");
                ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFile));
                zipOut.putNextEntry(new ZipEntry(name));

                FileInputStream in = new FileInputStream(file);
                StreamUtils.transfer(in, zipOut);
                in.close();//from  w w  w  . j av  a2 s  . c o  m

                zipOut.closeEntry();
                zipOut.close();

                emailContent.addAttachment(new EmailAttachment.FileAttachment(name + ".zip", zipFile));

                filesToDelete.add(zipFile);
            } catch (IOException e) {
                LOG.error("Failed to create zip file", e);
            }
        } else
            emailContent.addAttachment(new EmailAttachment.FileAttachment(name, file));

        filesToDelete.add(file);
    }
}

From source file:ZipTransformTest.java

public void testStringZipEntryTransformerInStream() throws IOException {
    final String name = "foo";
    String FILE_CONTENTS = "bar";
    final byte[] contents = FILE_CONTENTS.getBytes();

    File file1 = File.createTempFile("temp", null);
    File file2 = File.createTempFile("temp", null);
    try {/*from   w w w .j a v a2 s  .co m*/
        // Create the ZIP file
        ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(file1));
        try {
            zos.putNextEntry(new ZipEntry(name));
            zos.write(contents);
            zos.closeEntry();
        } finally {
            IOUtils.closeQuietly(zos);
        }

        // Transform the ZIP file
        FileInputStream in = null;
        FileOutputStream out = null;
        try {
            in = new FileInputStream(file1);
            out = new FileOutputStream(file2);

            ZipUtil.transformEntry(in, name, new StringZipEntryTransformer("UTF-8") {
                protected String transform(ZipEntry zipEntry, String input) throws IOException {
                    return input.toUpperCase();
                }
            }, out);
        } finally {
            IOUtils.closeQuietly(in);
            IOUtils.closeQuietly(out);
        }

        // Test the ZipUtil
        byte[] actual = ZipUtil.unpackEntry(file2, name);
        assertEquals(FILE_CONTENTS.toUpperCase(), new String(actual));
    } finally {
        FileUtils.deleteQuietly(file1);
        FileUtils.deleteQuietly(file2);
    }
}

From source file:io.lavagna.service.LavagnaExporter.java

private void exportFiles(ZipOutputStream zf, OutputStreamWriter osw) throws IOException {
    osw.flush();
    for (CardDataUploadContentInfo fileData : cardDataRepository.findAllDataUploadContentInfo()) {
        zf.putNextEntry(new ZipEntry("files/" + fileData.getDigest()));
        cardDataRepository.outputFileContent(fileData.getDigest(), zf);
        writeEntry("files/" + fileData.getDigest() + ".json", fileData, zf, osw);
    }//from   w  w w. j  a v  a  2 s  .co m
}

From source file:de.knowwe.revisions.manager.action.DownloadRevisionZip.java

/**
 * Zips the article contents from specified date and writes the resulting
 * zip-File to the ZipOutputStream.//from  w ww  . j  a va  2s .  c om
 * 
 * @created 22.04.2013
 * @param date
 * @param zos
 * @throws IOException
 */
private void zipRev(Date date, ZipOutputStream zos, UserActionContext context) throws IOException {
    RevisionManager revm = RevisionManager.getRM(context);
    ArticleManager am = revm.getArticleManager(date);
    Collection<Article> articles = am.getArticles();
    for (Article article : articles) {
        zos.putNextEntry(new ZipEntry(URLEncoder.encode(article.getTitle() + ".txt", "UTF-8")));
        zos.write(article.getRootSection().getText().getBytes("UTF-8"));
        zos.closeEntry();

        // Attachments
        Collection<WikiAttachment> atts = Environment.getInstance().getWikiConnector()
                .getAttachments(article.getTitle());
        for (WikiAttachment att : atts) {
            zos.putNextEntry(new ZipEntry(URLEncoder.encode(att.getParentName(), "UTF-8") + "-att/"
                    + URLEncoder.encode(att.getFileName(), "UTF-8")));
            IOUtils.copy(att.getInputStream(), zos);
            zos.closeEntry();
        }
    }
    zos.close();
}

From source file:ZipTransformTest.java

public void testByteArrayTransformerInStream() throws IOException {
    final String name = "foo";
    final byte[] contents = "bar".getBytes();

    File file1 = File.createTempFile("temp", null);
    File file2 = File.createTempFile("temp", null);
    try {/*from  www.  j  a v a2  s . co  m*/
        // Create the ZIP file
        ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(file1));
        try {
            zos.putNextEntry(new ZipEntry(name));
            zos.write(contents);
            zos.closeEntry();
        } finally {
            IOUtils.closeQuietly(zos);
        }

        // Transform the ZIP file
        FileInputStream in = null;
        FileOutputStream out = null;
        try {
            in = new FileInputStream(file1);
            out = new FileOutputStream(file2);

            ZipUtil.transformEntry(in, name, new ByteArrayZipEntryTransformer() {
                protected byte[] transform(ZipEntry zipEntry, byte[] input) throws IOException {
                    String s = new String(input);
                    assertEquals(new String(contents), s);
                    return s.toUpperCase().getBytes();
                }
            }, out);
        } finally {
            IOUtils.closeQuietly(in);
            IOUtils.closeQuietly(out);
        }

        // Test the ZipUtil
        byte[] actual = ZipUtil.unpackEntry(file2, name);
        assertNotNull(actual);
        assertEquals(new String(contents).toUpperCase(), new String(actual));
    } finally {
        FileUtils.deleteQuietly(file1);
        FileUtils.deleteQuietly(file2);
    }
}

From source file:ZipTransformTest.java

public void testFileZipEntryTransformerInStream() throws IOException {
    final String name = "foo";
    final byte[] contents = "bar".getBytes();

    File file1 = File.createTempFile("temp", null);
    File file2 = File.createTempFile("temp", null);
    try {/*from  w ww .ja  v  a  2  s.c o m*/
        // Create the ZIP file
        ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(file1));
        try {
            zos.putNextEntry(new ZipEntry(name));
            zos.write(contents);
            zos.closeEntry();
        } finally {
            IOUtils.closeQuietly(zos);
        }

        // Transform the ZIP file
        FileInputStream in = null;
        FileOutputStream out = null;
        try {
            in = new FileInputStream(file1);
            out = new FileOutputStream(file2);

            ZipUtil.transformEntry(in, name, new FileZipEntryTransformer() {
                protected void transform(ZipEntry zipEntry, File in, File out) throws IOException {
                    FileWriter fw = new FileWriter(out);
                    fw.write("CAFEBABE");
                    fw.close();
                }
            }, out);
        } finally {
            IOUtils.closeQuietly(in);
            IOUtils.closeQuietly(out);
        }

        // Test the ZipUtil
        byte[] actual = ZipUtil.unpackEntry(file2, name);
        assertNotNull(actual);
        assertEquals("CAFEBABE", new String(actual));
    } finally {
        FileUtils.deleteQuietly(file1);
        FileUtils.deleteQuietly(file2);
    }
}

From source file:com.coinblesk.client.backup.BackupDialogFragment.java

private void addZipEntry(String filename, byte[] data, ZipOutputStream zos) throws IOException {
    ZipEntry entry = new ZipEntry(filename);
    zos.putNextEntry(entry);
    zos.write(data);//from   ww  w .j a va2 s  .  c  o m
    zos.closeEntry();
    zos.flush();
}