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

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

Introduction

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

Prototype

public void close() throws IOException 

Source Link

Document

Closes the archive.

Usage

From source file:at.spardat.xma.xdelta.JarPatcher.java

/**
 * Main method to make {@link #applyDelta(ZipFile, ZipFile, ZipArchiveOutputStream, BufferedReader)} available at
 * the command line.<br>/*from  w  w w .j  a  v a 2s .  co m*/
 * usage JarPatcher source patch output
 *
 * @param args the arguments
 * @throws IOException Signals that an I/O exception has occurred.
 */
public static void main(String[] args) throws IOException {
    String patchName = null;
    String outputName = null;
    String sourceName = null;
    if (args.length == 0) {
        System.err.println("usage JarPatcher patch [output [source]]");
        System.exit(1);
    } else {
        patchName = args[0];
        if (args.length > 1) {
            outputName = args[1];
            if (args.length > 2) {
                sourceName = args[2];
            }
        }
    }
    ZipFile patch = new ZipFile(patchName);
    ZipArchiveEntry listEntry = patch.getEntry("META-INF/file.list");
    if (listEntry == null) {
        System.err.println("Invalid patch - list entry 'META-INF/file.list' not found");
        System.exit(2);
    }
    BufferedReader list = new BufferedReader(new InputStreamReader(patch.getInputStream(listEntry)));
    String next = list.readLine();
    if (sourceName == null) {
        sourceName = next;
    }
    next = list.readLine();
    if (outputName == null) {
        outputName = next;
    }
    int ignoreSourcePaths = Integer.parseInt(System.getProperty("patcher.ignoreSourcePathElements", "0"));
    int ignoreOutputPaths = Integer.parseInt(System.getProperty("patcher.ignoreOutputPathElements", "0"));
    Path sourcePath = Paths.get(sourceName);
    Path outputPath = Paths.get(outputName);
    if (ignoreOutputPaths >= outputPath.getNameCount()) {
        patch.close();
        StringBuilder b = new StringBuilder().append("Not enough path elements to ignore in output (")
                .append(ignoreOutputPaths).append(" in ").append(outputName).append(")");
        throw new IOException(b.toString());
    }
    if (ignoreSourcePaths >= sourcePath.getNameCount()) {
        patch.close();
        StringBuilder b = new StringBuilder().append("Not enough path elements to ignore in source (")
                .append(sourcePath).append(" in ").append(sourceName).append(")");
        throw new IOException(b.toString());
    }
    sourcePath = sourcePath.subpath(ignoreSourcePaths, sourcePath.getNameCount());
    outputPath = outputPath.subpath(ignoreOutputPaths, outputPath.getNameCount());
    File sourceFile = sourcePath.toFile();
    File outputFile = outputPath.toFile();
    if (!(outputFile.getAbsoluteFile().getParentFile().mkdirs()
            || outputFile.getAbsoluteFile().getParentFile().exists())) {
        patch.close();
        throw new IOException("Failed to create " + outputFile.getAbsolutePath());
    }
    new JarPatcher(patchName, sourceFile.getName()).applyDelta(patch, new ZipFile(sourceFile),
            new ZipArchiveOutputStream(new FileOutputStream(outputFile)), list);
    list.close();
}

From source file:com.android.tradefed.util.ZipUtil2.java

/**
 * Close an open {@link ZipFile}, ignoring any exceptions.
 *
 * @param zipFile the file to close//w w w.  jav a 2  s  .co m
 */
public static void closeZip(ZipFile zipFile) {
    if (zipFile != null) {
        try {
            zipFile.close();
        } catch (IOException e) {
            // ignore
        }
    }
}

From source file:de.nx42.maps4cim.util.Compression.java

/**
 * Reads the first file entry in a zip file and returns it's contents
 * as uncompressed byte-array//from  w w  w  . j a v a  2 s  .c o  m
 * @param zipFile the zip file to read from
 * @return the first file entry (uncompressed)
 * @throws IOException if there is an error accessing the zip file
 */
public static byte[] readFirstZipEntry(File zipFile) throws IOException {
    // open zip
    ZipFile zf = new ZipFile(zipFile);
    Enumeration<ZipArchiveEntry> entries = zf.getEntries();

    // read first entry to byte[]
    ZipArchiveEntry entry = entries.nextElement();
    InputStream is = zf.getInputStream(entry);
    byte[] raw = ByteStreams.toByteArray(is);

    // close all streams and return byte[]
    is.close();
    zf.close();
    return raw;
}

From source file:de.nx42.maps4cim.util.Compression.java

/**
 * Reads the first file entry in a zip file and writes it in uncompressed
 * form to the desired file./*from  w w  w .j  ava  2s  .  co  m*/
 * @param zipFile the zip file to read from
 * @param dest the file to write the first zip file entry to
 * @return same as destination
 * @throws IOException if there is an error accessing the zip file or the
 * destination file
 */
