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:io.github.jeremgamer.preview.actions.Download.java

private void download() {

    GeneralSave gs = new GeneralSave();
    try {//  ww w. ja  v  a2  s . co m
        gs.load(new File("projects/" + Editor.getProjectName() + "/general.rbd"));
    } catch (IOException e1) {
        e1.printStackTrace();
    }
    name = gs.getString("name");
    new Thread(new Runnable() {

        @Override
        public void run() {
            if (url == null) {

            } else {
                File archive = new File(System.getProperty("user.home") + "/AppData/Roaming/.rocketbuilder/"
                        + name + "/data.zip");
                File outputFolder = new File(
                        System.getProperty("user.home") + "/AppData/Roaming/.rocketbuilder/" + name);

                new File(System.getProperty("user.home") + "/AppData/Roaming/.rocketbuilder/" + name).mkdirs();
                URL webFile;
                try {
                    webFile = new URL(url);
                    ReadableByteChannel rbc = Channels.newChannel(webFile.openStream());
                    fos = new FileOutputStream(System.getProperty("user.home")
                            + "/AppData/Roaming/.rocketbuilder/" + name + "/data.zip");
                    HttpURLConnection httpConn = (HttpURLConnection) webFile.openConnection();
                    totalBytes = httpConn.getContentLength();
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                while (bytesCopied < totalBytes) {
                                    for (CustomProgressBar bar : barList) {
                                        bytesCopied = fos.getChannel().size();
                                        progressValue = (int) (100 * bytesCopied / totalBytes);
                                        bar.setValue(progressValue);
                                        if (bar.isStringPainted()) {
                                            bar.setString(progressValue + "%     " + bytesCopied / 1000 + "/"
                                                    + totalBytes / 1000 + "Kb     tape " + step + "/2");
                                        }
                                    }
                                }
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }

                    }).start();
                    fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
                    fos.close();
                    step = 2;
                    for (CustomProgressBar bar : barList) {
                        if (bar.isStringPainted()) {
                            bar.setString("tape " + step + "/2 : Extraction");
                        }
                    }

                    for (int timeout = 100; timeout > 0; timeout--) {
                        RandomAccessFile ran = null;

                        try {
                            ran = new RandomAccessFile(archive, "rw");
                            break;
                        } catch (Exception ex) {
                        } finally {
                            if (ran != null)
                                try {
                                    ran.close();
                                } catch (IOException ex) {
                                }

                            ran = null;
                        }

                        try {
                            Thread.sleep(100);
                        } catch (InterruptedException ex) {
                        }
                    }

                    ZipFile zipFile = new ZipFile(archive, Charset.forName("Cp437"));
                    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())
                            entryDestination.mkdirs();
                        else {
                            InputStream in = zipFile.getInputStream(entry);
                            OutputStream out = new FileOutputStream(entryDestination);
                            IOUtils.copy(in, out);
                            IOUtils.closeQuietly(in);
                            IOUtils.closeQuietly(out);
                            in.close();
                            out.close();
                        }
                    }

                    for (CustomProgressBar bar : barList) {
                        bar.setString("");
                    }

                    zipFile.close();
                    archive.delete();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }).start();
}

From source file:com.edgenius.wiki.service.impl.ThemeServiceImpl.java

private String getMetafile(File zipFile, String fileName) {
    String content = "";
    ZipFile zip = null;
    try {//from  w  w  w  .  ja v a2  s.com
        zip = new ZipFile(zipFile);
        ZipEntry entry = zip.getEntry(fileName);
        if (entry != null) {
            content = IOUtils.toString(zip.getInputStream(entry));
        }
    } catch (Exception e) {
        log.info("backup/restore file comment not available:" + zipFile.getAbsolutePath());
    } finally {
        if (zip != null)
            try {
                zip.close();
            } catch (Exception e) {
            }
    }

    return content;
}

From source file:de.jcup.egradle.core.util.FileSupport.java

/**
 * Unzips the given zip file to the given destination directory extracting
 * only those entries the pass through the given filter.
 *
 * @param zipFile/*from   ww  w .  ja  v  a2 s. c  o m*/
 *            the zip file to unzip
 * @param dstDir
 *            the destination directory
 * @throws IOException
 *             in case of problem
 */
