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:com.taobao.android.tpatch.utils.JarSplitUtils.java

/**
 * ?jarentry// ww w . j av a  2 s.c  om
 *
 * @param jarFile
 * @return
 */
public static List<String> getZipEntries(File jarFile) throws IOException {
    List<String> entries = new ArrayList<String>();
    FileInputStream fis = new FileInputStream(jarFile);
    ZipInputStream zis = new ZipInputStream(fis);
    try {
        // loop on the entries of the jar file package and put them in the final jar
        ZipEntry entry;
        while ((entry = zis.getNextEntry()) != null) {
            // do not take directories or anything inside a potential META-INF folder.
            if (entry.isDirectory()) {
                continue;
            }
            String name = entry.getName();
            entries.add(name);
        }
    } finally {
        zis.close();
    }
    return entries;
}

From source file:ZipFileIO.java

/**
 * Extract zip file to destination folder.
 * /*from   w  ww.j  a va  2 s . co m*/
 * @param file
 *            zip file to extract
 * @param destination
 *            destinatin folder
 */
public static void extract(File file, File destination) throws IOException {
    ZipInputStream in = null;
    OutputStream out = null;
    try {
        // Open the ZIP file
        in = new ZipInputStream(new FileInputStream(file));

        // Get the first entry
        ZipEntry entry = null;

        while ((entry = in.getNextEntry()) != null) {
            String outFilename = entry.getName();

            // Open the output file
            if (entry.isDirectory()) {
                new File(destination, outFilename).mkdirs();
            } else {
                out = new FileOutputStream(new File(destination, outFilename));

                // Transfer bytes from the ZIP file to the output file
                byte[] buf = new byte[1024];
                int len;

                while ((len = in.read(buf)) > 0) {
                    out.write(buf, 0, len);
                }

                // Close the stream
                out.close();
            }
        }
    } finally {
        // Close the stream
        if (in != null) {
            in.close();
        }
        if (out != null) {
            out.close();
        }
    }
}

From source file:com.openkm.misc.ZipTest.java

public void testJava() throws IOException {
    log.debug("testJava()");
    File zip = File.createTempFile("java_", ".zip");

    // Create zip
    FileOutputStream fos = new FileOutputStream(zip);
    ZipOutputStream zos = new ZipOutputStream(fos);
    zos.putNextEntry(new ZipEntry("coeta"));
    zos.closeEntry();/*from w  w w.  j a  va  2s.c o  m*/
    zos.close();

    // Read zip
    FileInputStream fis = new FileInputStream(zip);
    ZipInputStream zis = new ZipInputStream(fis);
    ZipEntry ze = zis.getNextEntry();
    System.out.println(ze.getName());
    assertEquals(ze.getName(), "coeta");
    zis.close();
}

From source file:org.hawkular.wildfly.module.installer.JBossModule.java

public static JBossModule readFromURL(URL moduleZip) throws Exception {
    JBossModule m = new JBossModule();
    m.root = moduleZip;//from   www  .j a  va 2s .c o m

    ZipInputStream zin = null;
    BufferedInputStream bin = null;
    boolean moduleXmlFound = false;
    try {
        bin = new BufferedInputStream(moduleZip.openStream());
        zin = new ZipInputStream(bin);
        ZipEntry ze = null;
        while ((ze = zin.getNextEntry()) != null) {
            if (ze.getName().endsWith("main/module.xml")) {
                moduleXmlFound = true;
                DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
                DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
                Document doc = dBuilder.parse(zin);
                m.readModuleXmlInfo(doc);
                break;
            }
        }
    } finally {
        try {
            // it should be enough to close the latest created stream in
            // case of chained (nested) streams, @see
            // http://www.javapractices.com/topic/TopicAction.do?Id=8
            if (zin != null) {
                zin.close();
            }
        } catch (IOException ex) {
        }
    }
    if (!moduleXmlFound) {
        throw new FileNotFoundException("module.xml was not found in " + moduleZip);
    }
    return m;
}

From source file:org.gatein.wcm.util.FileAux.java

