Example usage for java.util.zip ZipInputStream closeEntry

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

Introduction

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

Prototype

public void closeEntry() throws IOException 

Source Link

Document

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

Usage

From source file:com.microsoft.azure.hdinsight.util.HDInsightJobViewUtils.java

public static void unzip(String zipFilePath, String destDirectory) throws IOException {
    File destDir = new File(destDirectory);
    if (!destDir.exists()) {
        destDir.mkdir();//from   w  w  w  .  j av a2  s  .  c o m
    }
    ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFilePath));
    ZipEntry entry = zipIn.getNextEntry();
    while (entry != null) {
        String filePath = destDirectory + File.separator + entry.getName();
        if (!entry.isDirectory()) {
            extractFile(zipIn, filePath);
        } else {
            File dir = new File(filePath);
            dir.mkdir();
        }
        zipIn.closeEntry();
        entry = zipIn.getNextEntry();
    }
    zipIn.close();
}

From source file:eu.openanalytics.rsb.message.MultiFilesJob.java

/**
 * Adds all the files contained in a Zip archive to a job. Rejects Zips that
 * contain sub-directories./*from  ww w  . jav  a2  s  .c  o m*/
 * 
 * @param data
 * @param job
 * @throws IOException
 */
public static void addZipFilesToJob(final InputStream data, final MultiFilesJob job) throws IOException {
    final ZipInputStream zis = new ZipInputStream(data);
    ZipEntry ze = null;

    while ((ze = zis.getNextEntry()) != null) {
        if (ze.isDirectory()) {
            job.destroy();
            throw new IllegalArgumentException("Invalid zip archive: nested directories are not supported");
        }
        job.addFile(ze.getName(), zis);
        zis.closeEntry();
    }

    IOUtils.closeQuietly(zis);
}

From source file:de.micromata.genome.gdbfs.FileSystemUtils.java

/**
 * Copy from zip.//from  w w  w . j  a  v  a  2s. c  o m
 *
 * @param zipIn the zip in
 * @param target the target
 */
public static void copyFromZip(InputStream zipIn, FsObject target) {
    ZipInputStream zin = new ZipInputStream(zipIn);
    try {
        while (true) {
            ZipEntry ze = zin.getNextEntry();
            if (ze == null) {
                break;
            }
            copyFromZip(ze, zin, target);
            zin.closeEntry();
        }
    } catch (IOException ex) {
        throw new RuntimeIOException(ex);
    }
}

From source file:org.craftercms.commons.zip.ZipUtils.java

/**
 * Unzips a zip from an input stream into an output folder.
 *
 * @param inputStream  the zip input stream
 * @param outputFolder the output folder where the files
 * @throws IOException/*from w  w  w  . j  ava2  s  .co m*/
 */
public static void unZipFiles(InputStream inputStream, File outputFolder) throws IOException {
    ZipInputStream zis = new ZipInputStream(inputStream);
    ZipEntry ze = zis.getNextEntry();

    while (ze != null) {
        File file = new File(outputFolder, ze.getName());
        OutputStream os = new BufferedOutputStream(FileUtils.openOutputStream(file));

        try {
            IOUtils.copy(zis, os);
        } finally {
            IOUtils.closeQuietly(os);
        }

        zis.closeEntry();
        ze = zis.getNextEntry();
    }
}

From source file:com.joliciel.jochre.lexicon.TextFileLexicon.java

public static TextFileLexicon deserialize(ZipInputStream zis) {
    TextFileLexicon memoryBase = null;//  w  w w  .jav  a 2  s .co m
    try {
        ZipEntry zipEntry;
        if ((zipEntry = zis.getNextEntry()) != null) {
            LOG.debug("Scanning zip entry " + zipEntry.getName());

            ObjectInputStream in = new ObjectInputStream(zis);
            memoryBase = (TextFileLexicon) in.readObject();
            zis.closeEntry();
            in.close();
        } else {
            throw new RuntimeException("No zip entry in input stream");
        }
    } catch (IOException ioe) {
        throw new RuntimeException(ioe);
    } catch (ClassNotFoundException cnfe) {
        throw new RuntimeException(cnfe);
    }

    return memoryBase;
}

From source file:org.yccheok.jstock.file.Utils.java

/**
 * Performs close operation on ZIP input stream, without the need of
 * writing cumbersome try...catch block.
 *
 * @param zipInputStream The ZIP input stream.
 *///from w  ww  .j av  a 2  s . c om