public void unzip(ZipFile zipFile, File dstDir) throws IOException {

    Enumeration<? extends ZipEntry> entries = zipFile.entries();

    try {
        while (entries.hasMoreElements()) {
            ZipEntry entry = entries.nextElement();
            if (entry.isDirectory()) {
                continue;
            }
            String entryName = entry.getName();
            File file = new File(dstDir, changeSeparator(entryName, '/', File.separatorChar));
            file.getParentFile().mkdirs();
            InputStream src = null;
            OutputStream dst = null;
            try {
                src = zipFile.getInputStream(entry);
                dst = new FileOutputStream(file);
                transferData(src, dst);
            } finally {
                if (dst != null) {
                    try {
                        dst.close();
                    } catch (IOException e) {
                    }
                }
                if (src != null) {
                    src.close();
                }
            }
        }
    } finally {
        try {
            zipFile.close();
        } catch (IOException e) {
        }
    }
}

From source file:com.axelor.apps.base.service.imports.importer.Importer.java

protected void unZip(File file, File directory) throws ZipException, IOException {

    File extractFile = null;//from   w  w  w .  ja  va2s.  c  o  m
    FileOutputStream fileOutputStream = null;
    ZipFile zipFile = new ZipFile(file);
    Enumeration<? extends ZipEntry> entries = zipFile.entries();

    while (entries.hasMoreElements()) {
        try {
            ZipEntry entry = entries.nextElement();
            InputStream entryInputStream = zipFile.getInputStream(entry);
            byte[] buffer = new byte[1024];
            int bytesRead = 0;

            extractFile = new File(directory, entry.getName());
            if (entry.isDirectory()) {
                extractFile.mkdirs();
                continue;
            } else {
                extractFile.getParentFile().mkdirs();
                extractFile.createNewFile();
            }

            fileOutputStream = new FileOutputStream(extractFile);
            while ((bytesRead = entryInputStream.read(buffer)) != -1) {
                fileOutputStream.write(buffer, 0, bytesRead);
            }

        } catch (IOException ioException) {
            log.error(ioException.getMessage());
            continue;
        } finally {
            if (fileOutputStream == null) {
                continue;
            }
            try {
                fileOutputStream.close();
            } catch (IOException e) {
            }
        }
    }

    zipFile.close();

}

From source file:org.opencastproject.util.ZipUtilTest.java

@Test
public void zipRecFileStr() throws Exception {

    String destFilePath = destDir.getCanonicalPath() + File.separator + "recFileStr.zip";

    Vector<String> names = new Vector<String>();
    names.add(srcFileName);/*w  w w  .  j a  va  2  s.com*/
    names.add(nestedSrcDirName + File.separator);
    names.add(nestedSrcDirName + File.separator + nestedSrcFileName);

    File test = ZipUtil.zip(srcDir.listFiles(), destFilePath, true, ZipUtil.NO_COMPRESSION);
    Assert.assertTrue(test.exists());
    ZipFile zip = new ZipFile(test);
    Assert.assertEquals(3, zip.size());
    Enumeration<? extends ZipEntry> entries = zip.entries();

    try {
        while (entries.hasMoreElements()) {
            ZipEntry entry = entries.nextElement();
            // The 'replace' method is used because the filesystem directory separator may not be the same as the Zip
            // files's
            Assert.assertTrue(names.contains(entry.getName().replace('/', File.separatorChar)));
        }
    } catch (AssertionError ae) {
        zip.close();
        throw ae;
    }

    zip.close();
}

From source file:org.opencastproject.util.ZipUtilTest.java

@Test
public void zipRecFileFile() throws Exception {

    File destFile = new File(destDir, "recFileFile.zip");

    Vector<String> names = new Vector<String>();
    names.add(srcFileName);/*from   www .  ja va2  s.co  m*/
    names.add(nestedSrcDirName + File.separator);
    names.add(nestedSrcDirName + File.separator + nestedSrcFileName);

    File test = ZipUtil.zip(srcDir.listFiles(), destFile, true, ZipUtil.NO_COMPRESSION);
    Assert.assertTrue(test.exists());
    ZipFile zip = new ZipFile(test);
    Assert.assertEquals(3, zip.size());
    Enumeration<? extends ZipEntry> entries = zip.entries();

    try {
        while (entries.hasMoreElements()) {
            ZipEntry entry = entries.nextElement();
            // The 'replace' method is used because the filesystem directory separator may not be the same as the Zip
            // files's
            Assert.assertTrue(names.contains(entry.getName().replace('/', File.separatorChar)));
        }
    } catch (AssertionError ae) {
        zip.close();
        throw ae;
    }

    zip.close();
}

