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

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

Introduction

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

Prototype

public Enumeration getEntries() 

Source Link

Document

Returns all entries.

Usage

From source file:adams.core.io.ZipUtils.java

/**
 * Lists the files stored in the ZIP file.
 *
 * @param input   the ZIP file to obtain the file list from
 * @param listDirs   whether to include directories in the list
 * @return      the stored files/*from  www.j a va 2s .  c  o  m*/
 */
public static List<File> listFiles(File input, boolean listDirs) {
    List<File> result;
    ZipFile zipfile;
    Enumeration<ZipArchiveEntry> enm;
    ZipArchiveEntry entry;

    result = new ArrayList<>();
    zipfile = null;
    try {
        zipfile = new ZipFile(input.getAbsoluteFile());
        enm = zipfile.getEntries();
        while (enm.hasMoreElements()) {
            entry = enm.nextElement();

            // extract
            if (entry.isDirectory()) {
                if (listDirs)
                    result.add(new File(entry.getName()));
            } else {
                result.add(new File(entry.getName()));
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (zipfile != null) {
            try {
                zipfile.close();
            } catch (Exception e) {
                // ignored
            }
        }
    }

    return result;
}

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

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

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

/**
 * <p>//  w ww . j  ava  2s  . c o  m
 * unzip.
 * </p>
 *
 * @param zipFile     a {@link File} object.
 * @param destination a {@link String} object.
 * @param encoding    a {@link String} object.
 * @return a {@link List} object.
 */
public static List<String> unzip(final File zipFile, final String destination, String encoding) {
    List<String> fileNames = new ArrayList<String>();
    String dest = destination;
    if (!destination.endsWith("/")) {
        dest = destination + "/";
    }
    ZipFile file;
    try {
        file = null;
        if (null == encoding)
            file = new ZipFile(zipFile);
        else
            file = new ZipFile(zipFile, encoding);
        Enumeration<ZipArchiveEntry> en = file.getEntries();
        ZipArchiveEntry ze = null;
        while (en.hasMoreElements()) {
            ze = en.nextElement();
            File f = new File(dest, ze.getName());
            if (ze.isDirectory()) {
                f.mkdirs();
                continue;
            } else {
                f.getParentFile().mkdirs();
                InputStream is = file.getInputStream(ze);
                OutputStream os = new FileOutputStream(f);
                IOUtils.copy(is, os);
                is.close();
                os.close();
                fileNames.add(f.getAbsolutePath());
            }
        }
        file.close();
    } catch (IOException e) {
        throw new RuntimeException(e.getMessage(), e);
    }
    return fileNames;
}

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

/**
 * zip?//  w ww.  ja va  2 s  .  c  om
 *
 * @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.google.dart.tools.update.core.internal.UpdateUtils.java

/**
 * Unzip a zip file, notifying the given monitor along the way.
 *//*from  ww  w. jav  a2 s .c o m*/
public static void unzip(File zipFile, File destination, String taskName, IProgressMonitor monitor)
        throws IOException {

    ZipFile zip = new ZipFile(zipFile);

    //TODO (pquitslund): add real progress units
    if (monitor != null) {
        monitor.beginTask(taskName, 1);
    }

    Enumeration<ZipArchiveEntry> e = zip.getEntries();
    while (e.hasMoreElements()) {
        ZipArchiveEntry entry = e.nextElement();
        File file = new File(destination, entry.getName());
        if (entry.isDirectory()) {
            file.mkdirs();
        } else {
            InputStream is = zip.getInputStream(entry);

            File parent = file.getParentFile();
            if (parent != null && parent.exists() == false) {
                parent.mkdirs();
            }

            FileOutputStream os = new FileOutputStream(file);
            try {
                IOUtils.copy(is, os);
            } finally {
                os.close();
                is.close();
            }
            file.setLastModified(entry.getTime());

            int mode = entry.getUnixMode();

            if ((mode & EXEC_MASK) != 0) {
                file.setExecutable(true);
            }

        }
    }

    //TODO (pquitslund): fix progress units
    if (monitor != null) {
        monitor.worked(1);
        monitor.done();
    }

}

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

/**
 * <p>//ww  w .j  a v a 2  s .  co  m
 * unzip.
 * </p>
 *
 * @param zipFile     a {@link java.io.File} object.
 * @param destination a {@link String} object.
 * @param encoding    a {@link String} object.
 * @return a {@link java.util.List} object.
 */
public static List<String> unzip(final File zipFile, final String destination, String encoding) {
    List<String> fileNames = new ArrayList<String>();
    String dest = destination;
    if (!destination.endsWith(File.separator)) {
        dest = destination + File.separator;
    }
    ZipFile file;
    try {
        file = null;
        if (null == encoding) {
            file = new ZipFile(zipFile);
        } else {
            file = new ZipFile(zipFile, encoding);
        }
        Enumeration<ZipArchiveEntry> en = file.getEntries();
        ZipArchiveEntry ze = null;
        while (en.hasMoreElements()) {
            ze = en.nextElement();
            File f = new File(dest, ze.getName());
            if (ze.isDirectory()) {
                f.mkdirs();
                continue;
            } else {
                f.getParentFile().mkdirs();
                InputStream is = file.getInputStream(ze);
                OutputStream os = new FileOutputStream(f);
                IOUtils.copy(is, os);
                is.close();
                os.close();
                fileNames.add(f.getAbsolutePath());
            }
        }
        file.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return fileNames;
}

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

public static List<String> unzip(final File zipFile, final String destination, String encoding,
        Map<String, ZipEntry> zipEntryMethodMap, boolean isRelativePath) {
    List<String> fileNames = new ArrayList<String>();
    String dest = destination;//w w  w.  j av  a 2s.  c  om
    if (!destination.endsWith(File.separator)) {
        dest = destination + File.separator;
    }
    ZipFile file;
    try {
        file = null;
        if (null == encoding) {
            file = new ZipFile(zipFile);
        } else {
            file = new ZipFile(zipFile, encoding);
        }
        Enumeration<ZipArchiveEntry> en = file.getEntries();
        ZipArchiveEntry ze = null;
        while (en.hasMoreElements()) {
            ze = en.nextElement();
            File f = new File(dest, ze.getName());
            if (ze.isDirectory()) {
                f.mkdirs();
                continue;
            } else {
                f.getParentFile().mkdirs();
                InputStream is = file.getInputStream(ze);
                OutputStream os = new FileOutputStream(f);
                IOUtils.copy(is, os);
                is.close();
                os.close();
                fileNames.add(f.getAbsolutePath());
                if (zipEntryMethodMap != null && ze.getMethod() == STORED) {
                    zipEntryMethodMap.put(isRelativePath ? ze.getName() : f.getAbsolutePath(), ze);
                }
            }
        }
        file.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return fileNames;
}

From source file:adams.core.io.ZipUtils.java

/**
 * Unzips the specified file from a ZIP file.
 *
 * @param input   the ZIP file to unzip//w  w w.j  a  v  a 2  s  .  c o m
 * @param archiveFile   the file from the archive to extract
 * @param output   the name of the output file
 * @param createDirs   whether to create the directory structure represented
 *          by output file
 * @param bufferSize   the buffer size to use
 * @param errors   for storing potential errors
 * @return      whether file was successfully extracted
 */
public static boolean decompress(File input, String archiveFile, File output, boolean createDirs,
        int bufferSize, StringBuilder errors) {
    boolean result;
    ZipFile zipfile;
    Enumeration<ZipArchiveEntry> enm;
    ZipArchiveEntry entry;
    File outFile;
    String outName;
    byte[] buffer;
    BufferedInputStream in;
    BufferedOutputStream out;
    FileOutputStream fos;
    int len;
    String error;
    long read;

    result = false;
    zipfile = null;
    try {
        // unzip archive
        buffer = new byte[bufferSize];
        zipfile = new ZipFile(input.getAbsoluteFile());
        enm = zipfile.getEntries();
        while (enm.hasMoreElements()) {
            entry = enm.nextElement();

            if (entry.isDirectory())
                continue;
            if (!entry.getName().equals(archiveFile))
                continue;

            in = null;
            out = null;
            fos = null;
            outName = null;
            try {
                // output name
                outName = output.getAbsolutePath();

                // create directory, if necessary
                outFile = new File(outName).getParentFile();
                if (!outFile.exists()) {
                    if (!createDirs) {
                        error = "Output directory '" + outFile.getAbsolutePath() + " does not exist', "
                                + "skipping extraction of '" + outName + "'!";
                        System.err.println(error);
                        errors.append(error + "\n");
                        break;
                    } else {
                        if (!outFile.mkdirs()) {
                            error = "Failed to create directory '" + outFile.getAbsolutePath() + "', "
                                    + "skipping extraction of '" + outName + "'!";
                            System.err.println(error);
                            errors.append(error + "\n");
                            break;
                        }
                    }
                }

                // extract data
                in = new BufferedInputStream(zipfile.getInputStream(entry));
                fos = new FileOutputStream(outName);
                out = new BufferedOutputStream(fos, bufferSize);
                read = 0;
                while (read < entry.getSize()) {
                    len = in.read(buffer);
                    read += len;
                    out.write(buffer, 0, len);
                }

                result = true;
                break;
            } catch (Exception e) {
                result = false;
                error = "Error extracting '" + entry.getName() + "' to '" + outName + "': " + e;
                System.err.println(error);
                errors.append(error + "\n");
            } finally {
                FileUtils.closeQuietly(in);
                FileUtils.closeQuietly(out);
                FileUtils.closeQuietly(fos);
            }
        }
    } catch (Exception e) {
        result = false;
        e.printStackTrace();
        errors.append("Error occurred: " + e + "\n");
    } finally {
        if (zipfile != null) {
            try {
                zipfile.close();
            } catch (Exception e) {
                // ignored
            }
        }
    }

    return result;
}

From source file:adams.core.io.ZipUtils.java

/**
 * Unzips the files in a ZIP file. Files can be filtered based on their
 * filename, using a regular expression (the matching sense can be inverted).
 *
 * @param input   the ZIP file to unzip/* w  w w  .  ja v  a2 s.  c om*/
 * @param outputDir   the directory where to store the extracted files
 * @param createDirs   whether to re-create the directory structure from the
 *          ZIP file
 * @param match   the regular expression that the files are matched against
 * @param invertMatch   whether to invert the matching sense
 * @param bufferSize   the buffer size to use
 * @param errors   for storing potential errors
 * @return      the successfully extracted files
 */
@MixedCopyright(copyright = "Apache compress commons", license = License.APACHE2, url = "http://commons.apache.org/compress/examples.html")
public static List<File> decompress(File input, File outputDir, boolean createDirs, BaseRegExp match,
        boolean invertMatch, int bufferSize, StringBuilder errors) {
    List<File> result;
    ZipFile archive;
    Enumeration<ZipArchiveEntry> enm;
    ZipArchiveEntry entry;
    File outFile;
    String outName;
    byte[] buffer;
    BufferedInputStream in;
    BufferedOutputStream out;
    FileOutputStream fos;
    int len;
    String error;
    long read;

    result = new ArrayList<>();
    archive = null;
    try {
        // unzip archive
        buffer = new byte[bufferSize];
        archive = new ZipFile(input.getAbsoluteFile());
        enm = archive.getEntries();
        while (enm.hasMoreElements()) {
            entry = enm.nextElement();

            if (entry.isDirectory() && !createDirs)
                continue;

            // does name match?
            if (!match.isMatchAll() && !match.isEmpty()) {
                if (invertMatch && match.isMatch(entry.getName()))
                    continue;
                else if (!invertMatch && !match.isMatch(entry.getName()))
                    continue;
            }

            // extract
            if (entry.isDirectory() && createDirs) {
                outFile = new File(outputDir.getAbsolutePath() + File.separator + entry.getName());
                if (!outFile.mkdirs()) {
                    error = "Failed to create directory '" + outFile.getAbsolutePath() + "'!";
                    System.err.println(error);
                    errors.append(error + "\n");
                }
            } else {
                in = null;
                out = null;
                fos = null;
                outName = null;
                try {
                    // assemble output name
                    outName = outputDir.getAbsolutePath() + File.separator;
                    if (createDirs)
                        outName += entry.getName();
                    else
                        outName += new File(entry.getName()).getName();

                    // create directory, if necessary
                    outFile = new File(outName).getParentFile();
                    if (!outFile.exists()) {
                        if (!outFile.mkdirs()) {
                            error = "Failed to create directory '" + outFile.getAbsolutePath() + "', "
                                    + "skipping extraction of '" + outName + "'!";
                            System.err.println(error);
                            errors.append(error + "\n");
                            continue;
                        }
                    }

                    // extract data
                    in = new BufferedInputStream(archive.getInputStream(entry));
                    fos = new FileOutputStream(outName);
                    out = new BufferedOutputStream(fos, bufferSize);
                    read = 0;
                    while (read < entry.getSize()) {
                        len = in.read(buffer);
                        read += len;
                        out.write(buffer, 0, len);
                    }
                    result.add(new File(outName));
                } catch (Exception e) {
                    error = "Error extracting '" + entry.getName() + "' to '" + outName + "': " + e;
                    System.err.println(error);
                    errors.append(error + "\n");
                } finally {
                    FileUtils.closeQuietly(in);
                    FileUtils.closeQuietly(out);
                    FileUtils.closeQuietly(fos);
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        errors.append("Error occurred: " + e + "\n");
    } finally {
        if (archive != null) {
            try {
                archive.close();
            } catch (Exception e) {
                // ignored
            }
        }
    }

    return result;
}

From source file:de.crowdcode.movmvn.core.Unzipper.java

/**
 * Unzip a file.//  ww  w.j a v  a 2s .  c  om
 * 
 * @param archiveFile
 *            to be unzipped
 * @param outPath
 *            the place to put the result
 * @throws IOException
 *             exception
 * @throws ZipException
 *             exception
 */
public void unzipFileToDir(final File archiveFile, final File outPath) throws IOException, ZipException {
    ZipFile zipFile = new ZipFile(archiveFile);
    Enumeration<ZipArchiveEntry> e = zipFile.getEntries();
    while (e.hasMoreElements()) {
        ZipArchiveEntry entry = e.nextElement();
        File file = new File(outPath, entry.getName());
        if (entry.isDirectory()) {
            FileUtils.forceMkdir(file);
        } else {
            InputStream is = zipFile.getInputStream(entry);
            FileOutputStream os = FileUtils.openOutputStream(file);
            try {
                IOUtils.copy(is, os);
            } finally {
                os.close();
                is.close();
            }
            file.setLastModified(entry.getTime());
        }
    }
}