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.cloudfoundry.tools.io.zip.ZipArchive.java

/**
 * Unzip the specified input stream into a folder.
 * //w w  w .j  a v a  2s  . com
 * @param inputStream the input stream to unzip (this must contain zip contents)
 * @param destination the destination folder
 * @see #unpack(File, Folder)
 */
public static void unpack(InputStream inputStream, Folder destination) {
    Assert.notNull(inputStream, "InputStream must not be null");
    Assert.notNull(destination, "Destination must not be null");
    destination.createIfMissing();
    ZipInputStream zip = new ZipInputStream(new BufferedInputStream(inputStream));
    try {
        InputStream noCloseZip = new NoCloseInputStream(zip);
        ZipEntry entry = zip.getNextEntry();
        while (entry != null) {
            if (entry.isDirectory()) {
                destination.getFolder(entry.getName()).createIfMissing();
            } else {
                destination.getFile(entry.getName()).getContent().write(noCloseZip);
            }
            entry = zip.getNextEntry();
        }
    } catch (IOException e) {
        throw new ResourceException(e);
    } finally {
        try {
            zip.close();
        } catch (IOException e) {
        }
    }
}

From source file:io.vertx.config.vault.utils.VaultDownloader.java

private static void unzip(File zipFilePath, File destDir) throws IOException {
    if (!destDir.exists()) {
        destDir.mkdir();/*from  www  . ja  v  a  2 s. c  o  m*/
    }
    ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFilePath));
    ZipEntry entry = zipIn.getNextEntry();
    // iterates over entries in the zip file
    while (entry != null) {
        String filePath = destDir.getAbsolutePath() + File.separator + entry.getName();
        if (!entry.isDirectory()) {
            // if the entry is a file, extracts it
            extractFile(zipIn, filePath);
        } else {
            // if the entry is a directory, make the directory
            File dir = new File(filePath);
            dir.mkdir();
        }
        zipIn.closeEntry();
        entry = zipIn.getNextEntry();
    }
    zipIn.close();
}

From source file:org.icefaces.sample.location.CityDictionary.java

/**
 * Reads the zipped xml city cityDictionary and loads it into memory.
 *
 * @throws java.io.IOException If anything goes wrong acquiring or readying the zip file of city data.
 *///from   w w  w .j a  v  a 2s .  c o m
private static void init() throws IOException {

    if (!initialized) {

        initialized = true;

        // Loading of the resource must be done the "JSF way" so that
        // it is agnostic about it's environment (portlet vs servlet).
        // First we get the resource as an InputStream
        FacesContext fc = FacesContext.getCurrentInstance();
        ExternalContext ec = fc.getExternalContext();
        InputStream is = ec.getResourceAsStream(DATA_RESOURCE_PATH);

        //is a zip file.
        ZipInputStream zipStream = new ZipInputStream(is);

        //Prime the stream by reading the first entry.  The way
        //we have it currently configured, there should only be
        //one.
        ZipEntry firstEntry = zipStream.getNextEntry();

        //Pass the ZipInputStream to the XMLDecoder so that it
        //can read in the list of cities and associated data.
        XMLDecoder xDecoder = new XMLDecoder(zipStream);
        List cityList = (List) xDecoder.readObject();

        //Close the decoder and the stream.
        xDecoder.close();
        zipStream.close();

        if (cityList == null) {
            throw new IOException();
        }

        cityDictionary = new ArrayList(cityList.size());
        City tmpCity = null;
        for (int i = 0, max = cityList.size(); i < max; i++) {
            tmpCity = (City) cityList.get(i);
            if (tmpCity != null && tmpCity.getCity() != null) {
                cityDictionary.add(new SelectItem(tmpCity, tmpCity.getCity()));
            }
        }
        cityList.clear();
        Collections.sort(cityDictionary, LABEL_COMPARATOR);
    }

}

From source file:org.icefaces.application.showcase.view.bean.examples.component.selectInputText.CityDictionary.java

/**
 * Reads the zipped xml city cityDictionary and loads it into memory.
 *//*from   w  w w.j a  v a 2s  .  c om*/
private static void init() throws IOException {

    if (!initialized) {

        initialized = true;

        // Loading of the resource must be done the "JSF way" so that
        // it is agnostic about it's environment (portlet vs servlet).
        // First we get the resource as an InputStream
        FacesContext fc = FacesContext.getCurrentInstance();
        ExternalContext ec = fc.getExternalContext();
        InputStream is = ec.getResourceAsStream(DATA_RESOURCE_PATH);

        //Wrap the InputStream as a ZipInputStream since it
        //is a zip file.
        ZipInputStream zipStream = new ZipInputStream(is);

        //Prime the stream by reading the first entry.  The way
        //we have it currently configured, there should only be
        //one.
        ZipEntry firstEntry = zipStream.getNextEntry();

        //Pass the ZipInputStream to the XMLDecoder so that it
        //can read in the list of cities and associated data.
        XMLDecoder xDecoder = new XMLDecoder(zipStream);
        List cityList = (List) xDecoder.readObject();

        //Close the decoder and the stream.
        xDecoder.close();
        zipStream.close();

        if (cityList == null) {
            throw new IOException();
        }

        cityDictionary = new ArrayList(cityList.size());
        City tmpCity;
        for (int i = 0, max = cityList.size(); i < max; i++) {
            tmpCity = (City) cityList.get(i);
            if (tmpCity != null && tmpCity.getCity() != null) {
                cityDictionary.add(new SelectItem(tmpCity, tmpCity.getCity()));
            }
        }
        cityList.clear();

        Collections.sort(cityDictionary, LABEL_COMPARATOR);
    }

}