From source file:org.java.plugin.standard.ShadingPathResolver.java

URL shadowResource(final URL source, final String uid, final boolean unpack) {
    try {/* w w  w  .  j  av  a2 s .  c  om*/
        URL result = deepCheck(source, uid);
        if (result != null) {
            if (log.isDebugEnabled()) {
                log.debug("got actual shaded resource, UID=" + uid //$NON-NLS-1$
                        + ", source=" + source //$NON-NLS-1$
                        + ", file=" + result); //$NON-NLS-1$
            }
            return result;
        }
    } catch (Exception e) {
        log.warn("deep check failed, UID=" + uid //$NON-NLS-1$
                + ", URL=" + source, e); //$NON-NLS-1$
        remove(uid);
    }
    Date lastModified;
    try {
        lastModified = ShadingUtil.getLastModified(source);
    } catch (IOException ioe) {
        log.error("shading failed, can't get modification date for " //$NON-NLS-1$
                + source, ioe);
        return source;
    }
    File file = IoUtil.url2file(source);
    if ((file != null) && file.isDirectory()) {
        // copy local folder to the shadow directory
        try {
            File rootFolder = new File(shadowFolder, uid);
            IoUtil.copyFolder(file, rootFolder, true, true, fileFilter);
            return add(uid, source, rootFolder, lastModified);
        } catch (IOException ioe) {
            log.error("failed shading local folder " + file, ioe); //$NON-NLS-1$
            return source;
        }
    }
    try {
        if ("jar".equalsIgnoreCase(source.getProtocol())) { //$NON-NLS-1$
            String urlStr = source.toExternalForm();
            int p = urlStr.indexOf("!/"); //$NON-NLS-1$
            if (p == -1) {
                p = urlStr.length();
            }
            URL jarFileURL = new URL(urlStr.substring(4, p));
            if (!unpack) {
                String ext = ShadingUtil.getExtension(jarFileURL.getFile());
                if (ext == null) {
                    ext = "jar"; //$NON-NLS-1$
                }
                File shadowFile = new File(shadowFolder, uid + '.' + ext);
                File sourceFile = IoUtil.url2file(jarFileURL);
                InputStream in;
                if (sourceFile != null) {
                    in = new BufferedInputStream(new FileInputStream(sourceFile));
                } else {
                    in = jarFileURL.openStream();
                }
                try {
                    OutputStream out = new FileOutputStream(shadowFile, false);
                    try {
                        IoUtil.copyStream(in, out, 1024);
                    } finally {
                        out.close();
                    }
                } finally {
                    in.close();
                }
                return add(uid, source, shadowFile, lastModified);
            }
            URLConnection cnn = null;
            try {
                File sourceFile = IoUtil.url2file(jarFileURL);
                ZipFile zipFile;
                if (sourceFile != null) {
                    zipFile = new ZipFile(sourceFile);
                } else {
                    cnn = source.openConnection();
                    cnn.setUseCaches(false);
                    zipFile = ((JarURLConnection) cnn).getJarFile();
                }
                File rootFolder = new File(shadowFolder, uid);
                try {
                    ShadingUtil.unpack(zipFile, rootFolder);
                } finally {
                    zipFile.close();
                }
                return add(uid, source, rootFolder, lastModified);
            } finally {
                if (cnn != null) {
                    cnn.getInputStream().close();
                }
            }
        }
    } catch (IOException ioe) {
        log.error("failed shading URL connection " + source, ioe); //$NON-NLS-1$
        return source;
    }
    String fileName = source.getFile();
    if (fileName == null) {
        log.warn("can't get file name from resource " + source //$NON-NLS-1$
                + ", shading failed"); //$NON-NLS-1$
        return source;
    }
    String ext = ShadingUtil.getExtension(fileName);
    if (ext == null) {
        log.warn("can't get file name extension for resource " + source //$NON-NLS-1$
                + ", shading failed"); //$NON-NLS-1$
        return source;
    }
    if (unpack && ("jar".equalsIgnoreCase(ext) //$NON-NLS-1$
            || "zip".equalsIgnoreCase(ext))) { //$NON-NLS-1$
        try {
            InputStream strm = source.openStream();
            File rootFolder = new File(shadowFolder, uid);
            try {
                ShadingUtil.unpack(strm, rootFolder);
            } finally {
                strm.close();
            }
            return add(uid, source, rootFolder, lastModified);
        } catch (IOException ioe) {
            log.error("failed shading packed resource " + source, ioe); //$NON-NLS-1$
            return source;
        }
    }
    try {
        File shadowFile = new File(shadowFolder, uid + '.' + ext);
        InputStream in = source.openStream();
        try {
            OutputStream out = new FileOutputStream(shadowFile, false);
            try {
                IoUtil.copyStream(in, out, 1024);
            } finally {
                out.close();
            }
        } finally {
            in.close();
        }
        return add(uid, source, shadowFile, lastModified);
    } catch (IOException ioe) {
        log.error("failed shading resource file " + source, ioe); //$NON-NLS-1$
        return source;
    }
}

