Example usage for java.util.zip ZipFile close

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

Introduction

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

Prototype

public void close() throws IOException 

Source Link

Document

Closes the ZIP file.

Usage

From source file:com.icesoft.icefaces.tutorial.component.autocomplete.AutoCompleteDictionary.java

private static void init() {
    // Raw list of xml cities.
    List cityList = null;/*from  w w w .  j a  v a2 s  . com*/

    // load the city dictionary from the compressed xml file.

    // get the path of the compressed file
    HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
    String basePath = session.getServletContext().getRealPath("/WEB-INF/resources");
    basePath += "/city.xml.zip";

    // extract the file
    ZipEntry zipEntry;
    ZipFile zipFile;
    try {
        zipFile = new ZipFile(basePath);
        zipEntry = zipFile.getEntry("city.xml");
    } catch (Exception e) {
        log.error("Error retrieving records", e);
        return;
    }

    // get the xml stream and decode it.
    if (zipFile.size() > 0 && zipEntry != null) {
        try {
            BufferedInputStream dictionaryStream = new BufferedInputStream(zipFile.getInputStream(zipEntry));
            XMLDecoder xDecoder = new XMLDecoder(dictionaryStream);
            // get the city list.
            cityList = (List) xDecoder.readObject();
            dictionaryStream.close();
            zipFile.close();
            xDecoder.close();
        } catch (ArrayIndexOutOfBoundsException e) {
            log.error("Error getting city list, not city objects", e);
            return;
        } catch (IOException e) {
            log.error("Error getting city list", e);
            return;
        }
    }

    // Finally load the object from the xml file.
    if (cityList != null) {
        dictionary = 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) {
                dictionary.add(new SelectItem(tmpCity, tmpCity.getCity()));
            }
        }
        cityList.clear();
        // finally sort the list
        Collections.sort(dictionary, LABEL_COMPARATOR);
    }

}

From source file:Main.java

public static boolean upZipFile(File zipFile, String folderPath) throws ZipException, IOException {
    ZipFile zfile = new ZipFile(zipFile);
    Enumeration<? extends ZipEntry> zList = zfile.entries();
    ZipEntry ze = null;/*from  ww  w .  j  av  a  2 s .c o m*/
    byte[] buf = new byte[1024];
    while (zList.hasMoreElements()) {
        ze = (ZipEntry) zList.nextElement();
        if (ze.isDirectory()) {
            continue;
        }
        Log.d(TAG, "ze.getName() = " + ze.getName());
        OutputStream os = new BufferedOutputStream(
                new FileOutputStream(getRealFileName(folderPath, ze.getName())));
        InputStream is = new BufferedInputStream(zfile.getInputStream(ze));
        int readLen = 0;
        while ((readLen = is.read(buf, 0, 1024)) != -1) {
            os.write(buf, 0, readLen);
        }
        is.close();
        os.close();
    }
    zfile.close();
    return true;
}

From source file:nz.co.fortytwo.freeboard.installer.ZipUtils.java

/**
 * Unzip a zipFile into a directory//from   w w w.  j  av  a 2s .co  m
 * @param targetDir
 * @param zipFile
 * @throws ZipException
 * @throws IOException
 */
public static void unzip(File targetDir, File zipFile) throws ZipException, IOException {
    ZipFile zip = new ZipFile(zipFile);

    @SuppressWarnings("unchecked")
    Enumeration<ZipEntry> z = (Enumeration<ZipEntry>) zip.entries();
    while (z.hasMoreElements()) {
        ZipEntry entry = z.nextElement();
        File f = new File(targetDir, entry.getName());
        if (f.isDirectory()) {
            if (!f.exists()) {
                f.mkdirs();
            }
        } else {
            f.getParentFile().mkdirs();
            InputStream in = zip.getInputStream(entry);
            BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(f));
            IOUtils.copy(in, out);
            in.close();
            out.flush();
            out.close();
        }

    }
    zip.close();
}

From source file:com.predic8.membrane.examples.DistributionExtractingTestcase.java

