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:Main.java

public static Enumeration<?> getEntriesEnumeration(File zipFile) throws IOException {
    ZipFile zf = new ZipFile(zipFile);
    Enumeration<?> entry = zf.entries();
    zf.close();//from www  .  ja v  a 2  s.c  om
    return entry;
}

From source file:Utils.java

/**
 * Validate that an archive contains a named entry
 * //from  w w w.  j  ava2s .c  o  m
 * @param theFile
 * @param name
 * @return true if the entry exists
 * @throws IOException
 */
public static boolean archiveContainsEntry(File theFile, String name) throws IOException {
    boolean result = false;
    ZipFile zipFile;
    zipFile = new ZipFile(theFile);
    for (Enumeration entries = zipFile.entries(); entries.hasMoreElements();) {
        ZipEntry entry = (ZipEntry) entries.nextElement();
        if (entry.getName().equals(name)) {
            result = true;
            break;
        }
    }
    zipFile.close();
    return result;
}

From source file:com.skcraft.launcher.builder.BuilderUtils.java

public static ZipEntry getZipEntry(ZipFile jarFile, String path) {
    Enumeration<? extends ZipEntry> entries = jarFile.entries();
    String expected = normalizePath(path);

    while (entries.hasMoreElements()) {
        ZipEntry entry = entries.nextElement();
        String test = normalizePath(entry.getName());
        if (expected.equals(test)) {
            return entry;
        }//from  w  w  w  . j ava  2s.  co m
    }

    return null;
}

From source file:Main.java

/**
 * To check if the apk file is available to install.
 * //from www.jav  a2 s  .  co m
 * @param apkFilePath the apk file path
 * @return            true if available, otherwise return false
 */
public static boolean isApkAvailable(String apkFilePath) {
    File apkFile = new File(apkFilePath);
    if (!apkFile.exists()) {
        return false;
    }

    try {
        ZipFile zipFile = new ZipFile(apkFile);
        Enumeration<? extends ZipEntry> entries = zipFile.entries();
        while (entries.hasMoreElements()) {
            ZipEntry zipEntry = entries.nextElement();
            if (APK_MANIFEST.equals(zipEntry.getName())) {
                zipFile.close();
                return true;
            }
        }

        zipFile.close();
    } catch (Exception e) {
        return false;
    }

    return false;
}

From source file:Main.java

public static ZipFile readZipEntriesForElectric(File archive,
        final HashMap<String, ZipEntry> compressionEntries) {
    ZipFile zipfile = null;
    try {//  w  w w .j a  v  a  2s .com
        zipfile = new ZipFile(archive);
        for (Enumeration<?> e = zipfile.entries(); e.hasMoreElements();) {
            ZipEntry entry = (ZipEntry) e.nextElement();

            if (!entry.getName().contains("MACOSX")) {
                compressionEntries.put(entry.getName(), entry);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {

    }
    return zipfile;
}

From source file:org.eclipse.che.plugin.gwt.Utils.java

/**
 * Reads content of the file from ZIP archive.
 *
 * @param zipFile ZIP file/*ww  w  .j  a v  a  2  s .  c om*/
 * @param path path of the file to read content
 * @return content of the file with the given path
 * @throws IOException if error occurs while reading
 * @throws IllegalArgumentException if file not found in ZIP archive
 */
public static String getFileContent(ZipFile zipFile, String path) throws IOException {
    Enumeration<? extends ZipEntry> entries = zipFile.entries();

    while (entries.hasMoreElements()) {
        ZipEntry entry = entries.nextElement();

        if (path.equals(entry.getName())) {
            try (InputStream in = zipFile.getInputStream(entry)) {
                byte[] bytes = IOUtils.toByteArray(in);

                return new String(bytes);
            }
        }
    }

    throw new IllegalArgumentException(format("Cannot find file '%s' in '%s'", path, zipFile.getName()));
}

From source file:Main.java

public static int upZipFile(File zipFile, String folderPath) throws IOException {
    ZipFile zfile = new ZipFile(zipFile);
    Enumeration zList = zfile.entries();
    ZipEntry ze = null;//from   ww  w. j a v a2  s. c  o m
    byte[] buf = new byte[1024];
    while (zList.hasMoreElements()) {
        ze = (ZipEntry) zList.nextElement();
        if (ze.isDirectory()) {
            String dirstr = folderPath + ze.getName();
            dirstr = new String(dirstr.getBytes("8859_1"), "GB2312");
            File f = new File(dirstr);
            f.mkdirs();
            continue;
        }
        OutputStream os = new BufferedOutputStream(
                new FileOutputStream(getRealFileName(folderPath, ze.getName())));
        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();
    }
    zfile.close();
    return 0;
}

From source file:ZipFileUtil.java

/**
 * @param zipFile/* ww  w.  j ava2 s  . co m*/
 * @param jiniHomeParentDir
 */
public static void unzipFileIntoDirectory(ZipFile zipFile, File jiniHomeParentDir) {
    Enumeration files = zipFile.entries();
    File f = null;
    FileOutputStream fos = null;

    while (files.hasMoreElements()) {
        try {
            ZipEntry entry = (ZipEntry) files.nextElement();
            InputStream eis = zipFile.getInputStream(entry);
            byte[] buffer = new byte[1024];
            int bytesRead = 0;

            f = new File(jiniHomeParentDir.getAbsolutePath() + File.separator + entry.getName());

            if (entry.isDirectory()) {
                f.mkdirs();
                continue;
            } else {
                f.getParentFile().mkdirs();
                f.createNewFile();
            }

            fos = new FileOutputStream(f);

            while ((bytesRead = eis.read(buffer)) != -1) {
                fos.write(buffer, 0, bytesRead);
            }
        } catch (IOException e) {
            e.printStackTrace();
            continue;
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    // ignore
                }
            }
        }
    }
}

From source file:Utils.java

/**
 * Unpack a zip file//  www. j av  a 2s  .  c om
 * 
 * @param theFile
 * @param targetDir
 * @return the file
 * @throws IOException
 */
public static File unpackArchive(File theFile, File targetDir) throws IOException {
    if (!theFile.exists()) {
        throw new IOException(theFile.getAbsolutePath() + " does not exist");
    }
    if (!buildDirectory(targetDir)) {
        throw new IOException("Could not create directory: " + targetDir);
    }
    ZipFile zipFile = new ZipFile(theFile);
    for (Enumeration entries = zipFile.entries(); entries.hasMoreElements();) {
        ZipEntry entry = (ZipEntry) entries.nextElement();
        File file = new File(targetDir, File.separator + entry.getName());
        if (!buildDirectory(file.getParentFile())) {
            throw new IOException("Could not create directory: " + file.getParentFile());
        }
        if (!entry.isDirectory()) {
            copyInputStream(zipFile.getInputStream(entry),
                    new BufferedOutputStream(new FileOutputStream(file)));
        } else {
            if (!buildDirectory(file)) {
                throw new IOException("Could not create directory: " + file);
            }
        }
    }
    zipFile.close();
    return theFile;
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static List<ZipEntry> zipEntries(ZipFile zipFile) {
    final List<ZipEntry> result = new LinkedList<ZipEntry>();
    final Enumeration<ZipEntry> en = (Enumeration<ZipEntry>) zipFile.entries();
    while (en.hasMoreElements()) {
        result.add(en.nextElement());//  ww w  .  ja  v  a 2 s  .c om
    }
    return result;
}