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.eclipse.emf.ecp.emf2web.wizard.ViewModelExportWizard.java

/**
 * Extracts the given zip file to the given destination.
 *
 * @param zipFilePath/*  w w  w.ja v  a  2s  . co m*/
 *            The absolute path of the zip file.
 * @param destinationPath
 *            The absolute path of the destination directory;
 * @throws IOException when extracting or copying fails.
 */
private void extractZip(String zipFilePath, String destinationPath) throws IOException {
    final ZipFile zipFile = new ZipFile(zipFilePath);
    final Enumeration<? extends ZipEntry> entries = zipFile.entries();
    while (entries.hasMoreElements()) {
        final ZipEntry entry = entries.nextElement();
        final File entryDestination = new File(destinationPath, entry.getName());
        entryDestination.getParentFile().mkdirs();
        if (entry.isDirectory()) {
            entryDestination.mkdirs();
        } else {
            final InputStream in = zipFile.getInputStream(entry);
            final OutputStream out = new FileOutputStream(entryDestination);
            IOUtils.copy(in, out);
            IOUtils.closeQuietly(in);
            IOUtils.closeQuietly(out);
        }
    }
    zipFile.close();
}

From source file:podd.resources.services.util.ZipUtility.java

public void unzipFile(File unzipDir, File zipFile) throws IOException {
    ZipFile zippedFile = new ZipFile(zipFile);
    try {//  w ww .ja v  a 2 s. c o m
        final Enumeration<? extends ZipEntry> entries = zippedFile.entries();
        while (entries.hasMoreElements()) {
            final ZipEntry zipEntry = entries.nextElement();
            if (zipEntry.isDirectory()) {
                File newDir = new File(zipEntry.getName());
                //System.out.println("newDir = " + newDir.getPath());
                newDir.mkdir();
            } else {
                final InputStream is = zippedFile.getInputStream(zipEntry);
                File curFile = new File(unzipDir, zipEntry.getName());
                if (!curFile.getParentFile().exists()) {
                    curFile.getParentFile().mkdirs();
                }
                final FileOutputStream os = new FileOutputStream(curFile);
                copyStream(is, os);
            }
        }
    } finally {
        zippedFile.close();
    }
}

From source file:gui.accessories.DownloadProgressWork.java

/**
 * Uncompress all the files contained in the compressed file and its folders in the same folder where the zip file is placed. Doesn't respects the
 * directory tree of the zip file. This method seems a clear candidate to ZipManager.
 *
 * @param file Zip file./* ww  w .j  a va2s . co  m*/
 * @return Count of files uncopressed.
 * @throws ZipException Exception.
 */
private int doUncompressZip(File file) throws ZipException {
    int fileCount = 0;
    try {
        byte[] buf = new byte[1024];
        ZipFile zipFile = new ZipFile(file);

        Enumeration zipFileEntries = zipFile.entries();

        while (zipFileEntries.hasMoreElements()) {
            ZipEntry zipentry = (ZipEntry) zipFileEntries.nextElement();
            if (zipentry.isDirectory()) {
                continue;
            }
            File entryZipFile = new File(zipentry.getName());
            File outputFile = new File(file.getParentFile(), entryZipFile.getName());
            FileOutputStream fileoutputstream = new FileOutputStream(outputFile);
            InputStream is = zipFile.getInputStream(zipentry);
            int n;
            while ((n = is.read(buf, 0, 1024)) > -1) {
                fileoutputstream.write(buf, 0, n);
            }
            fileoutputstream.close();
            is.close();
            fileoutputstream.close();
            fileCount++;
        }
        zipFile.close();

    } catch (IOException ex) {
        throw new ZipException(ex.getMessage());
    }

    return fileCount;
}

From source file:com.genericworkflownodes.knime.nodes.io.outputfile.OutputFileNodeModel.java

/**
 * {@inheritDoc}/*from   w  ww .  j  a va  2 s.  c om*/
 */
