Search in sources :

Example 1 with AlignedZipOutputStream

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;
}
Also used : BufferedInputStream(java.io.BufferedInputStream) ZipInputStream(java.util.zip.ZipInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) FileInputStream(java.io.FileInputStream) AlignedZipOutputStream(com.tencent.tinker.ziputils.ziputil.AlignedZipOutputStream) ZipFile(java.util.zip.ZipFile) BufferedInputStream(java.io.BufferedInputStream) FileOutputStream(java.io.FileOutputStream) ZipFile(java.util.zip.ZipFile) ShareElfFile(com.tencent.tinker.loader.shareutil.ShareElfFile) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) ShareDexDiffPatchInfo(com.tencent.tinker.loader.shareutil.ShareDexDiffPatchInfo)

Aggregations

ShareDexDiffPatchInfo (com.tencent.tinker.loader.shareutil.ShareDexDiffPatchInfo)1 ShareElfFile (com.tencent.tinker.loader.shareutil.ShareElfFile)1 AlignedZipOutputStream (com.tencent.tinker.ziputils.ziputil.AlignedZipOutputStream)1 BufferedInputStream (java.io.BufferedInputStream)1 BufferedOutputStream (java.io.BufferedOutputStream)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 InputStream (java.io.InputStream)1 ZipEntry (java.util.zip.ZipEntry)1 ZipFile (java.util.zip.ZipFile)1 ZipInputStream (java.util.zip.ZipInputStream)1