public static void closeEntry(ZipInputStream zipInputStream) {
    // Instead of returning boolean, we will just simply swallow any
    // exception silently. This is because this method will usually be
    // invoked within finally block. If we are having control statement
    // (return, break, continue) within finally block, a lot of surprise may
    // happen.
    // http://stackoverflow.com/questions/48088/returning-from-a-finally-block-in-java
    if (null != zipInputStream) {
        try {
            zipInputStream.closeEntry();
        } catch (IOException ex) {
            log.error(null, ex);
        }
    }
}

From source file:com.google.gdt.eclipse.designer.gwtext.actions.ConfigureGwtExtOperation.java

private static void extractZip(InputStream zipFile, IFolder targetFolder) throws Exception {
    ZipInputStream zipInputStream = new ZipInputStream(zipFile);
    while (true) {
        ZipEntry zipEntry = zipInputStream.getNextEntry();
        if (zipEntry == null) {
            break;
        }//from ww  w  .j  a  v  a2s.  co m
        if (!zipEntry.isDirectory()) {
            String entryName = zipEntry.getName();
            byte[] byteArray = IOUtils.toByteArray(zipInputStream);
            IOUtils2.setFileContents(targetFolder.getFile(new Path(entryName)),
                    new ByteArrayInputStream(byteArray));
        }
        zipInputStream.closeEntry();
    }
    zipInputStream.close();
}

From source file:org.asciidoctor.gradle.ZipUtils.java

/**
 * Unzips a file from an input stream to a location specified by the ZipFileFactory interface.
 * @param zip/*from  w  ww .  jav a2 s. c  om*/
 * @param zipFileFactory
 * @throws IOException
 */
static void unzip(InputStream zip, ZipFileFactory zipFileFactory) throws IOException {
    ZipInputStream zis = new ZipInputStream(new BufferedInputStream(zip));
    ZipEntry zipEntry = zis.getNextEntry();

    while (zipEntry != null) {

        File unzippedFile = zipFileFactory.createUnzippedFile(zipEntry);

        if (zipEntry.isDirectory()) {
            unzippedFile.mkdirs();
        } else {
            unzippedFile.getParentFile().mkdirs();

            FileOutputStream fos = new FileOutputStream(unzippedFile);

            IOUtils.copy(zis, fos);
        }
        zipEntry = zis.getNextEntry();
    }

    zis.closeEntry();
    zis.close();
}

From source file:Main.java

/**
 * Reads the in_stream and extracts them to out_dir.
 * @param in_stream Input stream corresponding to the zip file.
 * @param out_dir Output directory for the zip file contents.
 * @throws IOException/* ww  w . ja v  a2s.com*/
 */
public static void zipExtract(InputStream in_stream, File out_dir) throws IOException {
    if (!out_dir.exists()) {
        if (!out_dir.mkdirs()) {
            throw new IOException("Could not create output directory: " + out_dir.getAbsolutePath());
        }
    }
    ZipInputStream zis = new ZipInputStream(in_stream);
    ZipEntry ze;
    byte[] buffer = new byte[BUFFER_SIZE];
    int count;
    while ((ze = zis.getNextEntry()) != null) {
        if (ze.isDirectory()) {
            File fmd = new File(out_dir.getAbsolutePath() + "/" + ze.getName());
            fmd.mkdirs();
            continue;
        }
        FileOutputStream fout = new FileOutputStream(out_dir.getAbsolutePath() + "/" + ze.getName());
        while ((count = zis.read(buffer)) != -1) {
            fout.write(buffer, 0, count);
        }

        fout.close();
        zis.closeEntry();
    }
    zis.close();
}

From source file:Utils.java

/**
 * unpack a segment from a zip//w  w w  .  ja v a  2  s.  com
 * 
 * @param addsi
 * @param packetStream
 * @param version
 */
public static void unpack(InputStream source, File destination) throws IOException {
    ZipInputStream zin = new ZipInputStream(source);
    ZipEntry zipEntry = null;
    FileOutputStream fout = null;

    byte[] buffer = new byte[4096];
    while ((zipEntry = zin.getNextEntry()) != null) {

        long ts = zipEntry.getTime();
        // the zip entry needs to be a full path from the
        // searchIndexDirectory... hence this is correct

        File f = new File(destination, zipEntry.getName());

        f.getParentFile().mkdirs();

        fout = new FileOutputStream(f);
        int len;
        while ((len = zin.read(buffer)) > 0) {
            fout.write(buffer, 0, len);
        }
        zin.closeEntry();
        fout.close();
        f.setLastModified(ts);
    }
    fout.close();
}