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:gov.va.oia.terminology.converters.sharedUtils.Unzip.java

public final static void unzip(File zipFile, File rootDir) throws IOException {
    ZipFile zip = new ZipFile(zipFile);
    @SuppressWarnings("unchecked")
    Enumeration<ZipEntry> entries = (Enumeration<ZipEntry>) zip.entries();
    while (entries.hasMoreElements()) {
        ZipEntry entry = entries.nextElement();
        java.io.File f = new java.io.File(rootDir, entry.getName());
        if (entry.isDirectory()) {
            f.mkdirs();//from  w ww .  j a  va2  s  .co  m
            continue;
        } else {
            f.createNewFile();
        }
        InputStream is = null;
        OutputStream os = null;
        try {
            is = zip.getInputStream(entry);
            os = new FileOutputStream(f);
            IOUtils.copy(is, os);
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (Exception e) {
                    // noop
                }
            }
            if (os != null) {
                try {
                    os.close();
                } catch (Exception e) {
                    // noop
                }
            }
        }
    }
    zip.close();
}

From source file:org.paxml.bean.UnzipTag.java

public static void unzip(File file, File dir) {
    dir.mkdirs();//from   w ww  .  j  av  a 2 s.c o m
    ZipFile zipFile = null;

    try {
        zipFile = new ZipFile(file);

        Enumeration entries = zipFile.entries();

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

            if (entry.isDirectory()) {

                new File(dir, entry.getName()).mkdirs();
                continue;
            }

            InputStream in = null;
            OutputStream out = null;
            try {
                zipFile.getInputStream(entry);
                out = new BufferedOutputStream(new FileOutputStream(entry.getName()));
                IOUtils.copy(in, out);
            } finally {
                IOUtils.closeQuietly(in);
                IOUtils.closeQuietly(out);
            }
        }

    } catch (IOException ioe) {

        throw new PaxmlRuntimeException(
                "Cannot unzip file: " + file.getAbsolutePath() + " under dir: " + dir.getAbsolutePath(), ioe);

    } finally {

        if (zipFile != null) {
            try {
                zipFile.close();
            } catch (IOException e) {
                // do nothing
            }
        }
    }
}

From source file:com.idocbox.common.file.ZipExtracter.java

/**
 * extract given entry in zip file to destination directory.
 * if given entry is a directory, extract all folder and files under it to
 * destination directory.//  w  w w. ja  va  2 s . c  om
 * @param zipFileName  name of zip file.
 * @param entry   it can be a directory or a file.(if it is end with "/",it 
 *                will be treated as a directory.) . For example, 
 *                conf/ represents subdirectory in zip file.
 *                Use "/" to represent root of zip file.
  *                
 * @param desDir  destination directory.
 * @param startDirLevel the level to start create directory.
 *                      Its value is 1,2,...
 * @throws IOException 
 * @throws ZipException 
 */
public static void extract(final String zipFileName, final String entry, final String desDir,
        final int... startDirLevel) throws ZipException, IOException {
    File f = new File(zipFileName);
    if (f.exists()) {
        //check destination directory.
        File desf = new File(desDir);
        if (!desf.exists()) {
            desf.mkdirs();
        }

        boolean toExtractDir = false;
        if (entry.endsWith("/")) {
            toExtractDir = true;
        }

        //create zip file object.
        ZipFile zf = new ZipFile(f);

        if (toExtractDir) {
            Enumeration<ZipEntry> entries = (Enumeration<ZipEntry>) zf.entries();
            ZipEntry ent = null;
            while (entries.hasMoreElements()) {
                ent = entries.nextElement();
                if (isUnderDir(ent, entry)) { //the entry is under directory 
                                              //which should to be extracted.
                                              //extract the entry.
                    extract(zf, ent, desDir, startDirLevel);
                } else {
                    //nothing to do.
                }
            }
        } else {//extract given zip entry file to destination directory.
            ZipEntry zipEntry = zf.getEntry(entry);
            if (null != zipEntry) {
                extract(zf, zipEntry, desDir, startDirLevel);
            }
        }
        if (null != zf) {
            //close zip file.
            zf.close();
        }
    }
}

