Example usage for java.util.zip ZipInputStream close

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

Introduction

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

Prototype

public void close() throws IOException 

Source Link

Document

Closes this input stream and releases any system resources associated with the stream.

Usage

From source file:org.apache.druid.java.util.common.CompressionUtils.java

/**
 * Decompress an input stream from a file, based on the filename.
 *///from w  ww  .  j  ava 2 s. com
public static InputStream decompress(final InputStream in, final String fileName) throws IOException {
    if (fileName.endsWith(GZ_SUFFIX)) {
        return gzipInputStream(in);
    } else if (fileName.endsWith(BZ2_SUFFIX)) {
        return new BZip2CompressorInputStream(in, true);
    } else if (fileName.endsWith(XZ_SUFFIX)) {
        return new XZCompressorInputStream(in, true);
    } else if (fileName.endsWith(SNAPPY_SUFFIX)) {
        return new FramedSnappyCompressorInputStream(in);
    } else if (fileName.endsWith(ZSTD_SUFFIX)) {
        return new ZstdCompressorInputStream(in);
    } else if (fileName.endsWith(ZIP_SUFFIX)) {
        // This reads the first file in the archive.
        final ZipInputStream zipIn = new ZipInputStream(in, StandardCharsets.UTF_8);
        try {
            final ZipEntry nextEntry = zipIn.getNextEntry();
            if (nextEntry == null) {
                zipIn.close();

                // No files in the archive - return an empty stream.
                return new ByteArrayInputStream(new byte[0]);
            }
            return zipIn;
        } catch (IOException e) {
            try {
                zipIn.close();
            } catch (IOException e2) {
                e.addSuppressed(e2);
            }
            throw e;
        }
    } else {
        return in;
    }
}

From source file:Main.java

public static void Unzip(String dir, byte[] data) throws Exception {
    ByteArrayInputStream input = new ByteArrayInputStream(data);
    ZipInputStream zipIn = new ZipInputStream(input);
    ZipEntry entry;//  ww  w .  j av  a 2  s .  c o m
    while ((entry = zipIn.getNextEntry()) != null) {
        String outpath = dir + entry.getName();
        FileOutputStream output = null;
        try {
            File f = new File(outpath);
            f.getParentFile().mkdirs();

            output = new FileOutputStream(f);
            int len = 0;
            byte[] buffer = new byte[4096];
            while ((len = zipIn.read(buffer)) > 0) {
                output.write(buffer, 0, len);
            }
        } finally {
            if (output != null)
                output.close();
        }
    }

    zipIn.close();
}

From source file:Main.java

/**
 * /*from  w ww .jav  a  2  s.  c o m*/
 * @param zipFile
 *            zip file
 * @param folderName
 *            folder to load
 * @return list of entry in the folder
 * @throws ZipException
 * @throws IOException
 */
public static List<ZipEntry> loadFiles(File zipFile, String folderName) throws ZipException, IOException {
    Log.d("loadFiles", folderName);
    long start = System.currentTimeMillis();
    FileInputStream fis = new FileInputStream(zipFile);
    BufferedInputStream bis = new BufferedInputStream(fis);
    ZipInputStream zis = new ZipInputStream(bis);

    ZipEntry zipEntry = null;
    List<ZipEntry> list = new LinkedList<ZipEntry>();
    String folderLowerCase = folderName.toLowerCase();
    int counter = 0;
    while ((zipEntry = zis.getNextEntry()) != null) {
        counter++;
        String zipEntryName = zipEntry.getName();
        Log.d("loadFiles.zipEntry.getName()", zipEntryName);
        if (zipEntryName.toLowerCase().startsWith(folderLowerCase) && !zipEntry.isDirectory()) {
            list.add(zipEntry);
        }
    }
    Log.d("Loaded 1", counter + " files, took: " + (System.currentTimeMillis() - start) + " milliseconds.");
    zis.close();
    bis.close();
    fis.close();
    return list;
}

From source file:com.alibaba.antx.util.ZipUtil.java

/**
 * zip/*from   ww  w . j a v  a2s.  com*/
 *
 * @param istream   ?
 * @param todir     
 * @param overwrite ?
 * @throws IOException Zip?
 */
public static void expandFile(InputStream istream, File todir, boolean overwrite) throws IOException {
    ZipInputStream zipStream = null;

    if (!(istream instanceof BufferedInputStream)) {
        istream = new BufferedInputStream(istream, 8192);
    }

    try {
        zipStream = new ZipInputStream(istream);

        ZipEntry zipEntry = null;

        while ((zipEntry = zipStream.getNextEntry()) != null) {
            extractFile(todir, zipStream, zipEntry, overwrite);
        }

        log.info("expand complete");
    } finally {
        if (zipStream != null) {
            try {
                zipStream.close();
            } catch (IOException e) {
            }
        }
    }
}

From source file:nl.surfsara.warcexamples.hadoop.ldps.LDPRMapper.java