From source file:org.zeroturnaround.zip.ZipUtil.java

/**
 * Closes the ZIP file while ignoring any errors.
 *
 * @param zf/*w  ww .  ja v  a  2  s. co m*/
 *          ZIP file to be closed.
 */
public static void closeQuietly(ZipFile zf) {
    try {
        if (zf != null) {
            zf.close();
        }
    } catch (IOException e) {
    }
}

From source file:org.opencastproject.util.ZipUtilTest.java

@Test
public void zipRecStrStr() throws Exception {

    File destFile = new File(destDir, "recStrStr.zip");

    Vector<String> names = new Vector<String>();
    names.add(srcFileName);/*from   w w w.  j  a v  a  2 s  . c  o  m*/
    names.add(nestedSrcDirName + File.separator);
    names.add(nestedSrcDirName + File.separator + nestedSrcFileName);

    String[] filenames = srcDir.list();
    for (int i = 0; i < filenames.length; i++)
        filenames[i] = srcDir.getCanonicalPath() + File.separator + filenames[i];

    File test = ZipUtil.zip(filenames, destFile.getCanonicalPath(), true, ZipUtil.NO_COMPRESSION);
    Assert.assertTrue(test.exists());
    ZipFile zip = new ZipFile(test);
    Assert.assertEquals(3, zip.size());
    Enumeration<? extends ZipEntry> entries = zip.entries();

    try {
        while (entries.hasMoreElements()) {
            ZipEntry entry = entries.nextElement();
            // The 'replace' method is used because the filesystem directory separator may not be the same as the Zip
            // files's
            Assert.assertTrue(names.contains(entry.getName().replace('/', File.separatorChar)));
        }
    } catch (AssertionError ae) {
        zip.close();
        throw ae;
    }

    zip.close();
}

From source file:org.opencastproject.util.ZipUtilTest.java

@Test
public void zipRecStrFile() throws Exception {

    File destFile = new File(destDir, "recStrFile.zip");

    Vector<String> names = new Vector<String>();
    names.add(srcFileName);//from ww w  . ja  v a2  s  .c  o m
    names.add(nestedSrcDirName + File.separator);
    names.add(nestedSrcDirName + File.separator + nestedSrcFileName);

    String[] filenames = srcDir.list();
    for (int i = 0; i < filenames.length; i++)
        filenames[i] = srcDir.getCanonicalPath() + File.separator + filenames[i];

    File test = ZipUtil.zip(filenames, destFile, true, ZipUtil.NO_COMPRESSION);
    Assert.assertTrue(test.exists());
    ZipFile zip = new ZipFile(test);
    Assert.assertEquals(3, zip.size());
    Enumeration<? extends ZipEntry> entries = zip.entries();

    try {
        while (entries.hasMoreElements()) {
            ZipEntry entry = entries.nextElement();
            // The 'replace' method is used because the filesystem directory separator may not be the same as the Zip
            // files's
            Assert.assertTrue(names.contains(entry.getName().replace('/', File.separatorChar)));
        }
    } catch (AssertionError ae) {
        zip.close();
        throw ae;
    }

    zip.close();
}