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 String getChannel(Context context) {
    ApplicationInfo appinfo = context.getApplicationInfo();
    String sourceDir = appinfo.sourceDir;
    ZipFile zipfile = null;
    String ret = null;/*from  w w  w  .  j a  v a2s  . com*/
    try {
        zipfile = new ZipFile(sourceDir);
        Enumeration<?> entries = zipfile.entries();
        while (entries.hasMoreElements()) {
            ZipEntry entry = ((ZipEntry) entries.nextElement());
            String entryName = entry.getName();
            if (entryName.startsWith(FOLDER_NAME) && entryName.contains(CHANNEL_SCHEME)) {
                ret = entryName;
                break;
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (zipfile != null) {
            try {
                zipfile.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    try {
        if (ret != null) {
            String[] split = ret.split(CHANNEL_SCHEME_SPIT);
            if (split.length >= 2) {
                return ret.substring(split[0].length() + 1);
            }
        }
    } catch (Exception e) {

    }

    return null;
}

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  ww .  j  av a 2 s .c  om
            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:Zip.java

/**
 * Create a InputStream on a given file, by looking for a zip archive whose
 * path is the file path with ".zip" appended, and by reading the first
 * entry in this zip file./*from  w ww  .j  a v a  2s.  c  om*/
 *
 * @param file the file (with no zip extension)
 *
 * @return a InputStream on the zip entry
 */
public static InputStream createInputStream(File file) {
    try {
        String path = file.getCanonicalPath();

        //ZipFile zf = new ZipFile(path + ".zip");
        ZipFile zf = new ZipFile(path);

        for (Enumeration entries = zf.entries(); entries.hasMoreElements();) {
            ZipEntry entry = (ZipEntry) entries.nextElement();

            return zf.getInputStream(entry);
        }
    } catch (FileNotFoundException ex) {
        System.err.println(ex.toString());
        System.err.println(file + " not found");
    } catch (IOException ex) {
        System.err.println(ex.toString());
    }

    return null;
}

From source file:net.sourceforge.atunes.utils.ZipUtils.java

/**
 * Unzips a file in a directory/*from w w  w  .  j a v a2  s  . co m*/
 * 
 * @param archive
 * @param outputDir
 * @throws IOException
 */
public static void unzipArchive(File archive, File outputDir) throws IOException {
    ZipFile zipfile = null;
    try {
        zipfile = new ZipFile(archive);
        for (Enumeration<? extends ZipEntry> e = zipfile.entries(); e.hasMoreElements();) {
            ZipEntry entry = e.nextElement();
            unzipEntry(zipfile, entry, outputDir);
        }
    } finally {
        ClosingUtils.close(zipfile);
    }
}

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();/*from   w  w  w.  jav  a  2 s  .  com*/
            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:Zip.java

/**
 * Create a Reader on a given file, by looking for a zip archive whose path
 * is the file path with ".zip" appended, and by reading the first entry in
 * this zip file./*ww w .  j av a  2  s .c  o  m*/
 *
 * @param file the file (with no zip extension)
 *
 * @return a reader on the zip entry
 */
public static Reader createReader(File file) {
    try {
        String path = file.getCanonicalPath();

        ZipFile zf = new ZipFile(path + ".zip");

        for (Enumeration entries = zf.entries(); entries.hasMoreElements();) {
            ZipEntry entry = (ZipEntry) entries.nextElement();
            InputStream is = zf.getInputStream(entry);

            return new InputStreamReader(is);
        }
    } catch (FileNotFoundException ex) {
        System.err.println(ex.toString());
        System.err.println(file + " not found");
    } catch (IOException ex) {
        System.err.println(ex.toString());
    }

    return null;
}

From source file:com.predic8.membrane.examples.DistributionExtractingTestcase.java

public static final void unzip(File zip, File target) throws IOException {
    ZipFile zipFile = new ZipFile(zip);
    Enumeration<? extends ZipEntry> entries = zipFile.entries();
    while (entries.hasMoreElements()) {
        ZipEntry entry = entries.nextElement();
        if (entry.isDirectory()) {
            // Assume directories are stored parents first then children.
            // This is not robust, just for demonstration purposes.
            new File(target, entry.getName()).mkdir();
        } else {/*from  ww w .  ja va2 s. co  m*/
            FileOutputStream fos = new FileOutputStream(new File(target, entry.getName()));
            try {
                copyInputStream(zipFile.getInputStream(entry), new BufferedOutputStream(fos));
            } finally {
                fos.close();
            }
        }
    }
    zipFile.close();
}

From source file:com.netflix.nicobar.core.utils.__JDKPaths.java

static void processJar(final Set<String> pathSet, final File file) throws IOException {
    final ZipFile zipFile = new ZipFile(file);
    try {//from ww  w.  j a  v a2s  . c o  m
        final Enumeration<? extends ZipEntry> entries = zipFile.entries();
        while (entries.hasMoreElements()) {
            final ZipEntry entry = entries.nextElement();
            final String name = entry.getName();
            final int lastSlash = name.lastIndexOf('/');
            if (lastSlash != -1) {
                pathSet.add(name.substring(0, lastSlash));
            }
        }
        zipFile.close();
    } finally {
        IOUtils.closeQuietly(zipFile);
    }
}

From source file:eurecom.constrained.devices.ReadZipFile.java

public static void unzip(final ZipFile zipfile, final File directory) throws IOException {

    final Enumeration<? extends ZipEntry> entries = zipfile.entries();
    while (entries.hasMoreElements()) {
        final ZipEntry entry = entries.nextElement();
        final File file = file(directory, entry);
        if (entry.isDirectory()) {
            continue;
        }//  w w w.  j ava 2 s  .  c  om
        final InputStream input = zipfile.getInputStream(entry);
        try {
            // copy bytes from input to file
        } finally {
            input.close();
        }
    }
}

From source file:Main.java

private static String getChannelFromApk(Context context, String channelKey) {
    ApplicationInfo appinfo = context.getApplicationInfo();
    String sourceDir = appinfo.sourceDir;
    String key = "META-INF/" + channelKey;
    String ret = "";
    ZipFile zipfile = null;
    try {// w ww.  j  ava  2 s  .co  m
        zipfile = new ZipFile(sourceDir);
        Enumeration<?> entries = zipfile.entries();
        while (entries.hasMoreElements()) {
            ZipEntry entry = ((ZipEntry) entries.nextElement());
            String entryName = entry.getName();
            if (entryName.startsWith(key)) {
                ret = entryName;
                break;
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (zipfile != null) {
            try {
                zipfile.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    String[] split = ret.split("_");
    String channel = "";
    if (split != null && split.length >= 2) {
        channel = ret.substring(split[0].length() + 1);
    }
    return channel;
}