From source file:org.sonatype.flexmojos.tests.issues.Flexmojos247Test.java

@Test
public void includeAsClasses() throws Exception {
    String baseDir = testIssue("flexmojos-247").getBasedir();
    File target = new File(baseDir, "target");
    Assert.assertTrue(target.exists());/*from w  w w .  j a va 2s.c om*/

    File swc = new File(target, "flexmojos-247-1.0-SNAPSHOT.swc");
    Assert.assertTrue(swc.exists());

    String catalog;
    ZipFile zf = new ZipFile(swc);
    try {
        InputStream in = zf.getInputStream(zf.getEntry("catalog.xml"));
        catalog = IOUtils.toString(in);
        in.close();
    } finally {
        zf.close();
    }

    // must have both classes and the uri
    MatcherAssert.assertThat(catalog, StringContains.containsString("AClass"));
    MatcherAssert.assertThat(catalog, StringContains.containsString("BClass"));
    MatcherAssert.assertThat(catalog, StringContains.containsString("http://flexmojos.sonatype.org/tests"));
}

From source file:org.nuxeo.ecm.platform.filemanager.TestExportedZipImporterPlugin.java

@Test
public void testArchiveDetection() throws Exception {
    createTestDocumentsAndArchive();//from  w w w. j a  va2s .  c  o m
    ZipFile archive = ExportedZipImporter.getArchiveFileIfValid(archiveFile);
    assertNotNull(archive);
    archive.close();
}

From source file:org.nuxeo.ecm.platform.filemanager.service.extension.ExportedZipImporter.java

public DocumentModel create(CoreSession documentManager, Blob content, String path, boolean overwrite,
        String filename, TypeManager typeService) throws IOException {
    try (CloseableFile source = content.getCloseableFile()) {
        ZipFile zip = getArchiveFileIfValid(source.getFile());
        if (zip == null) {
            return null;
        }/*  w w w . j  a  va2 s. c om*/
        zip.close();

        boolean importWithIds = false;
        DocumentReader reader = new NuxeoArchiveReader(source.getFile());
        ExportedDocument root = reader.read();
        IdRef rootRef = new IdRef(root.getId());

        if (documentManager.exists(rootRef)) {
            DocumentModel target = documentManager.getDocument(rootRef);
            if (target.getPath().removeLastSegments(1).equals(new Path(path))) {
                importWithIds = true;
            }
        }

        DocumentWriter writer = new DocumentModelWriter(documentManager, path, 10);
        reader.close();
        reader = new NuxeoArchiveReader(source.getFile());

        DocumentRef resultingRef;
        if (overwrite && importWithIds) {
            resultingRef = rootRef;
        } else {
            String rootName = root.getPath().lastSegment();
            resultingRef = new PathRef(path, rootName);
        }

        try {
            DocumentPipe pipe = new DocumentPipeImpl(10);
            pipe.setReader(reader);
            pipe.setWriter(writer);
            pipe.run();
        } catch (IOException e) {
            log.warn(e, e);
        } finally {
            reader.close();
            writer.close();
        }
        return documentManager.getDocument(resultingRef);
    }
}

From source file:org.cloudgraph.hbase.mapreduce.GraphMapReduceSetup.java

/**
 * Add entries to <code>packagedClasses</code> corresponding to class files
 * contained in <code>jar</code>.
 * //from  w w  w.j av a2s.  c  o  m
 * @param jar
 *          The jar who's content to list.
 * @param packagedClasses
 *          map[class -> jar]
 */
private static void updateMap(String jar, Map<String, String> packagedClasses) throws IOException {
    ZipFile zip = null;
    try {
        zip = new ZipFile(jar);
        for (Enumeration<? extends ZipEntry> iter = zip.entries(); iter.hasMoreElements();) {
            ZipEntry entry = iter.nextElement();
            if (entry.getName().endsWith("class")) {
                packagedClasses.put(entry.getName(), jar);
            }
        }
    } finally {
        if (null != zip)
            zip.close();
    }
}

