Example usage for java.util.zip ZipFile ZipFile

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

Introduction

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

Prototype

public ZipFile(File file) throws ZipException, IOException 

Source Link

Document

Opens a ZIP file for reading given the specified File object.

Usage

From source file:Main.java

public static void readZipFile(String file) throws Exception {
    ZipFile zf = new ZipFile(file);
    InputStream in = new BufferedInputStream(new FileInputStream(file));
    ZipInputStream zin = new ZipInputStream(in);
    ZipEntry ze;//w  ww  .  ja  v a 2 s.  c o m
    while ((ze = zin.getNextEntry()) != null) {
        if (ze.isDirectory()) {
        } else {
            System.err.println("file - " + ze.getName() + " : " + ze.getSize() + " bytes");
            long size = ze.getSize();
            if (size > 0) {
                BufferedReader br = new BufferedReader(new InputStreamReader(zf.getInputStream(ze)));
                String line;
                while ((line = br.readLine()) != null) {
                    System.out.println(line);
                }
                br.close();
            }
            System.out.println();
        }
    }
    zin.closeEntry();
}

From source file:Main.java

public static ZipFile readZipEntriesForElectric(File archive,
        final HashMap<String, ZipEntry> compressionEntries) {
    ZipFile zipfile = null;/* w  w  w .  java2  s.c om*/
    try {
        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:Main.java

/**
 * Determine whether a file is a JAR File.
 *//*from  w  w  w  . ja  v  a 2 s  .  c  om*/
public static boolean isJarFile(File file) throws IOException {
    if (!isZipFile(file)) {
        return false;
    }
    ZipFile zip = new ZipFile(file);
    boolean manifest = zip.getEntry("META-INF/MANIFEST.MF") != null;
    zip.close();
    return manifest;
}

From source file:Main.java

public static String getBuildTimestamp(Activity activity) {
    String s = "";
    try {//from w w  w  . ja  v a  2 s.  c  om
        ApplicationInfo ai = activity.getPackageManager().getApplicationInfo(activity.getPackageName(), 0);
        ZipFile zf = new ZipFile(ai.sourceDir);
        ZipEntry ze = zf.getEntry("classes.dex");
        long time = ze.getTime();
        s = SimpleDateFormat.getInstance().format(new java.util.Date(time));
        zf.close();
    } catch (Exception e) {
    }
    return s;
}

From source file:Main.java

public static String readChannelString(String path, String prefixString) {
    String ret = null;/*from w  w  w .  java2  s  . c  o m*/
    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:Utils.java

/**
 * Validate that an archive contains a named entry
 * //from  ww w .j  a  va 2  s.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:Main.java

public static void upZipFile(File zipFile, String folderPath) {
    ZipFile zf;//  w  ww.j  a va 2s  .c o m
    try {
        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 List<File> unzip(File zip, File toDir) throws IOException {
    ZipFile zf = null;/*from  ww  w  .  jav  a 2s .  c o m*/
    List<File> files = null;
    try {
        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:Main.java

/**
 * Check is classes.dex file has modified
 * /*  w ww  .  j  a  v  a 2  s . c o m*/
 * @author : sWX293372
 * @version: 1.0
 * @return boolean if the classes.dex is modified then return true, else
 *         return true
 * @createTime : 2016-5-9
 */
public static boolean checkCRC(Context context, long crc) {
    boolean isModified = true;
    try {
        ZipFile zipFile = new ZipFile(context.getPackageCodePath());
        ZipEntry entry = zipFile.getEntry("classes.dex");
        if (crc == entry.getCrc()) {
            isModified = false;
        }
    } catch (IOException e) {
        isModified = true;
        e.printStackTrace();
    }
    return isModified;
}

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;//w  ww.ja  v a  2 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;
}