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

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

Introduction

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

Prototype

public static void closeQuietly(ZipFile zipfile) 

Source Link

Document

close a zipfile quietly; throw no io fault, do nothing on a null parameter

Usage

From source file:com.taobao.android.utils.ZipUtils.java

/**
 * zip?/*from  w  ww  . ja v  a 2s.c  o  m*/
 *
 * @param zipFile
 * @param path
 * @param destFolder
 * @throws IOException
 */
public static File extractZipFileToFolder(File zipFile, String path, File destFolder) {
    ZipFile zip;
    File destFile = null;
    try {
        zip = new ZipFile(zipFile);
        ZipArchiveEntry zipArchiveEntry = zip.getEntry(path);
        if (null != zipArchiveEntry) {
            String name = zipArchiveEntry.getName();
            name = FilenameUtils.getName(name);
            destFile = new File(destFolder, name);
            destFolder.mkdirs();
            destFile.createNewFile();
            InputStream is = zip.getInputStream(zipArchiveEntry);
            FileOutputStream fos = new FileOutputStream(destFile);
            int length = 0;
            byte[] b = new byte[1024];
            while ((length = is.read(b, 0, 1024)) != -1) {
                fos.write(b, 0, length);
            }
            is.close();
            fos.close();
        }
        if (null != zip)
            ZipFile.closeQuietly(zip);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return destFile;
}

From source file:com.taobao.android.builder.tools.zip.ZipUtils.java

/**
 * zip?/*from ww  w . j a  v a2 s.co m*/
 *
 * @param zipFile
 * @param path
 * @param destFolder
 * @throws java.io.IOException
 */
public static File extractZipFileToFolder(File zipFile, String path, File destFolder) {
    ZipFile zip;
    File destFile = null;
    try {
        zip = new ZipFile(zipFile);
        ZipArchiveEntry zipArchiveEntry = zip.getEntry(path);
        if (null != zipArchiveEntry) {
            String name = zipArchiveEntry.getName();
            name = FilenameUtils.getName(name);
            destFile = new File(destFolder, name);
            FileMkUtils.mkdirs(destFolder);
            destFile.createNewFile();
            InputStream is = zip.getInputStream(zipArchiveEntry);
            FileOutputStream fos = new FileOutputStream(destFile);
            int length = 0;
            byte[] b = new byte[1024];
            while ((length = is.read(b, 0, 1024)) != -1) {
                fos.write(b, 0, length);
            }
            is.close();
            fos.close();
        }
        if (null != zip) {
            ZipFile.closeQuietly(zip);
        }
    } catch (IOException e) {
        throw new GradleException(e.getMessage(), e);
    }
    return destFile;
}

From source file:com.taobao.android.utils.ZipUtils.java

public static List<String> listZipEntries(File zipFile) {
    List<String> list = new ArrayList<String>();
    ZipFile zip;/*from w  w w .j a  va 2  s . c  om*/
    try {
        zip = new ZipFile(zipFile);
        Enumeration<ZipArchiveEntry> en = zip.getEntries();
        ZipArchiveEntry ze = null;
        while (en.hasMoreElements()) {
            ze = en.nextElement();
            String name = ze.getName();
            list.add(name);
        }
        if (null != zip)
            ZipFile.closeQuietly(zip);
    } catch (IOException e) {
    }
    return list;
}

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

void setEncodingByDetection() throws IOException {
    UniversalDetector filenameCharsetDetector = new UniversalDetector(null);
    ZipFile zipFile = new ZipFile(zipFilename);
    try {//from  www .j av  a  2s. c  o m
        for (Enumeration<ZipArchiveEntry> zipArchiveEntryEnumeration = zipFile
                .getEntries(); zipArchiveEntryEnumeration.hasMoreElements();) {
            ZipArchiveEntry entry = zipArchiveEntryEnumeration.nextElement();
            if (!entry.getGeneralPurposeBit().usesUTF8ForNames()) {
                byte[] filename = entry.getRawName();
                filenameCharsetDetector.handleData(filename, 0, filename.length);
            }
        }
    } finally {
        ZipFile.closeQuietly(zipFile);
    }
    filenameCharsetDetector.dataEnd();
    this.encoding = filenameCharsetDetector.getDetectedCharset();
}

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

void listZipArchiveEntries() throws IOException {
    ZipFile zipFile = new ZipFile(zipFilename, encoding);
    try {/*  ww w  .  j av a2  s . com*/
        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 w  w  w .jav a  2s .  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.apache.ant.compress.resources.ZipResource.java

/**
 * fetches information from the named entry inside the archive.
 *///from  w w w.  j a v a  2s  .  co m
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   w w  w. j  a v a2  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.apache.ant.compress.taskdefs.Unzip.java

protected void expandFile(FileUtils fileUtils, File srcF, File dir) {
    log("Expanding: " + srcF + " into " + dir, Project.MSG_INFO);
    ZipFile zf = null;//from   w  w  w.j  a  v  a2s. c  om
    FileNameMapper mapper = getMapper();
    if (!srcF.exists()) {
        throw new BuildException("Unable to expand " + srcF + " as the file does not exist", getLocation());
    }
    try {
        zf = new ZipFile(srcF, getEncoding(), true);
        boolean empty = true;
        Enumeration e = zf.getEntries();
        while (e.hasMoreElements()) {
            empty = false;
            ZipArchiveEntry ze = (ZipArchiveEntry) e.nextElement();
            if (getSkipUnreadableEntries() && !zf.canReadEntryData(ze)) {
                log(Messages.skippedIsUnreadable(ze));
                continue;
            }
            log("extracting " + ze.getName(), Project.MSG_DEBUG);
            InputStream is = null;
            try {
                extractFile(fileUtils, srcF, dir, is = zf.getInputStream(ze), ze.getName(),
                        new Date(ze.getTime()), ze.isDirectory(), mapper);
            } finally {
                FileUtils.close(is);
            }
        }
        if (empty && getFailOnEmptyArchive()) {
            throw new BuildException("archive '" + srcF + "' is empty");
        }
        log("expand complete", Project.MSG_VERBOSE);
    } catch (IOException ioe) {
        throw new BuildException("Error while expanding " + srcF.getPath() + "\n" + ioe.toString(), ioe);
    } finally {
        ZipFile.closeQuietly(zf);
    }
}

From source file:org.artificer.atom.archive.ArchiveUtils.java

/**
 * Unpacks the given archive file into the output directory.
 * @param archiveFile an archive file/*from  w ww.j av  a  2 s  .  co  m*/
 * @param toDir where to unpack the archive to
 * @throws IOException
 */
public static void unpackToWorkDir(File archiveFile, File toDir) throws IOException {
    ZipFile zipFile = null;
    try {
        zipFile = new ZipFile(archiveFile);
        Enumeration<ZipArchiveEntry> zipEntries = zipFile.getEntriesInPhysicalOrder();
        while (zipEntries.hasMoreElements()) {
            ZipArchiveEntry entry = zipEntries.nextElement();
            String entryName = entry.getName();
            File outFile = new File(toDir, entryName);
            if (!outFile.getParentFile().exists()) {
                if (!outFile.getParentFile().mkdirs()) {
                    throw new IOException(Messages.i18n.format("FAILED_TO_CREATE_PARENT_DIR",
                            outFile.getParentFile().getCanonicalPath()));
                }
            }

            if (entry.isDirectory()) {
                if (outFile.isDirectory()) {
                    // Do nothing - already created.
                } else if (outFile.isFile()) {
                    throw new IOException(
                            Messages.i18n.format("FAILED_TO_CREATE_DIR", outFile.getCanonicalPath()));
                } else if (!outFile.mkdir()) {
                    throw new IOException(
                            Messages.i18n.format("FAILED_TO_CREATE_DIR", outFile.getCanonicalPath()));
                }
            } else {
                InputStream zipStream = null;
                OutputStream outFileStream = null;

                zipStream = zipFile.getInputStream(entry);
                outFileStream = new FileOutputStream(outFile);
                try {
                    IOUtils.copy(zipStream, outFileStream);
                } finally {
                    IOUtils.closeQuietly(zipStream);
                    IOUtils.closeQuietly(outFileStream);
                }
            }
        }
    } finally {
        ZipFile.closeQuietly(zipFile);
    }
}