public static File readFirstZipEntry(File zipFile, File dest) throws IOException {
    // open zip and get first entry
    ZipFile zf = new ZipFile(zipFile);
    Enumeration<ZipArchiveEntry> entries = zf.getEntries();
    ZipArchiveEntry entry = entries.nextElement();

    // write to file
    InputStream in = zf.getInputStream(entry);
    OutputStream out = new FileOutputStream(dest);
    ByteStreams.copy(in, out);

    // close all streams and return the new file
    in.close();
    out.close();
    zf.close();
    return dest;
}

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

/**
 * <p>/*  w  w  w . j  a  va2 s.co m*/
 * isZipFile.
 * </p>
 *
 * @param zipFile a {@link java.io.File} object.
 * @return a boolean.
 */
public static boolean isZipFile(File zipFile) {
    try {
        ZipFile zf = new ZipFile(zipFile);
        boolean isZip = zf.getEntries().hasMoreElements();
        zf.close();
        return isZip;
    } catch (IOException e) {
        return false;
    }
}

From source file:abfab3d.io.input.ModelLoader.java

/**
 * Unzip a file into a destination directory
 *
 * @param src//from  w w  w.  ja v  a  2s  . c om
 * @param dest
 */
private static void unzip(File src, File dest) throws IOException {
    ZipFile zipFile = null;

    try {
        zipFile = new ZipFile(src);

        for (Enumeration e = zipFile.getEntries(); e.hasMoreElements();) {
            ZipArchiveEntry entry = (ZipArchiveEntry) e.nextElement();
            unzipEntry(zipFile, entry, dest);
        }
    } finally {
        if (zipFile != null)
            zipFile.close();
    }
}

From source file:com.google.dart.tools.update.core.internal.UpdateUtils.java

/**
 * Check if the given zip file is valid.
 * /* w w  w  .  j  av a  2 s .c o  m*/
 * @param zip the zip to check
 * @return <code>true</code> if it's valid, <code>false</code> otherwise.
 */
public static boolean isZipValid(final File zip) {
    java.util.zip.ZipFile zipfile = null;
    try {
        zipfile = new java.util.zip.ZipFile(zip);
        return true;
    } catch (ZipException e) {
        return false;
    } catch (IOException e) {
        return false;
    } finally {
        try {
            if (zipfile != null) {
                zipfile.close();
                zipfile = null;
            }
        } catch (IOException e) {
        }
    }
}

From source file:com.taobao.android.tpatch.utils.PatchUtils.java

/**
 * ?so?bundle//from  w  w w  .j a v  a  2  s.  c o m
 *
 * @param soFile
 * @return
 */
public static boolean isBundleFile(File soFile) {
    ZipFile zf = null;
    try {
        zf = new ZipFile(soFile);
        Enumeration<ZipArchiveEntry> en = zf.getEntries();
        if (en.hasMoreElements()) {
            for (String name : BUNDLE_FILES) {
                ZipArchiveEntry zipArchiveEntry = zf.getEntry(name);
                if (null == zipArchiveEntry) {
                    return false;
                }

            }
            return true;
        }
        return false;
    } catch (IOException e) {
        return false;
    } finally {
        if (null != zf) {
            try {
                zf.close();
            } catch (IOException e) {
            }
        }
    }
}

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

/**
 * zip?/*from  ww  w . j  a va 2  s. c  o  m*/
 *
 * @param zipFile
 * @param pathName
 * @return
 */
public static boolean isFolderExist(File zipFile, String pathName) {

    ZipFile file = null;
    try {
        file = new ZipFile(zipFile);
        Enumeration<ZipArchiveEntry> en = file.getEntries();
        while (en.hasMoreElements()) {
            ZipArchiveEntry entry = en.nextElement();
            String name = entry.getName();
            if (name.startsWith(pathName)) {
                return true;
            }

        }
        return false;
    } catch (IOException e) {
    } finally {
        if (null != file) {
            try {
                file.close();
            } catch (IOException e) {

            }
        }

    }
    return false;
}

From source file:com.asakusafw.shafu.core.util.IoUtils.java

/**
 * Extracts a {@code *.zip} archive into the target folder.
 * @param monitor the progress monitor//from w w w.ja v a  2 s .c o  m
 * @param archiveFile the archive file
 * @param targetDirectory the target folder
 * @throws IOException if failed to extract the archive
 */
public static void extractZip(IProgressMonitor monitor, File archiveFile, File targetDirectory)
        throws IOException {
    SubMonitor sub = SubMonitor.convert(monitor, Messages.IoUtils_monitorExtractZip, 10);
    try {
        ZipFile zip = new ZipFile(archiveFile);
        try {
            Enumeration<ZipArchiveEntry> entries = zip.getEntries();
            while (entries.hasMoreElements()) {
                ZipArchiveEntry entry = entries.nextElement();
                if (entry.isDirectory()) {
                    createDirectory(targetDirectory, entry);
                } else {
                    InputStream input = zip.getInputStream(entry);
                    try {
                        File file = createFile(targetDirectory, entry, input);
                        setFileMode(file, entry.getUnixMode());
                    } finally {
                        input.close();
                    }
                    sub.worked(1);
                    sub.setWorkRemaining(10);
                }
            }
        } finally {
            zip.close();
        }
    } finally {
        if (monitor != null) {
            monitor.done();
        }
    }
}