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:org.kalypso.commons.java.util.zip.ZipUtilities.java

public static void unpack(final ZipFile zf, final File targetDir) throws ZipException, IOException {
    targetDir.mkdir();//from w w  w.j av  a  2  s.c  om

    for (final Enumeration<? extends ZipEntry> e = zf.entries(); e.hasMoreElements();) {
        final ZipEntry target = e.nextElement();
        System.out.print(target.getName() + " ."); //$NON-NLS-1$
        saveEntry(zf, targetDir, target);
        System.out.println(". unpacked"); //$NON-NLS-1$
    }
    zf.close();
}

From source file:org.roda.common.certification.OOXMLSignatureUtils.java

public static Map<Path, String> runDigitalSignatureExtract(Path input) throws SignatureException, IOException {
    Map<Path, String> paths = new HashMap<Path, String>();

    ZipFile zipFile = new ZipFile(input.toString());
    Enumeration<?> enumeration;
    for (enumeration = zipFile.entries(); enumeration.hasMoreElements();) {
        ZipEntry entry = (ZipEntry) enumeration.nextElement();
        String entryName = entry.getName();
        if (entryName.startsWith("_xmlsignatures") && entryName.endsWith(".xml")) {
            Path extractedSignature = Files.createTempFile("extraction", ".xml");
            InputStream zipStream = zipFile.getInputStream(entry);
            FileUtils.copyInputStreamToFile(zipStream, extractedSignature.toFile());
            paths.put(extractedSignature,
                    entryName.substring(entryName.lastIndexOf('/') + 1, entryName.lastIndexOf('.')));
            IOUtils.closeQuietly(zipStream);
        }//ww w.  j a  v  a2  s.  c  om
    }

    zipFile.close();
    return paths;
}

From source file:org.sorcersoft.sigar.Zip.java

/**
 * @return the list of roots, that is directories of files that are at the top level of the zip file
 * @throws IOException/*from  w  w w .  ja  v a  2s . c  o m*/
 */
public static List<File> unzip(File zip, File targetDir) throws IOException {
    ZipFile zipFile = new ZipFile(zip);
    try {
        Set<String> roots = new HashSet<String>();
        Enumeration<? extends ZipEntry> entries = zipFile.entries();
        while (entries.hasMoreElements()) {
            ZipEntry entry = entries.nextElement();
            if (entry.isDirectory()) {
                File dir = new File(targetDir, entry.getName());
                if (!dir.exists()) {
                    FileUtils.forceMkdir(dir);
                }
            } else
                copyInputStream(zipFile, entry, targetDir);
            roots.add(getRoot(entry.getName()));
        }

        ArrayList<File> result = new ArrayList<File>(roots.size());
        for (String root : roots) {
            result.add(new File(targetDir, root));
        }
        return result;
    } finally {
        zipFile.close();
    }
}

From source file:Main.java