From source file:org.jahia.utils.zip.ZipEntryCharsetDetector.java

private static boolean canRead(InputStream is, Charset charset) throws IOException {
    boolean canRead = true;
    ZipInputStream zis = null;
    try {/*from  w  w w  . java  2  s.  c o m*/
        zis = charset != null ? new ZipInputStream(is, charset) : new ZipInputStream(is);
        try {
            ZipEntry entry = zis.getNextEntry();
            while (entry != null) {
                try {
                    entry = zis.getNextEntry();
                } finally {

                }
            }
        } catch (IllegalArgumentException e) {
            canRead = false;
        }
    } finally {
        if (zis != null) {
            try {
                zis.close();
            } catch (IOException e) {
                // ignore
            }
        }
    }

    return canRead;
}

From source file:Main.java

private static void loadICUData(Context context, File destDir) throws IOException {
    OutputStream out = null;//  ww w.  j  a v  a2  s  .c om
    ZipInputStream in = null;
    File icuDir = new File(destDir, "icu");
    File icuDataFile = new File(icuDir, "icudt53l.dat");
    try {
        if (!icuDir.exists())
            icuDir.mkdirs();
        if (!icuDataFile.exists()) {
            in = new ZipInputStream(context.getAssets().open("icudt53l.zip"));
            in.getNextEntry();
            out = new FileOutputStream(icuDataFile);
            byte[] buf = new byte[1024];
            int len;
            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
        }
    } catch (IOException e) {
        if (icuDataFile.exists())
            icuDataFile.delete();
        throw e;
    } finally {
        if (in != null)
            in.close();
        if (out != null) {
            out.flush();
            out.close();
        }
    }
}

From source file:Main.java

/***
 * Extract zipfile to outdir with complete directory structure
 * @param zipfile Input .zip file/* w  w w.j  av a2 s. com*/
 * @param outdir Output directory
 */
public static void extract(InputStream zipfile, File outdir) {
    try {
        ZipInputStream zin = new ZipInputStream(zipfile);
        ZipEntry entry;
        String name, dir;
        Log.i("OF", "uncompressinggggg ");
        while ((entry = zin.getNextEntry()) != null) {
            name = entry.getName();
            if (entry.isDirectory()) {
                mkdirs(outdir, name);
                continue;
            }
            /* this part is necessary because file entry can come before
             * directory entry where is file located
             * i.e.:
             *   /foo/foo.txt
             *   /foo/
             */
            dir = dirpart(name);
            if (dir != null)
                mkdirs(outdir, dir);

            extractFile(zin, outdir, name);
        }
        zin.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void unZip(String zipFile, String outputFolder) {
    byte[] buffer = new byte[BUFFER_SIZE];

    try {//from w  w w.j av  a2 s  . co m
        File folder = new File(outputFolder);
        if (!folder.exists()) {
            folder.mkdir();
        }

        ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile));
        ZipEntry ze = zis.getNextEntry();

        while (ze != null) {
            String fileName = ze.getName();
            File newFile = new File(outputFolder + File.separator + fileName);

            System.out.println("file unzip : " + newFile.getAbsoluteFile());
            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.close();
            }
            ze = zis.getNextEntry();
        }
        zis.closeEntry();
        zis.close();
        System.out.println("Done");
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

From source file:com.eviware.soapui.integration.exporter.ProjectExporter.java

public static void unpackageAll(String archive, String path) {
    try {/*  w w w.ja  v a  2  s .c o m*/
        BufferedOutputStream dest = null;
        FileInputStream fis = new FileInputStream(archive);
        ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));
        ZipEntry entry;
        while ((entry = zis.getNextEntry()) != null) {
            int count;
            byte data[] = new byte[BUFFER];
            // write the files to the disk
            FileOutputStream fos = new FileOutputStream(path + File.separator + entry.getName());
            dest = new BufferedOutputStream(fos, BUFFER);
            while ((count = zis.read(data, 0, BUFFER)) != -1) {
                dest.write(data, 0, count);
            }
            dest.flush();
            dest.close();
        }
        zis.close();
    } catch (Exception e) {
        SoapUI.logError(e);
    }
}

From source file:com.pari.mw.api.execute.reports.template.ReportTemplateRunner.java

public static File getTopLevelFolder(File zipFile) throws IOException {
    ZipInputStream in = null;
    try {// w w  w  .ja va2s .co  m
        in = new ZipInputStream(new FileInputStream(zipFile));
        ZipEntry entry = null;

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

            if (entry.isDirectory()) {
                return new File(outFilename);
            }
        }
    } finally {
        if (in != null) {
            in.close();
        }
    }
    return null;
}