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:org.cloudfoundry.client.lib.io.DynamicZipInputStreamTest.java

@Test
public void shouldCreateValidZipContent() throws Exception {

    byte[] f1 = newRandomBytes(10000);
    byte[] f2 = newRandomBytes(10000);

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ZipOutputStream zipOutputStream = new ZipOutputStream(bos);
    zipOutputStream.putNextEntry(new ZipEntry("a/b/c"));
    zipOutputStream.write(f1);/* w  w w .  j av a  2  s.co m*/
    zipOutputStream.closeEntry();
    zipOutputStream.putNextEntry(new ZipEntry("a/b/c/d/"));
    zipOutputStream.closeEntry();
    zipOutputStream.putNextEntry(new ZipEntry("d/e/f"));
    zipOutputStream.write(f2);
    zipOutputStream.closeEntry();
    zipOutputStream.flush();
    zipOutputStream.close();
    byte[] expected = bos.toByteArray();

    List<DynamicZipInputStream.Entry> entries = new ArrayList<DynamicZipInputStream.Entry>();
    entries.add(newEntry("a/b/c", f1));
    entries.add(newEntry("a/b/c/d/", null));
    entries.add(newEntry("d/e/f", f2));
    DynamicZipInputStream inputStream = new DynamicZipInputStream(entries);
    bos.reset();
    FileCopyUtils.copy(inputStream, bos);
    byte[] actual = bos.toByteArray();

    assertThat(actual, is(equalTo(expected)));
}

From source file:org.apache.brooklyn.rest.resources.BundleAndTypeResourcesTest.java

private static File createZip(Map<String, String> files) throws Exception {
    File f = Os.newTempFile("osgi", "zip");

    ZipOutputStream zip = new ZipOutputStream(new FileOutputStream(f));

    for (Map.Entry<String, String> entry : files.entrySet()) {
        ZipEntry ze = new ZipEntry(entry.getKey());
        zip.putNextEntry(ze);
        zip.write(entry.getValue().getBytes());
    }//from  www .  j a  v  a2  s. co  m

    zip.closeEntry();
    zip.flush();
    zip.close();

    return f;
}

From source file:com.google.mr4c.sources.LogsDatasetSource.java

private void addZipEntry(ZipOutputStream zipStream, String name) throws IOException {
    ZipEntry entry = new ZipEntry(name);
    zipStream.putNextEntry(entry);
    DataFileSource entrySource = m_fileSrc.getFileSource(name);
    InputStream input = entrySource.getFileInputStream();
    try {//from   w w  w.j  ava2s  . c  o m
        IOUtils.copy(input, zipStream);
        zipStream.closeEntry();
    } finally {
        input.close();
    }
}

From source file:eionet.gdem.utils.ZipUtil.java

/**
 *
 * @param f// w w  w . j  a  v a 2  s . c o  m
 *            - File that will be zipped
 * @param outZip
 *            - ZipOutputStream represents the zip file, where the files will be placed
 * @param sourceDir
 *            - root directory, where the zipping started
 * @param doCompress
 *            - don't comress the file, if doCompress=true
 * @throws IOException If an error occurs.
 */
public static void zipFile(File f, ZipOutputStream outZip, String sourceDir, boolean doCompress)
        throws IOException {

    // Read the source file into byte array
    byte[] fileBytes = Utils.getBytesFromFile(f);

    // create a new zip entry
    String strAbsPath = f.getPath();
    String strZipEntryName = strAbsPath.substring(sourceDir.length() + 1, strAbsPath.length());
    strZipEntryName = Utils.Replace(strZipEntryName, File.separator, "/");
    ZipEntry anEntry = new ZipEntry(strZipEntryName);

    // Don't compress the file, if not needed
    if (!doCompress) {
        // ZipEntry can't calculate crc size automatically, if we use STORED method.
        anEntry.setMethod(ZipEntry.STORED);
        anEntry.setSize(fileBytes.length);
        CRC32 crc321 = new CRC32();
        crc321.update(fileBytes);
        anEntry.setCrc(crc321.getValue());
    }
    // place the zip entry in the ZipOutputStream object
    outZip.putNextEntry(anEntry);

    // now write the content of the file to the ZipOutputStream
    outZip.write(fileBytes);

    outZip.flush();
    // Close the current entry
    outZip.closeEntry();

}

From source file:com.c4om.autoconf.ulysses.configanalyzer.packager.ZipConfigurationPackager.java

/**
 * This implementation compresses files into a ZIP file
 * @see com.c4om.autoconf.ulysses.interfaces.configanalyzer.packager.CompressedFileConfigurationPackager#compressFiles(java.io.File, java.util.List, java.io.File)
 *//*  ww  w .  jav a 2  s . com*/
