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 void upZipFile(File zipFile, String folderPath) {
    ZipFile zf;
    try {/*from   w  ww . j ava 2  s  .c  om*/
        zf = new ZipFile(zipFile);
        for (Enumeration<?> entries = zf.entries(); entries.hasMoreElements();) {
            ZipEntry entry = ((ZipEntry) entries.nextElement());
            if (entry.isDirectory()) {
                String dirstr = entry.getName();
                dirstr = new String(dirstr.getBytes("8859_1"), "GB2312");
                File f = new File(dirstr);
                f.mkdir();
                continue;
            }

            InputStream in = zf.getInputStream(entry);
            String str = folderPath + File.separator + entry.getName();
            str = new String(str.getBytes("8859_1"), "GB2312");
            File desFile = new File(str);
            if (!desFile.exists()) {
                File fileParentDir = desFile.getParentFile();
                if (!fileParentDir.exists()) {
                    fileParentDir.mkdirs();
                }
                desFile.createNewFile();
            }

            OutputStream out = new FileOutputStream(desFile);
            byte buffer[] = new byte[BUFF_SIZE];
            int realLength;
            while ((realLength = in.read(buffer)) > 0) {
                out.write(buffer, 0, realLength);
            }
            in.close();
            out.close();
        }
    } catch (ZipException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static String[] getXmlFiles(String path) {
    List<String> xmlFiles = new ArrayList<>();
    ZipFile zipFile = null;
    try {/*  w w w.j  ava2 s . c om*/
        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

/**
 * Get channel from META-INF directory./*from  w  w w .j av a2s .co m*/
 *
 * @return the channel name if success,otherwise return "none"
 */
public static String getChannel(Context context) {
    ApplicationInfo appinfo = context.getApplicationInfo();
    String sourceDir = appinfo.sourceDir;
    String ret = "";
    ZipFile zipfile = null;
    try {
        zipfile = new ZipFile(sourceDir);
        Enumeration<?> entries = zipfile.entries();
        while (entries.hasMoreElements()) {
            ZipEntry entry = ((ZipEntry) entries.nextElement());
            String entryName = entry.getName();
            if (entryName.startsWith("META-INF/channel")) {
                ret = entryName;
                break;
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (zipfile != null) {
            try {
                zipfile.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    String[] split = ret.split("_");
    if (split != null && split.length >= 2) {
        return ret.substring(split[0].length() + 1);

    } else {
        return "none";
    }
}

From source file:org.docx4j.template.utils.WmlZipUtils.java

public static void unzip(File sourceFile, File outputDir) throws ZipException, IOException {
    FileUtils.deleteDirectory(outputDir);
    ZipFile zipFile = new ZipFile(sourceFile);
    Enumeration<?> files = zipFile.entries();
    File f = null;//from w  ww.j  a  v a2 s . c  o  m
    FileOutputStream fos = null;
    while (files.hasMoreElements()) {
        try {
            ZipEntry entry = (ZipEntry) files.nextElement();
            InputStream eis = zipFile.getInputStream(entry);
            byte[] buffer = new byte[BUFFER];
            int bytesRead = 0;
            f = new File(outputDir.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);
            }
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    // ignore
                }
            }
        }
    }
}

From source file:org.flowerplatform.core.CoreUtils.java

@SuppressWarnings("rawtypes")
public static void unzipArchive(File archive, File outputDir) throws IOException {
    ZipFile zipfile = new ZipFile(archive);
    for (Enumeration e = zipfile.entries(); e.hasMoreElements();) {
        ZipEntry entry = (ZipEntry) e.nextElement();
        unzipEntry(zipfile, entry, outputDir);
    }//  w  w w  .  ja va2 s  .  c o  m
    zipfile.close();
}

From source file:net.sourceforge.floggy.maven.ZipUtils.java

/**
 * DOCUMENT ME!// www . ja v a  2 s.c om
*
* @param file DOCUMENT ME!
* @param directory DOCUMENT ME!
*
* @throws IOException DOCUMENT ME!
*/
public static void unzip(File file, File directory) throws IOException {
    ZipFile zipFile = new ZipFile(file);
    Enumeration entries = zipFile.entries();

    if (!directory.exists() && !directory.mkdirs()) {
        throw new IOException("Unable to create the " + directory + " directory!");
    }

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

        if (entry.isDirectory()) {
            temp = new File(directory, entry.getName());

            if (!temp.exists() && !temp.mkdirs()) {
                throw new IOException("Unable to create the " + temp + " directory!");
            }
        } else {
            temp = new File(directory, entry.getName());
            IOUtils.copy(zipFile.getInputStream(entry), new FileOutputStream(temp));
        }
    }

    zipFile.close();
}

From source file:Main.java

public static String readChannelString(String path, String prefixString) {
    String ret = null;//  w  w w. ja v a 2s  .c om
    ZipFile zipfile = null;
    try {
        zipfile = new ZipFile(path);
        Enumeration<?> entries = zipfile.entries();
        while (entries.hasMoreElements()) {
            ZipEntry entry = ((ZipEntry) entries.nextElement());
            String entryName = entry.getName();
            if (debug) {
                System.out.println(entryName);
            }
            if (entryName.contains(prefixString)) {
                ret = entryName;

                break;
            }
        }

        if (ret != null) {
            String[] split = ret.split("_");
            if (split != null && split.length >= 2) {
                String substring = ret.substring(split[0].length() + 1);
                if (debug) {
                    System.out.println(String.format("find channel string:%s", substring));
                }
                return substring;

            }
        } else {
            if (debug) {
                System.out.println("not find channel");
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (zipfile != null) {
            try {
                zipfile.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    return ret;

}

From source file:org.apereo.portal.utils.ZipUtils.java

public static void extract(File archive, File outputDir) throws IOException {
    final ZipFile zipfile = new ZipFile(archive);
    for (final Enumeration<? extends ZipEntry> e = zipfile.entries(); e.hasMoreElements();) {
        final ZipEntry entry = e.nextElement();

        final File outputFile = checkDirectories(entry, outputDir);
        if (outputFile != null) {
            final InputStream is = zipfile.getInputStream(entry);
            try {
                writeFile(is, outputFile);
            } finally {
                is.close();/*from w  w  w  .  j  av  a 2  s . com*/
            }
        }
    }
}

From source file:Main.java

public static void unzip(URL _url, URL _dest, boolean _remove_top) {
    int BUFFER_SZ = 2048;
    File file = new File(_url.getPath());

    String dest = _dest.getPath();
    new File(dest).mkdir();

    try {//from  w ww. j av  a2  s .  co  m
        ZipFile zip = new ZipFile(file);
        Enumeration<? extends ZipEntry> zipFileEntries = zip.entries();

        // Process each entry
        while (zipFileEntries.hasMoreElements()) {
            // grab a zip file entry
            ZipEntry entry = (ZipEntry) zipFileEntries.nextElement();
            String currentEntry = entry.getName();

            if (_remove_top)
                currentEntry = currentEntry.substring(currentEntry.indexOf('/'), currentEntry.length());

            File destFile = new File(dest, currentEntry);
            //destFile = new File(newPath, destFile.getName());
            File destinationParent = destFile.getParentFile();

            // create the parent directory structure if needed
            destinationParent.mkdirs();

            if (!entry.isDirectory()) {
                BufferedInputStream is;
                is = new BufferedInputStream(zip.getInputStream(entry));
                int currentByte;
                // establish buffer for writing file
                byte data[] = new byte[BUFFER_SZ];

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

                // read and write until last byte is encountered
                while ((currentByte = is.read(data, 0, BUFFER_SZ)) != -1) {
                    dst.write(data, 0, currentByte);
                }
                dst.flush();
                dst.close();
                is.close();
            }
            /*
            if (currentEntry.endsWith(".zip"))
            {
            // found a zip file, try to open
            extractFolder(destFile.getAbsolutePath());
            }*/
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

From source file:Main.java

public static void unzipEPub(String inputZip, String destinationDirectory) throws IOException {
    int BUFFER = 2048;
    List zipFiles = new ArrayList();
    File sourceZipFile = new File(inputZip);
    File unzipDestinationDirectory = new File(destinationDirectory);
    unzipDestinationDirectory.mkdir();//from w w w.  ja v  a 2  s  .c  o  m

    ZipFile zipFile;
    zipFile = new ZipFile(sourceZipFile, ZipFile.OPEN_READ);
    Enumeration zipFileEntries = zipFile.entries();

    // Process each entry
    while (zipFileEntries.hasMoreElements()) {

        ZipEntry entry = (ZipEntry) zipFileEntries.nextElement();
        String currentEntry = entry.getName();
        File destFile = new File(unzipDestinationDirectory, currentEntry);

        if (currentEntry.endsWith(".zip")) {
            zipFiles.add(destFile.getAbsolutePath());
        }

        File destinationParent = destFile.getParentFile();
        destinationParent.mkdirs();

        if (!entry.isDirectory()) {
            BufferedInputStream is = new BufferedInputStream(zipFile.getInputStream(entry));
            int currentByte;
            // buffer for writing file
            byte data[] = new byte[BUFFER];

            FileOutputStream fos = new FileOutputStream(destFile);
            BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER);

            while ((currentByte = is.read(data, 0, BUFFER)) != -1) {
                dest.write(data, 0, currentByte);
            }
            dest.flush();
            dest.close();
            is.close();

        }

    }
    zipFile.close();

    for (Iterator iter = zipFiles.iterator(); iter.hasNext();) {
        String zipName = (String) iter.next();
        unzipEPub(zipName,
                destinationDirectory + File.separatorChar + zipName.substring(0, zipName.lastIndexOf(".zip")));
    }
}