Search in sources :

Example 86 with ZipFile

use of java.util.zip.ZipFile in project tinker by Tencent.

the class SharePatchFileUtil method verifyDexFileMd5.

/**
     * Returns whether the dex file is a valid file.
     * dex may wrap with jar
     */
public static boolean verifyDexFileMd5(File file, String md5) {
    if (file == null || md5 == null) {
        return false;
    }
    //if it is not the raw dex, we check the stream instead
    String fileMd5;
    if (isRawDexFile(file.getName())) {
        fileMd5 = getMD5(file);
    } else {
        ZipFile dexJar = null;
        try {
            dexJar = new ZipFile(file);
            ZipEntry classesDex = dexJar.getEntry(ShareConstants.DEX_IN_JAR);
            // no code
            if (null == classesDex) {
                Log.e(TAG, "There's no entry named: " + ShareConstants.DEX_IN_JAR + " in " + file.getAbsolutePath());
                return false;
            }
            fileMd5 = getMD5(dexJar.getInputStream(classesDex));
        } catch (Throwable e) {
            Log.e(TAG, "Bad dex jar file: " + file.getAbsolutePath(), e);
            return false;
        } finally {
            // SharePatchFileUtil.closeZip(dexJar);
            if (dexJar != null) {
                try {
                    dexJar.close();
                } catch (Throwable thr) {
                // Ignored.
                }
            }
        }
    }
    return md5.equals(fileMd5);
}
Also used : ZipFile(java.util.zip.ZipFile) ZipEntry(java.util.zip.ZipEntry)

Example 87 with ZipFile

use of java.util.zip.ZipFile in project tinker by Tencent.

the class SharePatchFileUtil method checkResourceArscMd5.

public static boolean checkResourceArscMd5(File resOutput, String destMd5) {
    ZipFile resourceZip = null;
    try {
        resourceZip = new ZipFile(resOutput);
        ZipEntry arscEntry = resourceZip.getEntry(ShareConstants.RES_ARSC);
        if (arscEntry == null) {
            Log.i(TAG, "checkResourceArscMd5 resources.arsc not found");
            return false;
        }
        InputStream inputStream = null;
        try {
            inputStream = resourceZip.getInputStream(arscEntry);
            String md5 = SharePatchFileUtil.getMD5(inputStream);
            if (md5 != null && md5.equals(destMd5)) {
                return true;
            }
        } finally {
            SharePatchFileUtil.closeQuietly(inputStream);
        }
    } catch (Throwable e) {
        Log.i(TAG, "checkResourceArscMd5 throwable:" + e.getMessage());
    } finally {
        SharePatchFileUtil.closeZip(resourceZip);
    }
    return false;
}
Also used : ZipFile(java.util.zip.ZipFile) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry)

Example 88 with ZipFile

use of java.util.zip.ZipFile in project tinker by Tencent.

the class FileUtil method differZip.

/**
     * differ zip
     *
     * @param differentOutputFullFilename
     * @param oldZipFullFilename
     * @param newZipFullFilename
     */