public static final void unzip(File zip, File target) throws IOException {
    ZipFile zipFile = new ZipFile(zip);
    Enumeration<? extends ZipEntry> entries = zipFile.entries();
    while (entries.hasMoreElements()) {
        ZipEntry entry = entries.nextElement();
        if (entry.isDirectory()) {
            // Assume directories are stored parents first then children.
            // This is not robust, just for demonstration purposes.
            new File(target, entry.getName()).mkdir();
        } else {/*from   w  w  w.j av a2 s.c  o  m*/
            FileOutputStream fos = new FileOutputStream(new File(target, entry.getName()));
            try {
                copyInputStream(zipFile.getInputStream(entry), new BufferedOutputStream(fos));
            } finally {
                fos.close();
            }
        }
    }
    zipFile.close();
}

From source file:org.sonarsource.commandlinezip.ZipUtils6.java

public static File unzip(File zip, File toDir, ZipEntryFilter filter) throws IOException {
    if (!toDir.exists()) {
        FileUtils.forceMkdir(toDir);/*www.j  a va2  s  .  com*/
    }

    ZipFile zipFile = new ZipFile(zip);
    try {
        Enumeration<? extends ZipEntry> entries = zipFile.entries();
        while (entries.hasMoreElements()) {
            ZipEntry entry = entries.nextElement();
            if (filter.accept(entry)) {
                File to = new File(toDir, entry.getName());
                if (entry.isDirectory()) {
                    throwExceptionIfDirectoryIsNotCreatable(to);
                } else {
                    File parent = to.getParentFile();
                    throwExceptionIfDirectoryIsNotCreatable(parent);
                    copy(zipFile, entry, to);
                }
            }
        }
        return toDir;

    } finally {
        zipFile.close();
    }
}

From source file:nz.ac.otago.psyanlab.common.util.FileUtils.java

/**
 * Extract a single named file from an archive.
 * // w  w w  .j  a v  a 2  s  .  co  m
 * @param needle Archive relative path of file to extract.
 * @param paleFile Experiment archive file.
 * @throws IOException
 * @throws ZipException
 */
public static byte[] extractJust(String needle, File paleFile) throws ZipException, IOException {
    ZipFile archive = new ZipFile(paleFile);
    try {
        ZipEntry ze = archive.getEntry(needle);
        if (ze == null) {
            throw new RuntimeException("Could not find experiment json in " + paleFile.getName());
        }
        InputStream in = archive.getInputStream(ze);
        ByteArrayOutputStream out = new ByteArrayOutputStream();

        try {
            copy(in, out);
            return out.toByteArray();
        } finally {
            in.close();
            out.close();
        }
    } finally {
        archive.close();
    }
}

From source file:com.opoopress.maven.plugins.plugin.zip.ZipUtils.java

public static void unzipFileToDirectory(File file, File destDir, boolean keepTimestamp) throws IOException {
    // create output directory if it doesn't exist
    if (!destDir.exists())
        destDir.mkdirs();//  w  ww.  j av a2  s .  c  o m

    ZipFile zipFile = new ZipFile(file);
    Enumeration<? extends ZipEntry> entries = zipFile.entries();
    while (entries.hasMoreElements()) {
        ZipEntry entry = entries.nextElement();
        if (entry.isDirectory()) {
            new File(destDir, entry.getName()).mkdirs();
            continue;
        }

        InputStream inputStream = zipFile.getInputStream(entry);
        File destFile = new File(destDir, entry.getName());

        System.out.println("Unzipping to " + destFile.getAbsolutePath());
        FileUtils.copyInputStreamToFile(inputStream, destFile);
        IOUtils.closeQuietly(inputStream);

        if (keepTimestamp) {
            long time = entry.getTime();
            if (time > 0) {
                destFile.setLastModified(time);
            }
        }
    }

    zipFile.close();
}

From source file:au.org.ands.vocabs.toolkit.utils.ToolkitFileUtils.java

/** Compress the files in the backup folder for a project.
 * @param projectId The project ID/*from ww w.ja v  a2s.  c om*/
 * @throws IOException Any exception when reading/writing data.
 */
