Search in sources :

Example 76 with ZipEntry

use of java.util.zip.ZipEntry 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 77 with ZipEntry

use of java.util.zip.ZipEntry 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)

Example 78 with ZipEntry

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

the class DexDiffDecoder method getRawOrWrappedDexMD5.

private String getRawOrWrappedDexMD5(File dexOrJarFile) {
    final String name = dexOrJarFile.getName();
    if (name.endsWith(".dex")) {
        return MD5.getMD5(dexOrJarFile);
    } else {
        JarFile dexJar = null;
        try {
            dexJar = new JarFile(dexOrJarFile);
            ZipEntry classesDex = dexJar.getEntry(DexFormat.DEX_IN_JAR_NAME);
            // no code
            if (classesDex == null) {
                throw new TinkerPatchException(String.format("Jar file %s do not contain 'classes.dex', it is not a correct dex jar file!", dexOrJarFile.getAbsolutePath()));
            }
            return MD5.getMD5(dexJar.getInputStream(classesDex), 1024 * 100);
        } catch (IOException e) {
            throw new TinkerPatchException(String.format("File %s is not end with '.dex', but it is not a correct dex jar file !", dexOrJarFile.getAbsolutePath()), e);
        } finally {
            if (dexJar != null) {
                try {
                    dexJar.close();
                } catch (Exception e) {
                // Ignored.
                }
            }
        }
    }
}
Also used : ZipEntry(java.util.zip.ZipEntry) IOException(java.io.IOException) JarFile(java.util.jar.JarFile) TinkerPatchException(com.tencent.tinker.build.util.TinkerPatchException) IOException(java.io.IOException) TinkerPatchException(com.tencent.tinker.build.util.TinkerPatchException)

Example 79 with ZipEntry

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

the class FileOperation method getZipEntryMd5.

public static String getZipEntryMd5(File file, String entryName) {
    ZipFile zipFile = null;
    try {
        zipFile = new ZipFile(file);
        ZipEntry entry = zipFile.getEntry(entryName);
        if (entry == null) {
            return null;
        }
        return MD5.getMD5(zipFile.getInputStream(entry), 1024 * 100);
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (zipFile != null) {
            try {
                zipFile.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return null;
}
Also used : ZipFile(java.util.zip.ZipFile) ZipEntry(java.util.zip.ZipEntry) IOException(java.io.IOException)

Example 80 with ZipEntry

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

the class FileUtil method mergeZip.

/**
     * merge zip file
     *
     * @param zipOutputFullFilename
     * @param mergeZipFullFilenameList
     */
public static void mergeZip(String zipOutputFullFilename, List<String> mergeZipFullFilenameList) {
    FileUtil.createFile(zipOutputFullFilename);
    ZipOutputStream zipOutputStream = null;
    try {
        zipOutputStream = new ZipOutputStream(new FileOutputStream(zipOutputFullFilename));
        if (mergeZipFullFilenameList != null) {
            for (String zipFullFilename : mergeZipFullFilenameList) {
                if (isExist(zipFullFilename)) {
                    ZipFile zipFile = new ZipFile(zipFullFilename);
                    Enumeration<? extends ZipEntry> enumeration = zipFile.entries();
                    while (enumeration.hasMoreElements()) {
                        ZipEntry zipEntry = enumeration.nextElement();
                        InputStream inputStream = zipFile.getInputStream(zipEntry);
                        addZipEntry(zipOutputStream, zipEntry, inputStream);
                    }
                    zipFile.close();
                }
            }
        }
    } catch (Exception e) {
        throw new FileUtilException(e);
    } finally {
        try {
            if (zipOutputStream != null) {
                zipOutputStream.close();
            }
        } catch (Exception e) {
            throw new FileUtilException(e);
        }
    }
}
Also used : ZipFile(java.util.zip.ZipFile) ZipOutputStream(java.util.zip.ZipOutputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) ZipEntry(java.util.zip.ZipEntry) ZipException(java.util.zip.ZipException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Aggregations

ZipEntry (java.util.zip.ZipEntry)1367 ZipFile (java.util.zip.ZipFile)479 File (java.io.File)469 IOException (java.io.IOException)361 ZipOutputStream (java.util.zip.ZipOutputStream)321 ZipInputStream (java.util.zip.ZipInputStream)300 InputStream (java.io.InputStream)282 FileOutputStream (java.io.FileOutputStream)278 FileInputStream (java.io.FileInputStream)270 Test (org.junit.Test)124 BufferedInputStream (java.io.BufferedInputStream)122 JarFile (java.util.jar.JarFile)122 BufferedOutputStream (java.io.BufferedOutputStream)99 ByteArrayOutputStream (java.io.ByteArrayOutputStream)97 ArrayList (java.util.ArrayList)84 ByteArrayInputStream (java.io.ByteArrayInputStream)78 OutputStream (java.io.OutputStream)67 JarOutputStream (java.util.jar.JarOutputStream)59 Path (java.nio.file.Path)56 Enumeration (java.util.Enumeration)56