public static void unzip(ZipInputStream zis, File output) throws IOException {
    if (zis == null || output == null)
        return;/*from   w  ww  .  ja v  a2  s  .  c om*/
    ZipEntry entry;
    OutputStream os = null;
    try {
        while ((entry = zis.getNextEntry()) != null) {
            File entryFile = new File(output, entry.getName());
            if (entry.isDirectory()) {
                if (!entryFile.exists()) {
                    entryFile.mkdirs();
                }
            } else {
                if (entryFile.getParentFile() != null && !entryFile.getParentFile().exists()) {
                    entryFile.getParentFile().mkdirs();
                }
                if (!entryFile.exists()) {
                    entryFile.createNewFile();
                }
                os = new FileOutputStream(entryFile);
                IOUtils.copy(zis, os);
                os.close();
            }
        }
        zis.close();
    } finally {
        IOUtils.closeQuietly(zis);
        IOUtils.closeQuietly(os);
    }
}

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

/**
 * @param zipFile/* www .  ja va  2 s .  c o m*/
 * @param files
 * @throws java.io.IOException
 */
public static void addFilesToZip(File zipFile, Map<String, File> files) throws IOException {
    // get a temp file
    File tempFile = File.createTempFile(zipFile.getName(), null);
    // delete it, otherwise you cannot rename your existing zip to it.
    FileUtils.deleteQuietly(tempFile);

    boolean renameOk = zipFile.renameTo(tempFile);
    if (!renameOk) {
        throw new RuntimeException(
                "could not rename the file " + zipFile.getAbsolutePath() + " to " + tempFile.getAbsolutePath());
    }

    ZipInputStream zin = new ZipInputStream(new FileInputStream(tempFile));
    ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFile));

    ZipEntry entry = zin.getNextEntry();
    while (entry != null) {
        String name = entry.getName();
        boolean notInFiles = true;
        for (Map.Entry<String, File> e : files.entrySet()) {
            if (e.getKey().equals(name)) {
                notInFiles = false;
                break;
            }
        }
        if (notInFiles) {
            out.putNextEntry(new ZipEntry(name));
            IOUtils.copy(zin, out);
        }
        entry = zin.getNextEntry();
    }
    // Close the streams
    zin.close();
    // Compress the files
    for (Map.Entry<String, File> e : files.entrySet()) {
        InputStream in = new FileInputStream(e.getValue());
        // Add ZIP entry to output stream.
        out.putNextEntry(new ZipEntry(e.getKey()));
        // Transfer bytes from the file to the ZIP file

        IOUtils.copy(in, out);
        // Complete the entry
        out.closeEntry();
        IOUtils.closeQuietly(in);
    }
    // Complete the ZIP file
    IOUtils.closeQuietly(out);
    FileUtils.deleteQuietly(tempFile);
}

From source file:net.sf.jasperreports.samples.wizards.SampleNewWizard.java

