Example usage for java.util.zip ZipFile ZipFile

List of usage examples for java.util.zip ZipFile ZipFile

Introduction

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

Prototype

public ZipFile(File file) throws ZipException, IOException 

Source Link

Document

Opens a ZIP file for reading given the specified File object.

Usage

From source file:com.denimgroup.threadfix.service.channel.AbstractChannelImporter.java

protected ZipFile unpackZipStream() {
    if (this.inputStream == null)
        return null;

    log.debug("Attempting to unpack the zip stream.");

    diskZipFile = new File("temp-zip-file");

    if (diskZipFile == null) {
        log.warn("The attempt to unpack the zip stream returned null.");
        return null;
    }/*w w w .  j av  a 2 s .com*/

    ZipFile zipFile = null;
    FileOutputStream out = null;
    try {

        out = new FileOutputStream(diskZipFile);
        byte buf[] = new byte[1024];
        int len = 0;

        while ((len = inputStream.read(buf)) > 0)
            out.write(buf, 0, len);

        zipFile = new ZipFile(diskZipFile);

        log.debug("Saved zip file to disk. Returning zip file.");
    } catch (ZipException e) {
        log.warn(
                "There was a ZipException while trying to save and open the file - probably not in a zip format.",
                e);
    } catch (IOException e) {
        log.warn("There was an IOException error in the unpackZipStream method: " + e + ".");
    } finally {
        closeInputStream(inputStream);
        if (out != null) {
            try {
                out.close();
            } catch (IOException ex) {
                log.warn("Closing an input stream failed.", ex);
            }
        }
    }

    return zipFile;
}

From source file:com.pari.nm.utils.backup.BackupRestore.java

public boolean unzip(File zipFile) {
    ZipFile zf = null;/*from w w  w .j  ava2  s  .c  o m*/
    FileOutputStream fout = null;
    // File zipFile = new File(localDir, zipFileName);

    try {
        zf = new ZipFile(zipFile);

        Enumeration en = zf.entries();

        while (en.hasMoreElements()) {
            ZipEntry ze = (ZipEntry) en.nextElement();
            String currentEntry = ze.getName();
            File destFile = new File(zipFile.getParent(), currentEntry);
            File destinationParent = destFile.getParentFile();
            if (!destinationParent.exists()) {
                destinationParent.mkdirs();
                destinationParent.setWritable(true, false);
            }
            System.err.println("ZIP ENTRY NAME:" + currentEntry);
            if (!ze.isDirectory()) {
                InputStream in = zf.getInputStream(ze);
                byte[] data = new byte[4096];
                int read = 0;

                File extractFile = new File(zipFile.getParent(), ze.getName());
                fout = new FileOutputStream(extractFile);

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

                fout.close();
            }
        }
        return true;
    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {
        try {
            if (zf != null) {
                zf.close();
            }
        } catch (Exception ex) {
        }

        try {
            if (fout != null) {
                fout.close();
            }
        } catch (Exception ex) {
        }
    }

    return false;
}