Search in sources :

Example 46 with ZipFile

use of java.util.zip.ZipFile in project atlas by alibaba.

the class DexReleaser method releaseDexes.

public static boolean releaseDexes(File bundleFile, File reversionDir) throws IOException {
    ZipFile zipFile = new ZipFile(bundleFile);
    boolean hasDexFile = hasDexFile(zipFile);
    if (!hasDexFile) {
        return true;
    }
    if (!isArt()) {
        Enumeration entryEnumeration = zipFile.entries();
        while (entryEnumeration.hasMoreElements()) {
            ZipEntry zipEntry = (ZipEntry) entryEnumeration.nextElement();
            if (zipEntry.getName().endsWith(DEX_SUFFIX)) {
                File dexFile = new File(reversionDir, zipEntry.getName());
                FileOutputStream fileOutputStream = new FileOutputStream(dexFile);
                copy(zipFile.getInputStream(zipEntry), fileOutputStream);
                fileOutputStream.close();
            }
        }
        return true;
    } else {
        File mergedFile = preProcessPatch(zipFile, bundleFile);
        if (mergedFile != null && mergedFile.exists()) {
            bundleFile.delete();
            boolean success = mergedFile.renameTo(bundleFile);
            if (!success || !isNewBundleFileValid(bundleFile)) {
                return false;
            }
        } else {
            return false;
        }
        try {
            zipFile.close();
        } catch (Throwable e) {
        }
    }
    return true;
}
Also used : Enumeration(java.util.Enumeration) ZipFile(java.util.zip.ZipFile) ZipEntry(java.util.zip.ZipEntry) ZipFile(java.util.zip.ZipFile)

Example 47 with ZipFile

use of java.util.zip.ZipFile in project atlas by alibaba.

the class DexReleaser method isNewBundleFileValid.

private static boolean isNewBundleFileValid(File bundleFile) throws IOException {
    ZipFile zipFile = new ZipFile(bundleFile);
    boolean result = zipFile.getEntry(CLASS_SUFFIX + DEX_SUFFIX) != null && zipFile.getEntry(CLASS_SUFFIX + 2 + DEX_SUFFIX) != null;
    try {
        zipFile.close();
    } catch (Throwable e) {
    }
    return result;
}
Also used : ZipFile(java.util.zip.ZipFile)

Example 48 with ZipFile

use of java.util.zip.ZipFile in project atlas by alibaba.

the class PatchMerger method findOriginalBundleFile.

/**
     * be not call in main thread
     */
/**
     * get original bundle
     *
     * @param bundleName
     * @return
     */
public File findOriginalBundleFile(String bundleName, String bundleDirIfNeedCreate, UpdateInfo.Item item) throws IOException {
    if (bundleName.equals(MAIN_DEX)) {
        return new File(RuntimeVariables.androidApplication.getApplicationInfo().sourceDir);
    }
    if (!TextUtils.isEmpty(bundleName)) {
        File oldBundle = null;
        if (TextUtils.isEmpty(item.srcVersion)) {
            oldBundle = Atlas.getInstance().getBundleFile(bundleName) != null ? Atlas.getInstance().getBundleFile(bundleName) : Framework.getInstalledBundle(bundleName, "");
        } else {
            BundleImpl impl = (BundleImpl) Atlas.getInstance().getBundle(bundleName);
            if (impl != null && !BaselineInfoManager.instance().isDexPatched(bundleName)) {
                String version = impl.getArchive().getCurrentRevision().getVersion();
                if (version != null && version.equals(item.srcVersion)) {
                    oldBundle = impl.getArchive().getArchiveFile();
                } else if (version != null && !version.equals(item.srcVersion) && !AtlasBundleInfoManager.instance().isInternalBundle(bundleName)) {
                    //                            Atlas.getInstance().restoreBundle(new String[]{bundleName});
                    return null;
                } else if (version != null && !version.equals(item.srcVersion)) {
                    throw new IOException("can not find valid src bundle of " + bundleName);
                }
            } else {
                oldBundle = Framework.getInstalledBundle(bundleName, item.srcVersion);
                if (oldBundle == null) {
                    throw new IOException("can not find valid src bundle of " + bundleName);
                }
            }
        }
        if (oldBundle == null) {
            String oldBundleFileName = String.format("lib%s.so", bundleName.replace(".", "_"));
            File libDir = new File(RuntimeVariables.androidApplication.getFilesDir().getParentFile(), "lib");
            oldBundle = new File(libDir, oldBundleFileName);
            if (oldBundle.exists()) {
                return oldBundle;
            } else {
                if (apkZip == null) {
                    apkZip = new ZipFile(RuntimeVariables.androidApplication.getApplicationInfo().sourceDir);
                }
                String entryName = String.format("lib/armeabi/%s", oldBundleFileName);
                if (apkZip.getEntry(entryName) != null) {
                    InputStream inputStream = apkZip.getInputStream(apkZip.getEntry(entryName));
                    oldBundle = new File(bundleDirIfNeedCreate, oldBundleFileName);
                    //                        oldBundle.createNewFile();
                    ApkUtils.copyInputStreamToFile(inputStream, oldBundle);
                    return oldBundle;
                }
            }
        }
        return oldBundle;
    }
    return null;
}
Also used : BundleImpl(android.taobao.atlas.framework.BundleImpl) ZipFile(java.util.zip.ZipFile) ZipFile(java.util.zip.ZipFile)