@Override
protected void loadInternals(final File internDir, final ExecutionMonitor exec)
        throws IOException, CanceledExecutionException {
    ZipFile zip = new ZipFile(new File(internDir, "loadeddata"));

    @SuppressWarnings("unchecked")
    Enumeration<ZipEntry> entries = (Enumeration<ZipEntry>) zip.entries();

    int BUFFSIZE = 2048;
    byte[] BUFFER = new byte[BUFFSIZE];

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

        if (entry.getName().equals("rawdata.bin")) {
            int size = (int) entry.getSize();
            byte[] data = new byte[size];
            InputStream in = zip.getInputStream(entry);
            int len;
            int totlen = 0;
            while ((len = in.read(BUFFER, 0, BUFFSIZE)) >= 0) {
                System.arraycopy(BUFFER, 0, data, totlen, len);
                totlen += len;
            }
            this.data = new String(data);
        }
    }
    zip.close();
}

From source file:org.jboss.web.tomcat.tc5.TomcatDeployer.java

private String findConfig(URL warURL) throws IOException {
    String result = null;/*from   w  w w  .  j  a v a2 s  .  c o  m*/
    // See if the warUrl is a dir or a file
    File warFile = new File(warURL.getFile());
    if (warURL.getProtocol().equals("file") && warFile.isDirectory() == true) {
        File webDD = new File(warFile, CONTEXT_CONFIG_FILE);
        if (webDD.exists() == true)
            result = webDD.getAbsolutePath();
    } else {
        ZipFile zipFile = new ZipFile(warFile);
        ZipEntry entry = zipFile.getEntry(CONTEXT_CONFIG_FILE);
        if (entry != null) {
            InputStream zipIS = zipFile.getInputStream(entry);
            byte[] buffer = new byte[512];
            int bytes;
            result = warFile.getAbsolutePath() + "-context.xml";
            FileOutputStream fos = new FileOutputStream(result);
            while ((bytes = zipIS.read(buffer)) > 0) {
                fos.write(buffer, 0, bytes);
            }
            zipIS.close();
            fos.close();
        }
        zipFile.close();
    }
    return result;
}

From source file:com.aurel.track.lucene.util.FileUtil.java

public static void unzipFile(File uploadZip, File unzipDestinationDirectory) throws IOException {
    if (!unzipDestinationDirectory.exists()) {
        unzipDestinationDirectory.mkdirs();
    }//from ww w  .j  a va 2  s  .  c om
    final int BUFFER = 2048;
    // Open Zip file for reading
    ZipFile zipFile = new ZipFile(uploadZip, ZipFile.OPEN_READ);

    // Create an enumeration of the entries in the zip file
    Enumeration zipFileEntries = zipFile.entries();

    // Process each entry
    while (zipFileEntries.hasMoreElements()) {
        // grab a zip file entry
        ZipEntry entry = (ZipEntry) zipFileEntries.nextElement();
        String currentEntry = entry.getName();
        File destFile = new File(unzipDestinationDirectory, currentEntry);
        // grab file's parent directory structure
        File destinationParent = destFile.getParentFile();

        // create the parent directory structure if needed
        destinationParent.mkdirs();

        // extract file if not a directory
        if (!entry.isDirectory()) {
            BufferedInputStream is = new BufferedInputStream(zipFile.getInputStream(entry));
            int currentByte;
            // establish buffer for writing file
            byte data[] = new byte[BUFFER];

            // write the current file to disk
            FileOutputStream fos = new FileOutputStream(destFile);
            BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER);

            // read and write until last byte is encountered
            while ((currentByte = is.read(data, 0, BUFFER)) != -1) {
                dest.write(data, 0, currentByte);
            }
            dest.flush();
            dest.close();
            is.close();
        }
    }
    zipFile.close();
}

From source file:com.s2g.pst.resume.importer.UnZipHelper.java

/**
 * Unzip it/*from   w  ww .j ava2  s . c  o m*/
 *
 * @param fileName
 * @param outputFolder
 *
 * @throws java.io.FileNotFoundException
 */