public static void unZip(InputStream zippedIS, File outputFolder) throws IOException {
    byte[] buffer = new byte[1024];

    ZipInputStream zis = new ZipInputStream(zippedIS);
    ZipEntry ze = zis.getNextEntry();

    while (ze != null) {
        String fileName = ze.getName();
        File newFile = new File(outputFolder + File.separator + fileName);
        new File(newFile.getParent()).mkdirs();
        FileOutputStream fos = new FileOutputStream(newFile);
        int len;//from   ww w .  j a va  2  s. c o m
        while ((len = zis.read(buffer)) > 0) {
            fos.write(buffer, 0, len);
        }
        fos.close();
        ze = zis.getNextEntry();
    }

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

From source file:org.apache.jackrabbit.oak.upgrade.cli.Util.java

public static void unzip(InputStream is, File targetDir) throws IOException {
    long start = System.currentTimeMillis();
    log.info("Unzipping to {}", targetDir.getAbsolutePath());
    final ZipInputStream zis = new ZipInputStream(is);
    try {//from w  w w  . ja v  a 2 s .  co  m
        ZipEntry entry = null;
        while ((entry = zis.getNextEntry()) != null) {
            if (entry.isDirectory()) {
                new File(targetDir, entry.getName()).mkdirs();
            } else {
                File target = new File(targetDir, entry.getName());
                target.getParentFile().mkdirs();
                OutputStream output = new FileOutputStream(target);
                try {
                    IOUtils.copy(zis, output);
                } finally {
                    output.close();
                }
            }
        }
    } finally {
        zis.close();
    }
    final long delta = System.currentTimeMillis() - start;
    if (delta > 1000L) {
        log.info("Unzip took {} msec", delta);
    }
}

From source file:com.comcast.magicwand.spells.web.iexplore.IePhoenixDriver.java

private static void extract(File src, String dstDir) throws IOException {
    ZipInputStream zis = null;
    ZipEntry entry;/*from   ww  w.  j  ava 2 s.  c om*/

    try {
        zis = new ZipInputStream(new BufferedInputStream(new FileInputStream(src)));

        while (null != (entry = zis.getNextEntry())) {
            File dst = Paths.get(dstDir, entry.getName()).toFile();
            FileOutputStream fos = new FileOutputStream(dst);

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

        zis.close();
    } finally {
        IOUtils.closeQuietly(zis);
    }
}

From source file:net.firejack.platform.core.utils.ArchiveUtils.java

/**
 * @param zip/*from   w w w  . j av a 2 s . c o  m*/
 * @param name
 * @return
 * @throws java.io.IOException
 */
public static InputStream getFile(InputStream zip, String name) throws IOException {
    ZipInputStream in = new ZipInputStream(zip);
    ZipEntry entry;
    while ((entry = in.getNextEntry()) != null) {
        String entityName = entry.getName();
        if (!entry.isDirectory() && entityName.contains(name)) {
            return in;
        }
    }
    in.close();
    return null;
}

From source file:com.magnet.tools.tests.MagnetToolStepDefs.java

@Then("^the package \"([^\"]*)\" should contain the following entries:$")
public static void package_should_contain(String packagePath, List<String> entries) throws Throwable {
    File file = new File(expandVariables(packagePath));
    Assert.assertTrue("File " + file + " should exist", file.exists());

    ZipInputStream zip = null;
    Set<String> actualSet;
    try {//from   w ww.  j a  va  2  s .c o  m
        FileInputStream fis = new FileInputStream(file);
        zip = new ZipInputStream(fis);
        ZipEntry ze;
        actualSet = new HashSet<String>();
        while ((ze = zip.getNextEntry()) != null) {
            actualSet.add(ze.getName());
        }
    } finally {
        if (null != zip) {
            zip.close();
        }
    }

    for (String e : entries) {
        String expected = expandVariables(e);
        Assert.assertTrue("File " + file + " should contain entry " + expected + ", actual set of entries are "
                + actualSet, actualSet.contains(e));
    }
}

From source file:Main.java

public static void unZip(InputStream zipFileIS, String outputFolder) throws IOException {

    byte[] buffer = new byte[1024];

    //create output directory is not exists
    File folder = new File(outputFolder);
    if (!folder.exists()) {
        folder.mkdir();//w w w  .j  a  v  a  2  s .c o m
    }

    //get the zip file content
    ZipInputStream zis = new ZipInputStream(zipFileIS);
    //get the zipped file list entry
    ZipEntry ze = zis.getNextEntry();

    while (ze != null) {

        String fileName = ze.getName();
        File newFile = new File(outputFolder + File.separator + fileName);

        if (ze.isDirectory()) {
            newFile.mkdirs();
        } else {

            FileOutputStream fos = new FileOutputStream(newFile);

            int len;
            while ((len = zis.read(buffer)) > 0) {
                fos.write(buffer, 0, len);
            }

            fos.flush();
            fos.close();
        }
        ze = zis.getNextEntry();
    }

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