use of com.tencent.tinker.ziputils.ziputil.AlignedZipOutputStream in project tinker by Tencent.
the class DexDiffPatchInternal method mergeClassNDexFiles.
private static boolean mergeClassNDexFiles(final Context context, final File patchFile, final String dexFilePath) {
// only merge for art vm
if (patchList.isEmpty() || !isVmArt) {
return true;
}
File classNFile = new File(dexFilePath, ShareConstants.CLASS_N_APK_NAME);
// repack just more than one classN.dex
if (classNDexInfo.isEmpty()) {
ShareTinkerLog.w(TAG, "classNDexInfo size: %d, no need to merge classN dex files", classNDexInfo.size());
return true;
}
long start = System.currentTimeMillis();
boolean result = true;
AlignedZipOutputStream out = null;
try {
out = new AlignedZipOutputStream(new BufferedOutputStream(new FileOutputStream(classNFile)));
for (ShareDexDiffPatchInfo info : classNDexInfo.keySet()) {
File dexFile = classNDexInfo.get(info);
if (info.isJarMode) {
ZipFile dexZipFile = null;
InputStream inputStream = null;
try {
dexZipFile = new ZipFile(dexFile);
ZipEntry rawDexZipEntry = dexZipFile.getEntry(ShareConstants.DEX_IN_JAR);
ZipEntry newDexZipEntry = makeStoredZipEntry(rawDexZipEntry, info.rawName);
inputStream = dexZipFile.getInputStream(rawDexZipEntry);
try {
out.putNextEntry(newDexZipEntry);
IOHelper.copyStream(inputStream, out);
} finally {
out.closeEntry();
}
} finally {
IOHelper.closeQuietly(inputStream);
IOHelper.closeQuietly(dexZipFile);
}
} else {
ZipEntry newDexZipEntry = new ZipEntry(info.rawName);
newDexZipEntry.setMethod(ZipEntry.STORED);
newDexZipEntry.setCompressedSize(dexFile.length());
newDexZipEntry.setSize(dexFile.length());
newDexZipEntry.setCrc(DigestUtil.getCRC32(dexFile));
InputStream is = null;
try {
is = new BufferedInputStream(new FileInputStream(dexFile));
try {
out.putNextEntry(newDexZipEntry);
IOHelper.copyStream(is, out);
} finally {
out.closeEntry();
}
} finally {
IOHelper.closeQuietly(is);
}
}
}
} catch (Throwable throwable) {
ShareTinkerLog.printErrStackTrace(TAG, throwable, "merge classN file");
result = false;
} finally {
IOHelper.closeQuietly(out);
}
if (result) {
for (ShareDexDiffPatchInfo info : classNDexInfo.keySet()) {
if (!SharePatchFileUtil.verifyDexFileMd5(classNFile, info.rawName, info.destMd5InArt)) {
result = false;
ShareTinkerLog.e(TAG, "verify dex file md5 error, entry name; %s, file len: %d", info.rawName, classNFile.length());
break;
}
}
}
if (result) {
for (File dexFile : classNDexInfo.values()) {
SharePatchFileUtil.safeDeleteFile(dexFile);
}
} else {
ShareTinkerLog.e(TAG, "merge classN dex error, try delete temp file");
SharePatchFileUtil.safeDeleteFile(classNFile);
Tinker.with(context).getPatchReporter().onPatchTypeExtractFail(patchFile, classNFile, classNFile.getName(), TYPE_CLASS_N_DEX);
}
ShareTinkerLog.i(TAG, "merge classN dex file %s, result: %b, size: %d, use: %dms", classNFile.getPath(), result, classNFile.length(), (System.currentTimeMillis() - start));
return result;
}
Aggregations