Example usage for java.util.zip ZipFile entries

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

Introduction

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

Prototype

public Enumeration<? extends ZipEntry> entries() 

Source Link

Document

Returns an enumeration of the ZIP file entries.

Usage

From source file:b2s.idea.mavenize.JarCombiner.java

private static void copyContentsOf(File input, ZipOutputStream output, JarContext context) throws IOException {
    ZipFile zip = null;
    try {/*  w  w  w. j a  v  a  2  s  . c  om*/
        zip = new ZipFile(input);
        Enumeration<? extends ZipEntry> entries = zip.entries();
        while (entries.hasMoreElements()) {
            ZipEntry entry = entries.nextElement();
            if (fileCanBeIgnored(entry, context)) {
                continue;
            }

            if (isServiceEntry(entry)) {
                context.addService(entry.getName(), contentsOf(entry, zip));
                continue;
            }

            output.putNextEntry(new ZipEntry(entry.getName()));
            output.write(contentsOf(entry, zip));
            output.flush();
            output.closeEntry();

            context.addVisitedEntry(entry);
        }
    } finally {
        close(zip);
    }
}

From source file:com.stevpet.sonar.plugins.dotnet.mscover.codecoverage.command.ZipUtils.java

/**
 * Extracts the specified folder from the specified archive, into the supplied output directory.
 * // w w  w.  j  av a2 s. com
 * @param archivePath
 *          the archive Path
 * @param folderToExtract
 *          the folder to extract
 * @param outputDirectory
 *          the output directory
 * @return the extracted folder path
 * @throws IOException
 *           if a problem occurs while extracting
 */
public static File extractArchiveFolderIntoDirectory(String archivePath, String folderToExtract,
        String outputDirectory) throws IOException {
    File destinationFolder = new File(outputDirectory);
    destinationFolder.mkdirs();

    ZipFile zip = null;
    try {
        zip = new ZipFile(new File(archivePath));
        Enumeration<?> zipFileEntries = zip.entries();
        // Process each entry
        while (zipFileEntries.hasMoreElements()) {
            ZipEntry entry = (ZipEntry) zipFileEntries.nextElement();
            String currentEntry = entry.getName();
            if (currentEntry.startsWith(folderToExtract)) {
                File destFile = new File(destinationFolder, currentEntry);
                destFile.getParentFile().mkdirs();
                if (!entry.isDirectory()) {
                    BufferedInputStream is = null;
                    BufferedOutputStream dest = null;
                    try {
                        is = new BufferedInputStream(zip.getInputStream(entry));
                        int currentByte;
                        // establish buffer for writing file
                        byte data[] = new byte[BUFFER_SIZE];

                        // write the current file to disk
                        FileOutputStream fos = new FileOutputStream(destFile);
                        dest = new BufferedOutputStream(fos, BUFFER_SIZE);

                        // read and write until last byte is encountered
                        while ((currentByte = is.read(data, 0, BUFFER_SIZE)) != -1) {
                            dest.write(data, 0, currentByte);
                        }
                    } finally {
                        if (dest != null) {
                            dest.flush();
                        }
                        IOUtils.closeQuietly(dest);
                        IOUtils.closeQuietly(is);
                    }
                }
            }
        }
    } finally {
        if (zip != null) {
            zip.close();
        }
    }

    return new File(destinationFolder, folderToExtract);
}

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;
    try {/*from  w  w  w .ja  va 2s .  c  om*/
        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:de.rub.syssec.saaf.analysis.steps.extract.ApkUnzipper.java

/**
 * Extracts the given archive into the given destination directory
 * /*from w  ww.j av  a2s.  c  o  m*/
 * @param archive
 *            - the file to extract
 * @param dest
 *            - the destination directory
 * @throws Exception
 */
public static void extractArchive(File archive, File destDir) throws IOException {

    if (!destDir.exists()) {
        destDir.mkdir();
    }

    ZipFile zipFile = new ZipFile(archive);
    Enumeration<? extends ZipEntry> entries = zipFile.entries();

    byte[] buffer = new byte[16384];
    int len;
    while (entries.hasMoreElements()) {
        ZipEntry entry = entries.nextElement();

        String entryFileName = entry.getName();

        File dir = buildDirectoryHierarchyFor(entryFileName, destDir);
        if (!dir.exists()) {
            dir.mkdirs();
        }

        if (!entry.isDirectory()) {

            File file = new File(destDir, entryFileName);

            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));

            BufferedInputStream bis = new BufferedInputStream(zipFile.getInputStream(entry));

            while ((len = bis.read(buffer)) > 0) {
                bos.write(buffer, 0, len);
            }

            bos.flush();
            bos.close();
            bis.close();
        }
    }
    zipFile.close();
}

From source file:android.databinding.tool.util.GenerationalClassUtil.java

private static void loadFomZipFile(File file) throws IOException {
    ZipFile zipFile = new ZipFile(file);
    Enumeration<? extends ZipEntry> entries = zipFile.entries();
    while (entries.hasMoreElements()) {
        ZipEntry entry = entries.nextElement();
        for (ExtensionFilter filter : ExtensionFilter.values()) {
            if (!filter.accept(entry.getName())) {
                continue;
            }/*ww w  . jav  a2  s.com*/
            InputStream inputStream = null;
            try {
                inputStream = zipFile.getInputStream(entry);
                Serializable item = fromInputStream(inputStream);
                L.d("loaded item %s from zip file", item);
                if (item != null) {
                    //noinspection unchecked
                    sCache[filter.ordinal()].add(item);
                }
            } catch (IOException e) {
                L.e(e, "Could not merge in Bindables from %s", file.getAbsolutePath());
            } catch (ClassNotFoundException e) {
                L.e(e, "Could not read Binding properties intermediate file. %s", file.getAbsolutePath());
            } finally {
                IOUtils.closeQuietly(inputStream);
            }
        }
    }
}

