Example usage for java.util.zip ZipFile ZipFile

List of usage examples for java.util.zip ZipFile ZipFile

Introduction

In this page you can find the example usage for java.util.zip ZipFile ZipFile.

Prototype

public ZipFile(File file) throws ZipException, IOException 

Source Link

Document

Opens a ZIP file for reading given the specified File object.

Usage

From source file:com.vamonossoftware.core.ZipUtil.java

public static int unzip(File zip, File dest) {
    try {//from   w  ww. java 2 s  .  c  om
        ZipFile zf = new ZipFile(zip);
        int count = 0;
        for (Enumeration entries = zf.entries(); entries.hasMoreElements();) {
            ZipEntry entry = (ZipEntry) entries.nextElement();
            File destfile = new File(dest, entry.getName());
            if (entry.isDirectory()) {
                destfile.mkdirs();
            } else {
                IOUtils.copy(zf.getInputStream(entry), new FileOutputStream(destfile));
            }
        }
        return count;
    } catch (IOException e) {
        return -1;
    }
}

From source file:Main.java

public static String[] getXmlFiles(String path) {
    List<String> xmlFiles = new ArrayList<>();
    ZipFile zipFile = null;//w ww . j a v a 2s . c  o m
    try {
        zipFile = new ZipFile(path);
        Enumeration<? extends ZipEntry> entries = zipFile.entries();
        while (entries.hasMoreElements()) {
            ZipEntry entry = entries.nextElement();
            String name = entry.getName();
            if (name.endsWith(".xml") && !name.equals("AndroidManifest.xml")) {
                xmlFiles.add(name);
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (zipFile != null) {
            try {
                zipFile.close();
            } catch (IOException ignored) {
            }
        }
    }
    Collections.sort(xmlFiles);
    return xmlFiles.toArray(new String[xmlFiles.size()]);
}

From source file:Main.java

public static String getJarSignature(String packagePath) throws IOException {
    Pattern signatureFilePattern = Pattern.compile("META-INF/[A-Z]+\\.SF");

    ZipFile packageZip = null;//from ww w .  j  ava 2 s .  c o m
    try {
        packageZip = new ZipFile(packagePath);
        // For each file in the zip.
        for (ZipEntry entry : Collections.list(packageZip.entries())) {
            // Ignore non-signature files.
            if (!signatureFilePattern.matcher(entry.getName()).matches()) {
                continue;
            }

            BufferedReader sigContents = null;
            try {
                sigContents = new BufferedReader(new InputStreamReader(packageZip.getInputStream(entry)));
                // For each line in the signature file.
                while (true) {
                    String line = sigContents.readLine();
                    if (line == null || line.equals("")) {
                        throw new IllegalArgumentException(
                                "Failed to find manifest digest in " + entry.getName());
                    }
                    String prefix = "SHA1-Digest-Manifest: ";
                    if (line.startsWith(prefix)) {
                        return line.substring(prefix.length());
                    }
                }
            } finally {
                if (sigContents != null) {
                    sigContents.close();
                }
            }
        }
    } finally {
        if (packageZip != null) {
            packageZip.close();
        }
    }

    throw new IllegalArgumentException("Failed to find signature file.");
}

From source file:com.mc.printer.model.utils.ZipHelper.java

public static void unZip(String sourceZip, String outDirName) throws IOException {
    log.info("unzip source:" + sourceZip);
    log.info("unzip to :" + outDirName);
    ZipFile zfile = new ZipFile(sourceZip);
    System.out.println(zfile.getName());
    Enumeration zList = zfile.entries();
    ZipEntry ze = null;/*from   ww  w  .  jav  a  2 s  .c o m*/
    byte[] buf = new byte[1024];
    while (zList.hasMoreElements()) {
        //ZipFileZipEntry
        ze = (ZipEntry) zList.nextElement();
        if (ze.isDirectory()) {
            continue;
        }
        //ZipEntry?InputStreamOutputStream
        File fil = getRealFileName(outDirName, ze.getName());

        OutputStream os = new BufferedOutputStream(new FileOutputStream(fil));
        InputStream is = new BufferedInputStream(zfile.getInputStream(ze));
        int readLen = 0;
        while ((readLen = is.read(buf, 0, 1024)) != -1) {
            os.write(buf, 0, readLen);
        }
        is.close();
        os.close();
        log.debug("Extracted: " + ze.getName());
    }
    zfile.close();

}

From source file:Main.java

/**
 * Uncompresses zipped files//from   w  w w . j  a  v  a 2 s  .  c  om
 * @param zippedFile The file to uncompress
 * @param destinationDir Where to put the files
 * @return  list of unzipped files
 * @throws java.io.IOException thrown if there is a problem finding or writing the files
 */
public static List<File> unzip(File zippedFile, File destinationDir) throws IOException {
    int buffer = 2048;

    List<File> unzippedFiles = new ArrayList<File>();

    BufferedOutputStream dest;
    BufferedInputStream is;
    ZipEntry entry;
    ZipFile zipfile = new ZipFile(zippedFile);
    Enumeration e = zipfile.entries();
    while (e.hasMoreElements()) {
        entry = (ZipEntry) e.nextElement();

        is = new BufferedInputStream(zipfile.getInputStream(entry));
        int count;
        byte data[] = new byte[buffer];

        File destFile;

        if (destinationDir != null) {
            destFile = new File(destinationDir, entry.getName());
        } else {
            destFile = new File(entry.getName());
        }

        FileOutputStream fos = new FileOutputStream(destFile);
        dest = new BufferedOutputStream(fos, buffer);
        try {
            while ((count = is.read(data, 0, buffer)) != -1) {
                dest.write(data, 0, count);
            }

            unzippedFiles.add(destFile);
        } finally {
            dest.flush();
            dest.close();
            is.close();
        }
    }

    return unzippedFiles;
}

From source file:com.opoopress.maven.plugins.plugin.zip.ZipUtils.java

public static void unzipFileToDirectory(File file, File destDir, boolean keepTimestamp) throws IOException {
    // create output directory if it doesn't exist
    if (!destDir.exists())
        destDir.mkdirs();/*w ww .  jav  a  2  s . co  m*/

    ZipFile zipFile = new ZipFile(file);
    Enumeration<? extends ZipEntry> entries = zipFile.entries();
    while (entries.hasMoreElements()) {
        ZipEntry entry = entries.nextElement();
        if (entry.isDirectory()) {
            new File(destDir, entry.getName()).mkdirs();
            continue;
        }

        InputStream inputStream = zipFile.getInputStream(entry);
        File destFile = new File(destDir, entry.getName());

        System.out.println("Unzipping to " + destFile.getAbsolutePath());
        FileUtils.copyInputStreamToFile(inputStream, destFile);
        IOUtils.closeQuietly(inputStream);

        if (keepTimestamp) {
            long time = entry.getTime();
            if (time > 0) {
                destFile.setLastModified(time);
            }
        }
    }

    zipFile.close();
}

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();// w w w . 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:com.compomics.pladipus.core.control.util.ZipUtils.java

/**
 * Unzips a file to the specified folder
 *
 * @param archive the zipped folder// w w w.j a  v a  2s  . c  o m
 * @param outputDir the destination folder
 */
public static void unzipArchive(File archive, File outputDir) {
    try (ZipFile zipfile = new ZipFile(archive)) {
        for (Enumeration e = zipfile.entries(); e.hasMoreElements();) {
            ZipEntry entry = (ZipEntry) e.nextElement();
            unzipEntry(zipfile, entry, outputDir);
        }
        unzipped = true;
        zipfile.close();
    } catch (Exception e) {
        LOGGER.error("Error while extracting file " + archive, e);
    } finally {
        if (unzipped) {
            archive.delete();
        }
    }
}

From source file:de.shadowhunt.subversion.internal.AbstractPrepare.java

private static boolean extractArchive(final File zip, final File prefix) throws Exception {
    final ZipFile zipFile = new ZipFile(zip);
    final Enumeration<? extends ZipEntry> enu = zipFile.entries();
    while (enu.hasMoreElements()) {
        final ZipEntry zipEntry = enu.nextElement();

        final String name = zipEntry.getName();

        final File file = new File(prefix, name);
        if (name.charAt(name.length() - 1) == Resource.SEPARATOR_CHAR) {
            if (!file.isDirectory() && !file.mkdirs()) {
                throw new IOException("can not create directory structure: " + file);
            }//from   w w  w .j  av  a  2 s  . c  o m
            continue;
        }

        final File parent = file.getParentFile();
        if (parent != null) {
            if (!parent.isDirectory() && !parent.mkdirs()) {
                throw new IOException("can not create directory structure: " + parent);
            }
        }

        final InputStream is = zipFile.getInputStream(zipEntry);
        final FileOutputStream fos = new FileOutputStream(file);
        final byte[] bytes = new byte[1024];
        int length;
        while ((length = is.read(bytes)) >= 0) {
            fos.write(bytes, 0, length);
        }
        is.close();
        fos.close();

    }
    zipFile.close();
    return true;
}

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()));
    }// www .ja v 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);
    }
}