From source file:org.eclipse.vjet.testframework.artifactmanager.project.ProjectArtifactManager.java

public static String unzipProject(File zipFile, File destDir, String projectName) throws IOException {
    if (!TestUtils.isDirectoryWritable(destDir, true)) {
        return "unable to create writable directory: " + destDir;
    }//from ww  w.  j  a v  a2s.  c  om
    if (!TestUtils.isFileReadable(zipFile)) {
        return "no readable file: " + zipFile;
    }

    ZipFile zip = null;
    String projectEntryName = "projects" + "/" + projectName;
    try {
        zip = new ZipFile(zipFile);
        Enumeration<? extends ZipEntry> entries = zip.entries();
        while (entries.hasMoreElements()) {
            ZipEntry entry = entries.nextElement();
            if (!entry.isDirectory() && entry.getName().startsWith(projectEntryName)) {
                File destFile = new File(destDir, entry.getName());
                InputStream src = null;
                try {
                    src = zip.getInputStream(entry);
                    String result = TestUtils.write(src, destFile);
                    if (null != result) {
                        return result;
                    }
                } finally {
                    if (null != src) {
                        src.close();
                    }
                }
            }
        }
    } finally {
        if (null != zip) {
            zip.close();
        }
    }
    return null;
}

From source file:com.gisgraphy.domain.geoloc.importer.ImporterHelper.java

/**
 * unzip a file in the same directory as the zipped file
 * /*from  www  .  j a  v  a 2  s  .  com*/
 * @param file
 *            The file to unzip
 */
public static void unzipFile(File file) {
    logger.info("will Extracting file: " + file.getName());
    Enumeration<? extends ZipEntry> entries;
    ZipFile zipFile;

    try {
        zipFile = new ZipFile(file);

        entries = zipFile.entries();

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

            if (entry.isDirectory()) {
                // Assume directories are stored parents first then
                // children.
                (new File(entry.getName())).mkdir();
                continue;
            }

            logger.info("Extracting file: " + entry.getName() + " to " + file.getParent() + File.separator
                    + entry.getName());
            copyInputStream(zipFile.getInputStream(entry), new BufferedOutputStream(
                    new FileOutputStream(file.getParent() + File.separator + entry.getName())));
        }

        zipFile.close();
    } catch (IOException e) {
        logger.error("can not unzip " + file.getName() + " : " + e.getMessage());
        throw new ImporterException(e);
    }
}

From source file:JarResources.java

public JarResources(String jarFileName) throws Exception {
    this.jarFileName = jarFileName;
    ZipFile zf = new ZipFile(jarFileName);
    Enumeration e = zf.entries();
    while (e.hasMoreElements()) {
        ZipEntry ze = (ZipEntry) e.nextElement();

        htSizes.put(ze.getName(), new Integer((int) ze.getSize()));
    }//from   ww w.j  av  a  2  s .  c  o  m
    zf.close();

    // extract resources and put them into the hashtable.
    FileInputStream fis = new FileInputStream(jarFileName);
    BufferedInputStream bis = new BufferedInputStream(fis);
    ZipInputStream zis = new ZipInputStream(bis);
    ZipEntry ze = null;
    while ((ze = zis.getNextEntry()) != null) {
        if (ze.isDirectory()) {
            continue;
        }

        int size = (int) ze.getSize();
        // -1 means unknown size.
        if (size == -1) {
            size = ((Integer) htSizes.get(ze.getName())).intValue();
        }

        byte[] b = new byte[(int) size];
        int rb = 0;
        int chunk = 0;
        while (((int) size - rb) > 0) {
            chunk = zis.read(b, rb, (int) size - rb);
            if (chunk == -1) {
                break;
            }
            rb += chunk;
        }

        htJarContents.put(ze.getName(), b);
    }
}