Example usage for java.util.zip ZipOutputStream closeEntry

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

Introduction

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

Prototype

public void closeEntry() throws IOException 

Source Link

Document

Closes the current ZIP entry and positions the stream for writing the next entry.

Usage

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 {/*from w w  w .j ava2  s.c om*/
        // 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:org.activiti.designer.kickstart.eclipse.sync.SyncUtil.java

protected static void zipDirectory(final ZipOutputStream out, final File base,
        final String absoluteDirPathToStrip) throws Exception {
    File[] reportFiles = base.listFiles();
    for (final File file : reportFiles) {
        if (file.isDirectory()) {
            zipDirectory(out, file, absoluteDirPathToStrip);
            continue;
        }//www  . j  av a 2  s.c  o  m
        String entryName = StringUtils.removeStart(file.getAbsolutePath(), absoluteDirPathToStrip);
        entryName = backlashReplace(entryName);
        ZipEntry entry = new ZipEntry(entryName);
        out.putNextEntry(entry);
        if (file.isFile()) {
            FileInputStream fin = new FileInputStream(file);
            byte[] fileContent = new byte[(int) file.length()];
            fin.read(fileContent);
            out.write(fileContent);
            fin.close();
        }
        out.closeEntry();
    }
}

From source file:com.maxl.java.aips2xml.Aips2Xml.java

static void zipToFile(String dir_name, String file_name) {
    byte[] buffer = new byte[1024];

    try {//  w  ww  . j  a va 2 s.c o m
        FileOutputStream fos = new FileOutputStream(dir_name + changeExtension(file_name, "zip"));
        ZipOutputStream zos = new ZipOutputStream(fos);
        ZipEntry ze = new ZipEntry(file_name);
        zos.putNextEntry(ze);
        FileInputStream in = new FileInputStream(dir_name + file_name);

        int len = 0;
        while ((len = in.read(buffer)) > 0) {
            zos.write(buffer, 0, len);
        }
        in.close();
        zos.closeEntry();
        zos.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

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 {// w  w  w.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 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: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 {/*w ww  .  j  a  va 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 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 {//  www . j a va2 s  . c om
        // 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:nl.mpi.tg.eg.frinex.rest.CsvController.java

private void addToZipArchive(final ZipOutputStream zipStream, String fileName, byte[] content)
        throws IOException {
    ZipEntry zipEntry = new ZipEntry(fileName);
    zipStream.putNextEntry(zipEntry);//from   w ww . j a  v a 2 s.co m
    zipStream.write(content);
    zipStream.closeEntry();
}

From source file:org.eclipse.cft.server.core.internal.CloudUtil.java

private static void addZipEntries(ZipOutputStream out, List<IModuleResource> allResources,
        Set<IModuleResource> filterInFiles) throws Exception {
    if (allResources == null)
        return;/*from w  ww  .j  a va  2 s .  c o  m*/

    for (IModuleResource resource : allResources) {
        if (resource instanceof IModuleFolder) {

            IModuleResource[] folderResources = ((IModuleFolder) resource).members();

            String entryPath = getZipRelativeName(resource);

            ZipEntry zipEntry = new ZipEntry(entryPath);

            long timeStamp = 0;
            IContainer folder = (IContainer) resource.getAdapter(IContainer.class);
            if (folder != null) {
                timeStamp = folder.getLocalTimeStamp();
            }

            if (timeStamp != IResource.NULL_STAMP && timeStamp != 0) {
                zipEntry.setTime(timeStamp);
            }

            out.putNextEntry(zipEntry);
            out.closeEntry();

            addZipEntries(out, Arrays.asList(folderResources), filterInFiles);
            continue;
        }

        IModuleFile moduleFile = (IModuleFile) resource;
        // Only add files that are in the filterInList
        if (!filterInFiles.contains(moduleFile)) {
            continue;
        }

        String entryPath = getZipRelativeName(resource);

        ZipEntry zipEntry = new ZipEntry(entryPath);

        InputStream input = null;
        long timeStamp = 0;
        IFile iFile = (IFile) moduleFile.getAdapter(IFile.class);
        if (iFile != null) {
            timeStamp = iFile.getLocalTimeStamp();
            input = iFile.getContents();
        } else {
            File file = (File) moduleFile.getAdapter(File.class);
            timeStamp = file.lastModified();
            input = new FileInputStream(file);
        }

        if (timeStamp != IResource.NULL_STAMP && timeStamp != 0) {
            zipEntry.setTime(timeStamp);
        }

        out.putNextEntry(zipEntry);

        try {
            int n = 0;
            while (n > -1) {
                n = input.read(buf);
                if (n > 0) {
                    out.write(buf, 0, n);
                }
            }
        } finally {
            input.close();
        }

        out.closeEntry();
    }
}

From source file:com.meltmedia.cadmium.core.util.WarUtils.java

/**
 * Writes a xml document to a zip file with an entry specified by the jbossWeb parameter.
 * @param outZip The zip output stream to write to.
 * @param jbossWeb The zip entry to add to the zip file.
 * @param doc The xml DOM document to write to the zip file.
 * @throws IOException //w ww. ja  v a2 s  .  c o m
 * @throws TransformerFactoryConfigurationError
 * @throws TransformerConfigurationException
 * @throws TransformerException
 */
public static void storeXmlDocument(ZipOutputStream outZip, ZipEntry jbossWeb, Document doc) throws IOException,
        TransformerFactoryConfigurationError, TransformerConfigurationException, TransformerException {
    jbossWeb = new ZipEntry(jbossWeb.getName());
    outZip.putNextEntry(jbossWeb);

    TransformerFactory tFactory = TransformerFactory.newInstance();
    Transformer transformer = tFactory.newTransformer();

    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");

    DOMSource source = new DOMSource(doc);
    StreamResult result = new StreamResult(outZip);
    transformer.transform(source, result);

    outZip.closeEntry();
}

From source file:com.android.tradefed.util.FileUtil.java

/**
 * Recursively adds given file and its contents to ZipOutputStream
 *
 * @param out//from   www  .  ja v a2 s  .c o m
 *            the {@link ZipOutputStream}
 * @param file
 *            the {@link File} to add to the stream
 * @param relativePathSegs
 *            the relative path of file, including separators
 * @throws IOException
 *             if failed to add file to zip
 */
private static void addToZip(ZipOutputStream out, File file, List<String> relativePathSegs) throws IOException {
    relativePathSegs.add(file.getName());
    if (file.isDirectory()) {
        // note: it appears even on windows, ZipEntry expects '/' as a path
        // separator
        relativePathSegs.add("/");
    }
    ZipEntry zipEntry = new ZipEntry(buildPath(relativePathSegs));
    out.putNextEntry(zipEntry);
    if (file.isFile()) {
        writeToStream(file, out);
    }
    out.closeEntry();
    if (file.isDirectory()) {
        // recursively add contents
        File[] subFiles = file.listFiles();
        if (subFiles == null) {
            throw new IOException(String.format("Could not read directory %s", file.getAbsolutePath()));
        }
        for (File subFile : subFiles) {
            addToZip(out, subFile, relativePathSegs);
        }
        // remove the path separator
        relativePathSegs.remove(relativePathSegs.size() - 1);
    }
    // remove the last segment, added at beginning of method
    relativePathSegs.remove(relativePathSegs.size() - 1);
}