From source file:Main.java

public static File UpdateZipFromPath(String sZipFile, String sPath) throws Exception {
    File tmpFile = File.createTempFile("z4zip-tmp-", ".zip");
    tmpFile.deleteOnExit();//from w  ww.  j  ava 2 s .com
    ZipFile inZip = new ZipFile(sZipFile);
    ZipOutputStream outZip = new ZipOutputStream(new FileOutputStream(tmpFile.getPath()));

    Enumeration<? extends ZipEntry> entries = inZip.entries();
    while (entries.hasMoreElements()) {
        ZipEntry e = entries.nextElement();
        outZip.putNextEntry(e);
        if (!e.isDirectory()) {
            File f = new File(sPath + "/" + e.getName());
            if (f.exists()) {
                copy(new FileInputStream(f.getPath()), outZip);
            } else {
                copy(inZip.getInputStream(e), outZip);
            }
        }
        outZip.closeEntry();
    }
    inZip.close();
    outZip.close();
    return tmpFile;
}

From source file:com.aliyun.odps.local.common.utils.ArchiveUtils.java

/**
 * Given a File input it will unzip the file in a the unzip directory passed
 * as the second parameter//  w  w  w  .j  a  v  a  2 s .  c o  m
 * 
 * @param inFile
 *          The zip file as input
 * @param unzipDir
 *          The unzip directory where to unzip the zip file.
 * @throws IOException
 */
public static void unZip(File inFile, File unzipDir) throws IOException {
    Enumeration<? extends ZipEntry> entries;
    ZipFile zipFile = new ZipFile(inFile);

    try {
        entries = zipFile.entries();
        while (entries.hasMoreElements()) {
            ZipEntry entry = entries.nextElement();
            if (!entry.isDirectory()) {
                InputStream in = zipFile.getInputStream(entry);
                try {
                    File file = new File(unzipDir, entry.getName());
                    if (!file.getParentFile().mkdirs()) {
                        if (!file.getParentFile().isDirectory()) {
                            throw new IOException("Mkdirs failed to create " + file.getParentFile().toString());
                        }
                    }
                    OutputStream out = new FileOutputStream(file);
                    try {
                        byte[] buffer = new byte[8192];
                        int i;
                        while ((i = in.read(buffer)) != -1) {
                            out.write(buffer, 0, i);
                        }
                    } finally {
                        out.close();
                    }
                } finally {
                    in.close();
                }
            }
        }
    } finally {
        zipFile.close();
    }
}

From source file:nz.co.fortytwo.freeboard.installer.ZipUtils.java

/**
 * Unzip a zipFile into a directory//ww  w .  j a v  a 2s . c  o  m
 * @param targetDir
 * @param zipFile
 * @throws ZipException
 * @throws IOException
 */
public static void unzip(File targetDir, File zipFile) throws ZipException, IOException {
    ZipFile zip = new ZipFile(zipFile);

    @SuppressWarnings("unchecked")
    Enumeration<ZipEntry> z = (Enumeration<ZipEntry>) zip.entries();
    while (z.hasMoreElements()) {
        ZipEntry entry = z.nextElement();
        File f = new File(targetDir, entry.getName());
        if (f.isDirectory()) {
            if (!f.exists()) {
                f.mkdirs();
            }
        } else {
            f.getParentFile().mkdirs();
            InputStream in = zip.getInputStream(entry);
            BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(f));
            IOUtils.copy(in, out);
            in.close();
            out.flush();
            out.close();
        }

    }
    zip.close();
}

From source file:Main.java

public static List<File> unzip(File zip, File toDir) throws IOException {
    ZipFile zf = null;
    List<File> files = null;
    try {/*from   ww w .  j  a va 2s  . c  o m*/
        zf = new ZipFile(zip);
        files = new ArrayList<File>();
        Enumeration<?> entries = zf.entries();
        while (entries.hasMoreElements()) {
            ZipEntry entry = (ZipEntry) entries.nextElement();
            if (entry.isDirectory()) {
                new File(toDir, entry.getName()).mkdirs();
                continue;
            }

            InputStream input = null;
            OutputStream output = null;
            try {
                File f = new File(toDir, entry.getName());
                input = zf.getInputStream(entry);
                output = new FileOutputStream(f);
                copy(input, output);
                files.add(f);
            } finally {
                closeQuietly(output);
                closeQuietly(input);
            }
        }
    } finally {
        if (zf != null) {
            zf.close();
        }
    }
    return files;
}

From source file:org.codice.ddf.admin.application.service.impl.ApplicationFileInstaller.java

/**
 * Loops through all of zipFile's entries to find the features.xml file.
 *
 * @param zipFile The ZipFile to search for the feature file.
 * @return The ZipEntry representing the features.xml file.
 */// w  w  w.j  a  v a  2s .  co  m
private static ZipEntry getFeatureFile(ZipFile zipFile) {
    Enumeration<?> entries = zipFile.entries();
    while (entries.hasMoreElements()) {
        ZipEntry curEntry = (ZipEntry) entries.nextElement();
        if (!curEntry.isDirectory() && !curEntry.getName().startsWith("META-INF")) {
            if (isFeatureFile(curEntry)) {
                return curEntry;
            }
        }
    }
    return null;
}