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);
}
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;
}
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);
}
}
}
}
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;
}
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;
}
Aggregations