Example 49 with ZipFile

use of java.util.zip.ZipFile in project atlas by alibaba.

the class ZipUtils method unzip.

public static void unzip(String zipFilename, String outputDirectory) throws IOException {
    File outFile = new File(outputDirectory);
    if (!outFile.exists()) {
        outFile.mkdirs();
    }
    if (!outFile.exists()) {
        throw new IOException("file not exist");
    }
    ZipFile zipFile = new ZipFile(zipFilename);
    Enumeration en = zipFile.entries();
    ZipEntry zipEntry = null;
    while (en.hasMoreElements()) {
        zipEntry = (ZipEntry) en.nextElement();
        if (zipEntry.isDirectory()) {
            String dirName = zipEntry.getName();
            dirName = dirName.substring(0, dirName.length() - 1);
            File f = new File(outFile.getPath() + File.separator + dirName);
            f.mkdirs();
        } else {
            String strFilePath = outFile.getPath() + File.separator + zipEntry.getName();
            File f = new File(strFilePath);
            // 判断文件不存在的话,就创建该文件所在文件夹的目录
            if (!f.exists()) {
                String[] arrFolderName = zipEntry.getName().split("/");
                String strRealFolder = "";
                for (int i = 0; i < (arrFolderName.length - 1); i++) {
                    strRealFolder += arrFolderName[i] + File.separator;
                }
                strRealFolder = outFile.getPath() + File.separator + strRealFolder;
                File tempDir = new File(strRealFolder);
                tempDir.mkdirs();
            }
            f.createNewFile();
            InputStream in = zipFile.getInputStream(zipEntry);
            FileOutputStream out = new FileOutputStream(f);
            try {
                int c;
                byte[] by = new byte[BUFFEREDSIZE];
                while ((c = in.read(by)) != -1) {
                    out.write(by, 0, c);
                }
                out.flush();
            } catch (IOException e) {
                throw e;
            } finally {
                closeQuitely(out);
                closeQuitely(in);
            }
        }
    }
    zipFile.close();
}
Also used : Enumeration(java.util.Enumeration) ZipFile(java.util.zip.ZipFile) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) ZipFile(java.util.zip.ZipFile) File(java.io.File)

Example 50 with ZipFile

use of java.util.zip.ZipFile in project atlas by alibaba.

the class PatchMerger method merge.

