Example usage for org.apache.commons.compress.archivers.zip ZipFile ZipFile

List of usage examples for org.apache.commons.compress.archivers.zip ZipFile ZipFile

Introduction

In this page you can find the example usage for org.apache.commons.compress.archivers.zip ZipFile ZipFile.

Prototype

public ZipFile(File f, String encoding) throws IOException 

Source Link

Document

Opens the given file for reading, assuming the specified encoding for file names and scanning for unicode extra fields.

Usage

From source file:org.abysm.onionzip.ZipFileExtractHelper.java

void listZipArchiveEntries() throws IOException {
    ZipFile zipFile = new ZipFile(zipFilename, encoding);
    try {/*from w w  w.  j  a va  2 s .  c  o  m*/
        System.out.println("Length\tDatetime\tName\tEFS\tUnix Mode");
        for (Enumeration<ZipArchiveEntry> zipArchiveEntryEnumeration = zipFile
                .getEntries(); zipArchiveEntryEnumeration.hasMoreElements();) {
            ZipArchiveEntry entry = zipArchiveEntryEnumeration.nextElement();
            System.out.format("%d\t%s\t%s\t%b\t%o\n", entry.getSize(), entry.getLastModifiedDate().toString(),
                    entry.getName(), entry.getGeneralPurposeBit().usesUTF8ForNames(), entry.getUnixMode());
        }
    } finally {
        ZipFile.closeQuietly(zipFile);
    }
}

From source file:org.abysm.onionzip.ZipFileExtractHelper.java

void extractZipArchiveEntries() throws IOException {
    ZipFile zipFile = new ZipFile(zipFilename, encoding);
    try {/*from ww w.java 2  s.c  o  m*/
        for (Enumeration<ZipArchiveEntry> zipArchiveEntryEnumeration = zipFile
                .getEntries(); zipArchiveEntryEnumeration.hasMoreElements();) {
            ZipArchiveEntry entry = zipArchiveEntryEnumeration.nextElement();
            System.out.println(entry.getName());
            if (entry.isDirectory()) {
                Path directory = Paths.get(entry.getName());
                Files.createDirectories(directory);
            } else if (entry.isUnixSymlink()) {
                Path symlink = Paths.get(entry.getName());
                Path parentDirectory = symlink.getParent();
                Path target = Paths.get(zipFile.getUnixSymlink(entry));
                if (parentDirectory != null) {
                    Files.createDirectories(parentDirectory);
                }
                Files.createSymbolicLink(symlink, target);
            } else {
                Path file = Paths.get(entry.getName());
                Path parentDirectory = file.getParent();
                if (parentDirectory != null) {
                    Files.createDirectories(parentDirectory);
                }
                InputStream contentInputStream = zipFile.getInputStream(entry);
                FileOutputStream extractedFileOutputStream = new FileOutputStream(entry.getName());
                try {
                    IOUtils.copy(contentInputStream, extractedFileOutputStream);
                } finally {
                    IOUtils.closeQuietly(contentInputStream);
                    IOUtils.closeQuietly(extractedFileOutputStream);
                }
                FileTime fileTime = FileTime.fromMillis(entry.getLastModifiedDate().getTime());
                Files.setLastModifiedTime(file, fileTime);
            }
        }
    } finally {
        ZipFile.closeQuietly(zipFile);
    }
}

From source file:org.alfresco.repo.importer.ACPImportPackageHandler.java

public void startImport() {
    log("Importing from zip file " + file.getAbsolutePath());
    try {//from ww w . j a  v  a2  s . co m
        // NOTE: This encoding allows us to workaround bug...
        //       http://bugs.sun.com/bugdatabase/view_bug.do;:WuuT?bug_id=4820807
        zipFile = new ZipFile(file, "UTF-8");
    } catch (IOException e) {
        throw new ImporterException("Failed to read zip file due to " + e.getMessage(), e);
    }
}

From source file:org.apache.ant.compress.resources.ZipResource.java

/**
 * Return an InputStream for reading the contents of this Resource.
 * @return an InputStream object./*from  ww w.  j  a va  2 s  . c o  m*/
 * @throws IOException if the zip file cannot be opened,
 *         or the entry cannot be read.
 */
public InputStream getInputStream() throws IOException {
    if (isReference()) {
        return ((Resource) getCheckedRef()).getInputStream();
    }
    File f = getZipfile();
    if (f == null) {
        return super.getInputStream();
    }

    final ZipFile z = new ZipFile(f, getEncoding());
    ZipArchiveEntry ze = z.getEntry(getName());
    if (ze == null) {
        z.close();
        throw new BuildException("no entry " + getName() + " in " + getArchive());
    }
    return new FilterInputStream(z.getInputStream(ze)) {
        public void close() throws IOException {
            FileUtils.close(in);
            z.close();
        }

        protected void finalize() throws Throwable {
            try {
                close();
            } finally {
                super.finalize();
            }
        }
    };
}

From source file:org.apache.ant.compress.resources.ZipResource.java

/**
 * fetches information from the named entry inside the archive.
 *//* w ww.ja  v  a  2s .  com*/
protected void fetchEntry() {
    File f = getZipfile();
    if (f == null) {
        super.fetchEntry();
        return;
    }

    ZipFile z = null;
    try {
        z = new ZipFile(getZipfile(), getEncoding());
        setEntry(z.getEntry(getName()));
    } catch (IOException e) {
        log(e.getMessage(), Project.MSG_DEBUG);
        throw new BuildException(e);
    } finally {
        ZipFile.closeQuietly(z);
    }
}

From source file:org.apache.ant.compress.resources.ZipScanner.java