@Override
protected void compressFiles(File actualRootFolder, List<String> entries, File zipFile)
        throws FileNotFoundException, IOException {
    //We use the FileUtils method so that necessary directories are automatically created.
    FileOutputStream zipFOS = FileUtils.openOutputStream(zipFile);
    ZipOutputStream zos = new ZipOutputStream(zipFOS);
    for (String entry : entries) {
        ZipEntry ze = new ZipEntry(entry);
        zos.putNextEntry(ze);
        FileInputStream sourceFileIN = new FileInputStream(
                actualRootFolder.getAbsolutePath() + File.separator + entry);
        IOUtils.copy(sourceFileIN, zos);
        sourceFileIN.close();
    }
    zos.closeEntry();
    zos.close();
}

From source file:com.taobao.android.builder.tools.zip.ZipUtils.java

public static boolean removeZipEntry(File file, Pattern pattern, File targetFile)
        throws FileNotFoundException, IOException {
    byte[] buffer = new byte[1024];
    java.util.zip.ZipFile zipFile = new java.util.zip.ZipFile(file);
    ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(targetFile)));
    BufferedOutputStream bo = new BufferedOutputStream(out);
    InputStream inputStream;/*from   ww w. j a  v  a 2  s .c  om*/
    Enumeration enumeration = zipFile.entries();
    while (enumeration.hasMoreElements()) {
        ZipEntry zipEntry = (ZipEntry) enumeration.nextElement();
        String name = zipEntry.getName();
        if (pattern.matcher(name).find()) {
            continue;
        }
        out.putNextEntry(zipEntry);
        inputStream = zipFile.getInputStream(zipEntry);
        write(inputStream, out, buffer);
        bo.flush();

    }

    closeQuitely(zipFile);
    closeQuitely(out);
    closeQuitely(bo);

    return true;
}

From source file:it.geosolutions.tools.compress.file.Compressor.java

/**
 * This function zip the input directory.
 * /*from   ww  w .  j a va2  s  . c  o m*/
 * @param directory
 *            The directory to be zipped.
 * @param base
 *            The base directory.
 * @param out
 *            The zip output stream.
 * @throws IOException
 */
public static void zipDirectory(final File directory, final File base, final ZipOutputStream out)
        throws IOException {

    if (directory != null && base != null && out != null) {
        File[] files = directory.listFiles();
        byte[] buffer = new byte[4096];
        int read = 0;

        FileInputStream in = null;
        ZipEntry entry = null;

        try {
            for (int i = 0, n = files.length; i < n; i++) {
                if (files[i].isDirectory()) {
                    zipDirectory(files[i], base, out);
                } else {
                    in = new FileInputStream(files[i]);
                    entry = new ZipEntry(base.getName().concat("\\")
                            .concat(files[i].getPath().substring(base.getPath().length() + 1)));
                    out.putNextEntry(entry);

                    while (-1 != (read = in.read(buffer))) {
                        out.write(buffer, 0, read);
                    }

                    // //////////////////////
                    // Complete the entry
                    // //////////////////////

                    out.closeEntry();

                    in.close();
                    in = null;
                }
            }

        } catch (IOException e) {
            if (LOGGER.isErrorEnabled())
                LOGGER.error(e.getLocalizedMessage(), e);
            if (out != null)
                out.close();
        } finally {
            if (in != null)
                in.close();
        }

    } else
        throw new IOException("One or more input parameters are null!");
}

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

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

    try {/* www. j  av  a2s  .  co  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:ZipUtilInPlaceTest.java

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

    File file1 = File.createTempFile("temp", null);
    try {//from ww  w .j  a v  a  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 ByteArrayZipEntryTransformer() {
            protected byte[] transform(ZipEntry zipEntry, byte[] input) throws IOException {
                String s = new String(input);
                assertEquals(new String(contents), s);
                return s.toUpperCase().getBytes();
            }
        });

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

From source file:com.ftb2om2.util.Zipper.java

private void addToZip(String path, String name, ZipOutputStream zos) throws IOException {
    File file = new File(path);
    FileInputStream fis = new FileInputStream(file);
    ZipEntry zipEntry = new ZipEntry(name);
    zos.putNextEntry(zipEntry);

    byte[] bytes = new byte[1024];
    int length;//from  ww w.  jav  a  2s  .co  m
    while ((length = fis.read(bytes)) >= 0) {
        zos.write(bytes, 0, length);
    }
    zos.closeEntry();
    fis.close();
}