public static void differZip(String differentOutputFullFilename, String oldZipFullFilename, String newZipFullFilename) {
    Map<String, String> map = getZipEntryHashMap(oldZipFullFilename);
    ZipFile newZipFile = null;
    ZipOutputStream zipOutputStream = null;
    try {
        newZipFile = new ZipFile(newZipFullFilename);
        Enumeration<? extends ZipEntry> entries = newZipFile.entries();
        FileUtil.createFile(differentOutputFullFilename);
        zipOutputStream = new ZipOutputStream(new FileOutputStream(differentOutputFullFilename));
        while (entries.hasMoreElements()) {
            ZipEntry zipEntry = entries.nextElement();
            if (!zipEntry.isDirectory()) {
                String zipEntryName = zipEntry.getName();
                String oldZipEntryHash = map.get(zipEntryName);
                String newZipEntryHash = zipEntry.getCrc() + Constant.Symbol.DOT + zipEntry.getSize();
                // is a modified zip entry
                if (oldZipEntryHash == null || (!newZipEntryHash.equals(oldZipEntryHash))) {
                    System.out.println(String.format("found modified entry, key=%s(%s/%s)", new Object[] { zipEntryName, oldZipEntryHash, newZipEntryHash }));
                    addZipEntry(zipOutputStream, zipEntry, newZipFile.getInputStream(zipEntry));
                }
            }
        }
    } catch (Exception e) {
        throw new FileUtilException(e);
    } finally {
        if (newZipFile != null) {
            try {
                newZipFile.close();
            } catch (IOException e) {
                throw new FileUtilException(e);
            }
        }
        if (zipOutputStream != null) {
            try {
                zipOutputStream.finish();
            } catch (IOException e) {
                throw new FileUtilException(e);
            }
        }
    }
}
Also used : ZipFile(java.util.zip.ZipFile) ZipOutputStream(java.util.zip.ZipOutputStream) FileOutputStream(java.io.FileOutputStream) ZipEntry(java.util.zip.ZipEntry) IOException(java.io.IOException) ZipException(java.util.zip.ZipException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Example 89 with ZipFile

use of java.util.zip.ZipFile in project tinker by Tencent.

the class FileUtil method unzip.

/**
     * unzip
     *
     * @param zipFullFilename
     * @param outputDirectory
     * @param zipEntryNameList,if it is null or empty,will unzip all
     * @return List<String>
     */
public static List<String> unzip(String zipFullFilename, String outputDirectory, List<String> zipEntryNameList) {
    if (outputDirectory == null) {
        throw new NullPointerException("out put directory can not be null.");
    }
    List<String> storeFileList = null;
    ZipFile zipFile = null;
    try {
        storeFileList = new ArrayList<String>();
        zipFile = new ZipFile(zipFullFilename);
        String outputDirectoryAbsolutePath = new File(outputDirectory).getAbsolutePath();
        Enumeration<? extends ZipEntry> enumeration = zipFile.entries();
        while (enumeration.hasMoreElements()) {
            ZipEntry zipEntry = enumeration.nextElement();
            String zipEntryName = zipEntry.getName();
            boolean contains = false;
            if (zipEntryNameList == null || zipEntryNameList.isEmpty()) {
                contains = true;
            } else {
                if (zipEntryNameList.contains(zipEntryName)) {
                    contains = true;
                }
            }
            if (contains) {
                InputStream inputStream = zipFile.getInputStream(zipEntry);
                String outputFullFilename = outputDirectoryAbsolutePath + Constant.Symbol.SLASH_LEFT + zipEntryName;
                if (zipEntry.isDirectory()) {
                    createDirectory(outputFullFilename);
                } else {
                    createFile(outputFullFilename);
                    OutputStream outputStream = new FileOutputStream(outputFullFilename);
                    try {
                        byte[] buffer = new byte[Constant.Capacity.BYTES_PER_KB];
                        int length = -1;
                        while ((length = inputStream.read(buffer, 0, buffer.length)) != -1) {
                            outputStream.write(buffer, 0, length);
                            outputStream.flush();
                        }
                    } finally {
                        if (inputStream != null) {
                            inputStream.close();
                        }
                        if (outputStream != null) {
                            outputStream.close();
                        }
                    }
                    storeFileList.add(outputFullFilename);
                }
            }
        }
    } catch (Exception e) {
        throw new FileUtilException(e);
    } finally {
        try {
            if (zipFile != null) {
                zipFile.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return storeFileList;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) ZipOutputStream(java.util.zip.ZipOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) ZipException(java.util.zip.ZipException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) ZipFile(java.util.zip.ZipFile) FileOutputStream(java.io.FileOutputStream) ZipFile(java.util.zip.ZipFile) File(java.io.File)

Example 90 with ZipFile

use of java.util.zip.ZipFile in project tinker by Tencent.

the class FileUtil method getZipEntryHashMap.

/**
     * get zip entry hash map
     *
     * @param zipFile
     * @return Map<String, String>
     */
private static Map<String, String> getZipEntryHashMap(String zipFullFilename) {
    ZipFile zipFile = null;
    Map<String, String> map = new HashMap<String, String>();
    try {
        zipFile = new ZipFile(zipFullFilename);
        Enumeration<? extends ZipEntry> entries = zipFile.entries();
        while (entries.hasMoreElements()) {
            ZipEntry zipEntry = (ZipEntry) entries.nextElement();
            if (!zipEntry.isDirectory()) {
                String key = zipEntry.getName();
                String value = zipEntry.getCrc() + Constant.Symbol.DOT + zipEntry.getSize();
                map.put(key, value);
            }
        }
    } catch (Exception e) {
        throw new FileUtilException(e);
    } finally {
        if (zipFile != null) {
            try {
                zipFile.close();
            } catch (IOException e) {
                throw new FileUtilException(e);
            }
        }
    }
    return map;
}
Also used : ZipFile(java.util.zip.ZipFile) HashMap(java.util.HashMap) ZipEntry(java.util.zip.ZipEntry) IOException(java.io.IOException) ZipException(java.util.zip.ZipException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Aggregations

ZipFile (java.util.zip.ZipFile)637 ZipEntry (java.util.zip.ZipEntry)454 File (java.io.File)287 IOException (java.io.IOException)214 InputStream (java.io.InputStream)147 FileOutputStream (java.io.FileOutputStream)108 ZipOutputStream (java.util.zip.ZipOutputStream)92 Test (org.junit.Test)89 FileInputStream (java.io.FileInputStream)68 Enumeration (java.util.Enumeration)47 ArrayList (java.util.ArrayList)46 BufferedInputStream (java.io.BufferedInputStream)44 BufferedOutputStream (java.io.BufferedOutputStream)39 ZipInputStream (java.util.zip.ZipInputStream)35 ZipException (java.util.zip.ZipException)34 OutputStream (java.io.OutputStream)31 ClassReader (org.objectweb.asm.ClassReader)29 FileNotFoundException (java.io.FileNotFoundException)26 JarFile (java.util.jar.JarFile)26 ByteArrayOutputStream (java.io.ByteArrayOutputStream)24