/**
 * Fills the file and directory maps with resources read from the
 * archive.//from  ww w.  j a va 2  s  .  c o  m
 *
 * @param src the archive to scan.
 * @param encoding encoding used to encode file names inside the archive.
 * @param fileEntries Map (name to resource) of non-directory
 * resources found inside the archive.
 * @param matchFileEntries Map (name to resource) of non-directory
 * resources found inside the archive that matched all include
 * patterns and didn't match any exclude patterns.
 * @param dirEntries Map (name to resource) of directory
 * resources found inside the archive.
 * @param matchDirEntries Map (name to resource) of directory
 * resources found inside the archive that matched all include
 * patterns and didn't match any exclude patterns.
 */
protected void fillMapsFromArchive(Resource src, String encoding, Map fileEntries, Map matchFileEntries,
        Map dirEntries, Map matchDirEntries) {

    FileProvider fp = (FileProvider) src.as(FileProvider.class);
    if (fp == null) {
        super.fillMapsFromArchive(src, encoding, fileEntries, matchFileEntries, dirEntries, matchDirEntries);
        return;
    }

    File srcFile = fp.getFile();
    ZipArchiveEntry entry = null;
    ZipFile zf = null;

    try {
        try {
            zf = new ZipFile(srcFile, encoding);
        } catch (ZipException ex) {
            throw new BuildException("Problem reading " + srcFile, ex);
        } catch (IOException ex) {
            throw new BuildException("Problem opening " + srcFile, ex);
        }
        Enumeration e = zf.getEntries();
        while (e.hasMoreElements()) {
            entry = (ZipArchiveEntry) e.nextElement();
            if (getSkipUnreadableEntries() && !zf.canReadEntryData(entry)) {
                log(Messages.skippedIsUnreadable(entry));
                continue;
            }
            Resource r = new ZipResource(srcFile, encoding, entry);
            String name = entry.getName();
            if (entry.isDirectory()) {
                name = trimSeparator(name);
                dirEntries.put(name, r);
                if (match(name)) {
                    matchDirEntries.put(name, r);
                }
            } else {
                fileEntries.put(name, r);
                if (match(name)) {
                    matchFileEntries.put(name, r);
                }
            }
        }
    } finally {
        ZipFile.closeQuietly(zf);
    }
}

From source file:org.codehaus.plexus.archiver.zip.PlexusIoZipFileResourceCollection.java

protected Iterator<PlexusIoResource> getEntries() throws IOException {
    final File f = getFile();
    if (f == null) {
        throw new IOException("The zip file has not been set.");
    }//w  ww.  ja  v  a  2  s  . c o m
    final URLClassLoader urlClassLoader = new URLClassLoader(new URL[] { f.toURI().toURL() }, null) {
        public URL getResource(String name) {
            return findResource(name);
        }
    };

    final URL url = new URL("jar:" + f.toURI().toURL() + "!/");
    final ZipFile zipFile = new ZipFile(f, charset != null ? charset.name() : "UTF8");
    final Enumeration<ZipArchiveEntry> en = zipFile.getEntriesInPhysicalOrder();
    return new ZipFileResourceIterator(en, url, zipFile, urlClassLoader);
}

From source file:org.silverpeas.core.util.ZipUtilTest.java

/**
* Test of compressPathToZip method, of class ZipManager.
*
* @throws Exception//from  ww w.  j  av a2s.  c  om
*/
@Test
public void testCompressPathToZip(MavenTestEnv mavenTestEnv) throws Exception {
    File path = new File(mavenTestEnv.getResourceTestDirFile(), "ZipSample");
    File outfile = new File(tempDir, "testCompressPathToZip.zip");
    ZipUtil.compressPathToZip(path, outfile);
    ZipFile zipFile = new ZipFile(outfile, CharEncoding.UTF_8);
    try {
        Enumeration<? extends ZipEntry> entries = zipFile.getEntries();
        assertThat(zipFile.getEncoding(), is(CharEncoding.UTF_8));
        int nbEntries = 0;
        while (entries.hasMoreElements()) {
            ZipEntry entry = entries.nextElement();
            nbEntries++;
        }
        assertThat(nbEntries, is(5));
        assertThat(zipFile.getEntry("ZipSample/simple.txt"), is(notNullValue()));
        assertThat(zipFile.getEntry("ZipSample/level1/simple.txt"), is(notNullValue()));
        assertThat(zipFile.getEntry("ZipSample/level1/level2b/simple.txt"), is(notNullValue()));
        assertThat(zipFile.getEntry("ZipSample/level1/level2a/simple.txt"), is(notNullValue()));

        ZipEntry accentuatedEntry = zipFile.getEntry("ZipSample/level1/level2a/s\u00efmplifi\u00e9.txt");
        if (accentuatedEntry == null) {
            accentuatedEntry = zipFile.getEntry("ZipSample/level1/level2a/"
                    + new String("smplifi.txt".getBytes("UTF-8"), Charset.defaultCharset()));
        }
        assertThat(accentuatedEntry, is(notNullValue()));
        assertThat(zipFile.getEntry("ZipSample/level1/level2c/"), is(nullValue()));
    } finally {
        zipFile.close();
    }
}

From source file:org.yes.cart.utils.impl.ZipUtils.java

/**
 * Unzip archive to given folder.// w  ww.j a  v  a 2 s  .  c  o m
 * @param archive given archive
 * @param outputDir  given folder
 * @throws IOException in case of error
 */

public void unzipArchive(final File archive, final File outputDir) throws IOException {
    ZipFile zipfile = new ZipFile(archive, encoding);
    for (Enumeration e = zipfile.getEntries(); e.hasMoreElements();) {
        final ZipArchiveEntry entry = (ZipArchiveEntry) e.nextElement();
        unzipEntry(zipfile, entry, outputDir);
    }
    zipfile.close();
}