public static int upZipFile(File zipFile, String folderPath) throws IOException {
    ZipFile zfile = new ZipFile(zipFile);
    Enumeration zList = zfile.entries();
    ZipEntry ze = null;//from  w  w  w .  j  av a2 s.c om
    byte[] buf = new byte[1024];
    while (zList.hasMoreElements()) {
        ze = (ZipEntry) zList.nextElement();
        if (ze.isDirectory()) {
            String dirstr = folderPath + ze.getName();
            dirstr = new String(dirstr.getBytes("8859_1"), "GB2312");
            File f = new File(dirstr);
            f.mkdirs();
            continue;
        }
        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 0;
}

From source file:com.opengamma.util.ZipUtils.java

/**
 * Unzips a ZIP archive./*from  w  ww .  j  a  va  2  s  . co  m*/
 * 
 * @param zipFile  the archive file, not null
 * @param outputDir  the output directory, not null
 */
public static void unzipArchive(final ZipFile zipFile, final File outputDir) {
    ArgumentChecker.notNull(zipFile, "zipFile");
    ArgumentChecker.notNull(outputDir, "outputDir");

    try {
        Enumeration<? extends ZipEntry> entries = zipFile.entries();
        while (entries.hasMoreElements()) {
            ZipEntry entry = entries.nextElement();
            if (entry.isDirectory()) {
                FileUtils.forceMkdir(new File(outputDir, entry.getName()));
                continue;
            }
            File entryDestination = new File(outputDir, entry.getName());
            entryDestination.getParentFile().mkdirs();
            InputStream in = zipFile.getInputStream(entry);
            OutputStream out = new FileOutputStream(entryDestination);
            IOUtils.copy(in, out);
            IOUtils.closeQuietly(in);
            IOUtils.closeQuietly(out);
        }
        zipFile.close();
    } catch (IOException ex) {
        throw new OpenGammaRuntimeException(
                "Error while extracting file: " + zipFile.getName() + " to: " + outputDir, ex);
    }
}

From source file:net.technicpack.launchercore.util.ZipUtils.java

public static boolean extractFile(File zip, File output, String fileName) throws IOException {
    if (!zip.exists() || fileName == null) {
        return false;
    }/*from w  w w . ja va2  s .co  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();
    }
}

From source file:org.sipfoundry.sipxconfig.upload.ZipUpload.java

/**
 * Expand zip file into destination directory
 *///from www  .  j  av  a  2  s . c om
static void deployZipFile(File expandDirectory, File zipFile) {
    try {
        ZipFile zip = new ZipFile(zipFile);
        Enumeration<? extends ZipEntry> entries = zip.entries();
        while (entries.hasMoreElements()) {
            InputStream in = null;
            OutputStream out = null;
            try {
                ZipEntry entry = entries.nextElement();
                File file = new File(expandDirectory, entry.getName());
                if (entry.isDirectory()) {
                    file.mkdirs();
                } else {
                    file.getParentFile().mkdirs();
                    in = zip.getInputStream(entry);
                    out = new FileOutputStream(file);
                    IOUtils.copy(in, out);
                }
            } finally {
                IOUtils.closeQuietly(in);
                IOUtils.closeQuietly(out);
            }
        }
        zip.close();
    } catch (ZipException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.apache.maven.doxia.siterenderer.DefaultSiteRenderer.java

private static void closeZipFile(ZipFile zipFile) {
    // TODO: move to plexus utils
    try {/*w  w w .  j a  v  a  2  s.  com*/
        zipFile.close();
    } catch (IOException e) {
        // ignore
    }
}

From source file:org.qi4j.library.staticlet.files.TestData.java

public static synchronized File deployDocRoot(File buildDirectory) throws IOException {
    // Unzip staticlet-docroot.zip to ~/target/ --------------------------------------------------------------------
    String testResourcesDirectory = System.getProperty("testResourcesDirectory");
    FileUtils.forceMkdir(buildDirectory);
    int buffSize = 1024 * 64;
    ZipFile docRootZip = new ZipFile(new File(testResourcesDirectory, "staticlet-docroot.zip"));
    Enumeration<? extends ZipEntry> entries = docRootZip.entries();
    while (entries.hasMoreElements()) {
        ZipEntry eachEntry = entries.nextElement();
        File out = new File(buildDirectory, eachEntry.getName());
        FileUtils.forceMkdir(out.getParentFile());
        if (!eachEntry.isDirectory()) {
            BufferedInputStream input = new BufferedInputStream(docRootZip.getInputStream(eachEntry));
            byte[] buffer = new byte[buffSize];
            BufferedOutputStream output = new BufferedOutputStream(new FileOutputStream(out), buffSize);
            while (input.read(buffer, 0, buffSize) != -1) {
                output.write(buffer, 0, buffSize);
            }/*from  w  w  w . j  a  va2 s . c o m*/
            output.flush();
            output.close();
            input.close();
        }
    }
    docRootZip.close();

    // Load test docroot content in memory for unit tests ----------------------------------------------------------
    File docRoot = new File(buildDirectory, "staticlet-docroot");

    // text-plain.txt
    StringWriter sw = new StringWriter();
    IOUtils.copy(new FileInputStream(new File(docRoot, TEXT_PLAIN)), sw, "UTF-8");
    sw.flush();
    TEXT_PLAIN_TEXT = sw.toString().trim();

    return docRoot;
}

From source file:org.ut.biolab.medsavant.client.view.genetics.variantinfo.GenemaniaInfoRetriever.java

public static void extractGM(String pathToGMData) {
    String directoryPath = DirectorySettings.getCacheDirectory().getAbsolutePath();
    try {//w ww. j av  a 2  s .  c o  m
        File data = new File(pathToGMData);
        ZipFile zipData = new ZipFile(data.getAbsolutePath());
        Enumeration entries = zipData.entries();
        while (entries.hasMoreElements()) {
            ZipEntry entry = (ZipEntry) entries.nextElement();
            if (entry.isDirectory()) {
                (new File(directoryPath + File.separator + entry.getName())).mkdirs();
                continue;
            }

            IOUtils.copy(zipData.getInputStream(entry),
                    new FileOutputStream(directoryPath + File.separator + entry.getName()));
        }
        zipData.close();
        FileWriter fstream = new FileWriter(DirectorySettings.getGeneManiaDirectory() + File.separator
                + DirectorySettings.GENEMANIA_CHECK_FILE);
        BufferedWriter out = new BufferedWriter(fstream);
        out.write("This file indicates that the GeneMANIA data has finished downloading.");
        out.close();
        if (!data.delete()) {
            LOG.error("Couldn't delete GeneMANIA .zip: " + data.getAbsolutePath());
        }

    } catch (IOException ex) {
        java.util.logging.Logger.getLogger(GenemaniaInfoRetriever.class.getName()).log(Level.SEVERE, null, ex);
    }
}