public static void unpackArchive(File theFile, File targetDir, IProgressMonitor monitor, Set<String> cpaths,
        Set<String> lpaths) {
    byte[] buffer = new byte[1024];
    ZipInputStream zis = null;
    try {/*from w  w  w . j  av  a  2 s.c om*/
        zis = new ZipInputStream(new FileInputStream(theFile));

        ZipEntry ze = zis.getNextEntry();
        while (ze != null) {
            File file = new File(targetDir, File.separator + ze.getName());

            new File(file.getParent()).mkdirs();
            String fname = file.getName();
            if (ze.isDirectory()) {
                if (fname.equals("src"))
                    cpaths.add(ze.getName());
            } else {
                FileOutputStream fos = new FileOutputStream(file);
                int len;
                while ((len = zis.read(buffer)) > 0)
                    fos.write(buffer, 0, len);

                fos.close();
            }
            if (file.getParentFile().getName().equals("lib")
                    && (fname.endsWith(".jar") || fname.endsWith(".zip")))
                lpaths.add(ze.getName());
            if (monitor.isCanceled()) {
                zis.close();
                return;
            }
            ze = zis.getNextEntry();
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            zis.closeEntry();
            zis.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:com.nuvolect.deepdive.probe.ApkZipUtil.java

public static boolean unzip(OmniFile zipOmni, OmniFile targetDir, ProgressStream progressStream) {

    String volumeId = zipOmni.getVolumeId();
    String rootFolderPath = targetDir.getPath();
    boolean DEBUG = true;

    ZipInputStream zis = new ZipInputStream(zipOmni.getFileInputStream());
    ZipEntry entry = null;/*from w w  w  . jav  a 2s .co  m*/
    try {
        while ((entry = zis.getNextEntry()) != null) {

            if (entry.isDirectory()) {

                OmniFile dir = new OmniFile(volumeId, entry.getName());
                if (dir.mkdir()) {

                    if (DEBUG)
                        LogUtil.log(LogUtil.LogType.OMNI_ZIP, "dir created: " + dir.getPath());
                }
            } else {

                String path = rootFolderPath + "/" + entry.getName();
                OmniFile file = new OmniFile(volumeId, path);

                // Create any necessary directories
                file.getParentFile().mkdirs();

                OutputStream out = file.getOutputStream();

                OmniFiles.copyFileLeaveInOpen(zis, out);
                if (DEBUG)
                    LogUtil.log(LogUtil.LogType.OMNI_ZIP, "file created: " + file.getPath());

                progressStream.putStream("Unpacked: " + entry.getName());
            }
        }
        zis.close();

    } catch (IOException e) {
        LogUtil.logException(LogUtil.LogType.OMNI_ZIP, e);
        return false;
    }

    return true;
}

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

/**
 * @param zipFile//  w w w .  j av a 2  s.c  om
 * @param files
 * @throws java.io.IOException
 */
public static void addStreamsToZip(File zipFile, Map<String, InputStream> files) throws IOException {
    // get a temp file
    File tempFile = File.createTempFile(zipFile.getName(), null);
    // delete it, otherwise you cannot rename your existing zip to it.
    FileUtils.deleteQuietly(tempFile);

    boolean renameOk = zipFile.renameTo(tempFile);
    if (!renameOk) {
        throw new RuntimeException(
                "could not rename the file " + zipFile.getAbsolutePath() + " to " + tempFile.getAbsolutePath());
    }

    ZipInputStream zin = new ZipInputStream(new FileInputStream(tempFile));
    ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFile));

    ZipEntry entry = zin.getNextEntry();
    while (entry != null) {
        String name = entry.getName();
        boolean notInFiles = true;
        for (Map.Entry<String, InputStream> e : files.entrySet()) {
            if (e.getKey().equals(name)) {
                notInFiles = false;
                break;
            }
        }
        if (notInFiles) {
            out.putNextEntry(new ZipEntry(name));
            IOUtils.copy(zin, out);
        }
        entry = zin.getNextEntry();
    }
    // Close the streams
    zin.close();
    // Compress the files
    for (Map.Entry<String, InputStream> e : files.entrySet()) {
        InputStream in = e.getValue();
        // Add ZIP entry to output stream.
        out.putNextEntry(new ZipEntry(e.getKey()));
        // Transfer bytes from the file to the ZIP file

        IOUtils.copy(in, out);
        // Complete the entry
        out.closeEntry();
        IOUtils.closeQuietly(in);
    }
    // Complete the ZIP file
    IOUtils.closeQuietly(out);
    FileUtils.deleteQuietly(tempFile);
}

From source file:org.apache.hadoop.hbase.util.MigrationTest.java

private void loadTestData(final FileSystem dfs, final Path rootDir) throws IOException {
    FileSystem localfs = FileSystem.getLocal(conf);
    // Get path for zip file.  If running this test in eclipse, define
    // the system property src.testdata for your test run.
    String srcTestdata = System.getProperty("src.testdata");
    if (srcTestdata == null) {
        throw new NullPointerException("Define src.test system property");
    }//  w  w  w. jav a  2s. com
    Path data = new Path(srcTestdata, "HADOOP-2478-testdata-v0.1.zip");
    if (!localfs.exists(data)) {
        throw new FileNotFoundException(data.toString());
    }
    FSDataInputStream hs = localfs.open(data);
    ZipInputStream zip = new ZipInputStream(hs);
    unzip(zip, dfs, rootDir);
    zip.close();
    hs.close();
}