public static void compressBackupFolder(final String projectId) throws IOException {
    String backupPath = getBackupPath(projectId);
    if (!Files.isDirectory(Paths.get(backupPath))) {
        // No such directory, so nothing to do.
        return;
    }
    String projectSlug = makeSlug(projectId);
    // The name of the ZIP file that does/will contain all
    // backups for this project.
    Path zipFilePath = Paths.get(backupPath).resolve(projectSlug + ".zip");
    // A temporary ZIP file. Any existing content in the zipFilePath
    // will be copied into this, followed by any other files in
    // the directory that have not yet been added.
    Path tempZipFilePath = Paths.get(backupPath).resolve("temp" + ".zip");

    File tempZipFile = tempZipFilePath.toFile();
    if (!tempZipFile.exists()) {
        tempZipFile.createNewFile();
    }

    ZipOutputStream tempZipOut = new ZipOutputStream(new FileOutputStream(tempZipFile));

    File existingZipFile = zipFilePath.toFile();
    if (existingZipFile.exists()) {
        ZipFile zipIn = new ZipFile(existingZipFile);

        Enumeration<? extends ZipEntry> entries = zipIn.entries();
        while (entries.hasMoreElements()) {
            ZipEntry e = entries.nextElement();
            logger.debug("compressBackupFolder copying: " + e.getName());
            tempZipOut.putNextEntry(e);
            if (!e.isDirectory()) {
                copy(zipIn.getInputStream(e), tempZipOut);
            }
            tempZipOut.closeEntry();
        }
        zipIn.close();
    }

    File dir = new File(backupPath);
    File[] files = dir.listFiles();

    for (File source : files) {
        if (!source.getName().toLowerCase().endsWith(".zip")) {
            logger.debug("compressBackupFolder compressing and " + "deleting file: " + source.toString());
            if (zipFile(tempZipOut, source)) {
                source.delete();
            }
        }
    }

    tempZipOut.flush();
    tempZipOut.close();
    tempZipFile.renameTo(existingZipFile);
}

From source file:net.sourceforge.floggy.maven.ZipUtils.java

/**
 * DOCUMENT ME!/*from w  w w . java  2s .  c  o m*/
*
* @param file DOCUMENT ME!
* @param directory DOCUMENT ME!
*
* @throws IOException DOCUMENT ME!
*/
public static void unzip(File file, File directory) throws IOException {
    ZipFile zipFile = new ZipFile(file);
    Enumeration entries = zipFile.entries();

    if (!directory.exists() && !directory.mkdirs()) {
        throw new IOException("Unable to create the " + directory + " directory!");
    }

    while (entries.hasMoreElements()) {
        File temp;
        ZipEntry entry = (ZipEntry) entries.nextElement();

        if (entry.isDirectory()) {
            temp = new File(directory, entry.getName());

            if (!temp.exists() && !temp.mkdirs()) {
                throw new IOException("Unable to create the " + temp + " directory!");
            }
        } else {
            temp = new File(directory, entry.getName());
            IOUtils.copy(zipFile.getInputStream(entry), new FileOutputStream(temp));
        }
    }

    zipFile.close();
}

From source file:net.technicpack.utilslib.ZipUtils.java

public static boolean extractFile(File zip, File output, String fileName)
        throws IOException, InterruptedException {
    if (!zip.exists() || fileName == null) {
        return false;
    }//from w  w  w . ja va  2  s  .  c o m

    ZipFile zipFile = new ZipFile(zip);
    try {
        ZipEntry entry = zipFile.getEntry(fileName);
        if (entry == null) {
            Utils.getLogger().log(Level.WARNING, "File " + fileName + " not found in " + zip.getAbsolutePath());
            return false;
        }
        File outputFile = new File(output, entry.getName());

        if (outputFile.getParentFile() != null) {
            outputFile.getParentFile().mkdirs();
        }

        unzipEntry(zipFile, zipFile.getEntry(fileName), outputFile);
        return true;
    } catch (IOException e) {
        Utils.getLogger().log(Level.WARNING,
                "Error extracting file " + fileName + " from " + zip.getAbsolutePath());
        return false;
    } finally {
        zipFile.close();
    }
}