public void unZipFolder(String fileName, String outputFolder) throws IOException {

    ZipFile zipFile = new ZipFile(fileName);
    Enumeration<? extends ZipEntry> entries = zipFile.entries();
    while (entries.hasMoreElements()) {
        ZipEntry entry = entries.nextElement();
        File entryDestination = new File(outputFolder, entry.getName());
        entryDestination.getParentFile().mkdirs();
        if (entry.isDirectory()) {
            //FileHelper fileHelper = new FileHelper();
            //String filename = fileHelper.getUniqueFileName(outputFolder, FilenameUtils.removeExtension(entry.getName()));
            //System.out.println("zip :" + filename);
            //new File(filename).mkdirs();

            entryDestination.mkdirs();

        } else {
            InputStream in = zipFile.getInputStream(entry);
            OutputStream out = new FileOutputStream(entryDestination);
            IOUtils.copy(in, out);
            IOUtils.closeQuietly(in);
            IOUtils.closeQuietly(out);
        }
    }

    zipFile.close();
    this.deleteZipFile(fileName);
    System.out.println("Done");

}

From source file:org.geowe.server.upload.FileUploadZipServlet.java

private String readZipFile(ZipFile zipFile) {
    String content = EMPTY;/*  www.j a  va  2 s .  c om*/

    try {

        Enumeration<?> enu = zipFile.entries();
        if (enu.hasMoreElements()) {
            ZipEntry zipEntry = (ZipEntry) enu.nextElement();
            if (zipEntry.isDirectory()) {
                content = BAD_FORMAT;
            } else if (!(zipEntry.getName().equals(PRJ_FILE_NAME))) {
                content = BAD_FORMAT;
            } else {
                InputStream is = zipFile.getInputStream(zipEntry);
                content = new java.util.Scanner(is).useDelimiter("\\A").next();
            }

            // String name = zipEntry.getName();
            // long size = zipEntry.getSize();
            // long compressedSize = zipEntry.getCompressedSize();
            // LOG.info("name: " + name + " | size: " + size
            // + " | compressed size: " + compressedSize);

        }
        zipFile.close();

    } catch (IOException e) {
        LOG.error("Se produce error en ZipFile: " + e.getMessage());
        e.printStackTrace();
    }

    return content;
}

From source file:org.n52.geoar.codebase.resources.InfoResource.java

private void extractPluginImage(File pluginFile, File pluginImageFile) throws IOException {
    ZipFile zipFile = new ZipFile(pluginFile);
    ZipEntry pluginIconEntry = zipFile.getEntry(GEOAR_IMAGE_NAME);
    if (pluginIconEntry == null) {
        return;/*from   w ww  . j a  va 2  s . c o  m*/
    }

    InputStream inputStream = zipFile.getInputStream(pluginIconEntry);
    OutputStream outputStream = new FileOutputStream(pluginImageFile);

    int read = 0;
    byte[] bytes = new byte[4096];
    while ((read = inputStream.read(bytes)) != -1) {
        outputStream.write(bytes, 0, read);
    }

    zipFile.close();
    outputStream.flush();
    outputStream.close();
}

From source file:net.minecraftforge.fml.server.FMLServerHandler.java

@Override
public void addModAsResource(ModContainer container) {
    String langFile = "assets/" + container.getModId().toLowerCase() + "/lang/en_US.lang";
    File source = container.getSource();
    InputStream stream = null;/*from w  ww .ja va2  s  . com*/
    ZipFile zip = null;
    try {
        if (source.isDirectory() && (Boolean) Launch.blackboard.get("fml.deobfuscatedEnvironment")) {
            stream = new FileInputStream(new File(source.toURI().resolve(langFile).getPath()));
        } else {
            zip = new ZipFile(source);
            ZipEntry entry = zip.getEntry(langFile);
            if (entry == null)
                throw new FileNotFoundException();
            stream = zip.getInputStream(entry);
        }
        LanguageMap.inject(stream);
    } catch (IOException e) {
        // hush
    } catch (Exception e) {
        FMLLog.getLogger().error(e);
    } finally {
        IOUtils.closeQuietly(stream);
        try {
            if (zip != null)
                zip.close();
        } catch (IOException e) {
            // shush
        }
    }
}