public void merge() throws MergeException, IOException {
    try {
        File outputDirectory = updateInfo.workDir;
        File oringnalDir = new File(outputDirectory.getParentFile(), "orignal_" + updateInfo.baseVersion);
        if (!outputDirectory.exists()) {
            outputDirectory.mkdirs();
        }
        if (!oringnalDir.exists()) {
            oringnalDir.mkdirs();
        }
        ZipFile patchZip = new ZipFile(patchFile);
        Pair<File, String>[] updateBundles = new Pair[updateInfo.updateBundles.size()];
        List<MergeObject> toMergeList = new ArrayList<MergeObject>();
        //find original bundle and store in pair struct
        for (int x = 0; x < updateInfo.updateBundles.size(); x++) {
            UpdateInfo.Item item = updateInfo.updateBundles.get(x);
            String bundleName = item.name;
            String entryNamePrefix = String.format("%s%s", "lib", bundleName.replace(".", "_"));
            String entryName = entryNamePrefix + ".so";
            ZipEntry entry = patchZip.getEntry(entryName);
            if (patchZip.getEntry(entryName) != null && !supportMerge(bundleName)) {
                //全量部署
                File targetBundle = new File(outputDirectory, entryName);
                OutputStream outputStream = new FileOutputStream(targetBundle);
                InputStream inputStream = patchZip.getInputStream(entry);
                copyStream(inputStream, outputStream);
                mergeOutputs.put(bundleName, new Pair<>(targetBundle.getAbsolutePath(), item.version));
            } else {
                //差量部署
                File originalBundle = findOriginalBundleFile(bundleName, oringnalDir.getAbsolutePath(), item);
                if (originalBundle != null && originalBundle.exists()) {
                    updateBundles[x] = new Pair<File, String>(originalBundle, bundleName);
                    File targetBundle = new File(outputDirectory, "lib" + bundleName.replace(".", "_") + ".so");
                    if (targetBundle.exists()) {
                        targetBundle.delete();
                    }
                    toMergeList.add(new MergeObject(updateBundles[x].first.getAbsolutePath(), updateBundles[x].second, targetBundle.getAbsolutePath()));
                    mergeOutputs.put(bundleName, new Pair<>(targetBundle.getAbsolutePath(), item.version));
                }
            }
        }
        if (toMergeList.size() > 0) {
            DexMergeClient dexMergeClient = getMergeClient();
            boolean mergeFinish = dexMergeClient.dexMerge(patchFile.getAbsolutePath(), toMergeList, true);
            dexMergeClient.unPrepare();
            if (!mergeFinish) {
                throw new MergeException("merge failed!");
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        throw new MergeException("merge failed!");
    } finally {
        if (apkZip != null) {
            try {
                apkZip.close();
            } catch (Throwable e) {
            }
        }
    }
}
Also used : MergeObject(com.taobao.atlas.dexmerge.MergeObject) ZipEntry(java.util.zip.ZipEntry) ArrayList(java.util.ArrayList) MergeException(com.taobao.atlas.update.exception.MergeException) DexMergeClient(com.taobao.atlas.dexmerge.DexMergeClient) ZipFile(java.util.zip.ZipFile) MergeException(com.taobao.atlas.update.exception.MergeException) ZipFile(java.util.zip.ZipFile) UpdateInfo(com.taobao.atlas.update.model.UpdateInfo) Pair(android.util.Pair)

Aggregations

ZipFile (java.util.zip.ZipFile)580 ZipEntry (java.util.zip.ZipEntry)414 File (java.io.File)261 IOException (java.io.IOException)189 InputStream (java.io.InputStream)127 FileOutputStream (java.io.FileOutputStream)98 ZipOutputStream (java.util.zip.ZipOutputStream)89 Test (org.junit.Test)88 FileInputStream (java.io.FileInputStream)57 ArrayList (java.util.ArrayList)44 Enumeration (java.util.Enumeration)42 BufferedInputStream (java.io.BufferedInputStream)38 BufferedOutputStream (java.io.BufferedOutputStream)35 ZipException (java.util.zip.ZipException)32 ClassReader (org.objectweb.asm.ClassReader)29 OutputStream (java.io.OutputStream)27 JarFile (java.util.jar.JarFile)26 ZipInputStream (java.util.zip.ZipInputStream)26 FileNotFoundException (java.io.FileNotFoundException)23 Path (java.nio.file.Path)23