Search in sources :

Example 51 with ZipFile

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

the class MergeExcutorServices method excute.

public void excute(String patchFilePath, List<MergeObject> list, boolean b) throws ExecutionException, InterruptedException {
    if (!b) {
        needMergeCount.set(list.size());
    }
    try {
        sZipPatch = new ZipFile(patchFilePath);
        Enumeration<? extends ZipEntry> zes = sZipPatch.entries();
        ZipEntry entry = null;
        String key = null;
        HashMap<String, List<ZipEntry>> bundleEntryGroup = new HashMap<String, List<ZipEntry>>();
        while (zes.hasMoreElements()) {
            entry = zes.nextElement();
            if (entry.getName().startsWith("lib")) {
                if (entry.getName().indexOf("/") != -1) {
                    key = entry.getName().substring(3, entry.getName().indexOf("/"));
                    os = OS.mac;
                } else if (entry.getName().indexOf("\\") != -1) {
                    key = entry.getName().substring(3, entry.getName().indexOf("\\"));
                    os = OS.windows;
                }
                List<ZipEntry> bundleEntry = null;
                if ((bundleEntry = bundleEntryGroup.get(key)) == null) {
                    bundleEntry = new ArrayList<ZipEntry>();
                    bundleEntryGroup.put(key, bundleEntry);
                    bundleEntry.add(entry);
                } else {
                    bundleEntryGroup.get(key).add(entry);
                }
            } else if (entry.getName().equals("libcom_taobao_maindex.so")) {
                List<ZipEntry> mainDex = new ArrayList<ZipEntry>();
                mainDex.add(entry);
                bundleEntryGroup.put("com_taobao_maindex", mainDex);
            }
        }
        for (MergeObject mergeObject : list) {
            MergeTask mergeTask = new MergeTask(new File(mergeObject.originalFile), bundleEntryGroup.get(mergeObject.patchName.replace(".", "_")), mergeObject.patchName, new File(mergeObject.mergeFile), b);
            Future future = es.submit(mergeTask);
            fList.add(future);
        }
        waitTaskCompleted();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (sZipPatch != null) {
            try {
                sZipPatch.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    es.shutdown();
    try {
        if (successCount.get() == needMergeCount.get()) {
            Log.e(TAG, "merge all finished");
            mCallback.onMergeAllFinish(true, null);
            successCount.set(0);
            needMergeCount.set(0);
        } else {
            mCallback.onMergeAllFinish(false, "merge failed!");
            Log.e(TAG, "merge all finish but failed!");
            successCount.set(0);
            needMergeCount.set(0);
        }
    } catch (RemoteException e) {
        e.printStackTrace();
    }
}
Also used : HashMap(java.util.HashMap) ZipEntry(java.util.zip.ZipEntry) ZipFile(java.util.zip.ZipFile) ArrayList(java.util.ArrayList) List(java.util.List) RemoteException(android.os.RemoteException) ZipFile(java.util.zip.ZipFile)

Example 52 with ZipFile

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

the class MergeTool method createNewBundleInternal.

public static void createNewBundleInternal(String patchBundleName, ZipFile source, List<ZipEntry> entryList, File target, boolean isDiff, MergeExcutorServices.PrepareCallBack prepareCallBack) throws IOException, MergeException {
    // get a temp file
    byte[] buffer = new byte[BUFFEREDSIZE];
    ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(target)));
    BufferedOutputStream bo = new BufferedOutputStream(out);
    InputStream in;
    //先写入source中未变的文件
    java.util.Enumeration e = source.entries();
    Boolean isSourceHasDex = false;
    Boolean isPatchHasDex = false;
    ZipEntry originalDex = null;
    ZipEntry patchDex = null;
    File outDex;
    while (e.hasMoreElements()) {
        ZipEntry zipEnt = (ZipEntry) e.nextElement();
        String name = zipEnt.getName();
        /**
             * 差量更新需要做dex merge, 过滤出classes.dex
             */
        if (isDiff && name.equals("classes.dex")) {
            //                originalDex = zipEnt;
            isSourceHasDex = true;
            continue;
        }
        boolean toBeDeleted = isBundleFileUpdated(zipEnt, entryList);
        if (!toBeDeleted) {
            ZipEntry newEntry = new ZipEntry(name);
            if (MergeExcutorServices.os == MergeExcutorServices.OS.mac) {
                if (name.contains("raw/") || name.contains("assets/")) {
                    newEntry.setMethod(ZipEntry.STORED);
                    newEntry.setCrc(zipEnt.getCrc());
                    newEntry.setSize(zipEnt.getSize());
                }
            } else {
                if (name.contains("raw\\") || name.contains("assets\\")) {
                    newEntry.setMethod(ZipEntry.STORED);
                    newEntry.setCrc(zipEnt.getCrc());
                    newEntry.setSize(zipEnt.getSize());
                }
            }
            out.putNextEntry(newEntry);
            in = source.getInputStream(zipEnt);
            write(in, out, buffer);
            bo.flush();
        }
    }
    if (!isSourceHasDex && isDiff) {
        throw new MergeException("Original bundle has no dex");
    }
    //        }
    for (ZipEntry entry : entryList) {
        if (isDiff && (entry.getName().endsWith("classes.dex"))) {
            patchDex = entry;
            isPatchHasDex = true;
            MergeExcutorServices.needMergeCount.incrementAndGet();
            continue;
        }
        ZipEntry newEntry = null;
        if (MergeExcutorServices.os == MergeExcutorServices.OS.mac) {
            newEntry = new ZipEntry(entry.getName().substring(entry.getName().indexOf("/") + 1));
            if (newEntry.getName().contains("raw/") || newEntry.getName().contains("assets/")) {
                newEntry.setMethod(ZipEntry.STORED);
                newEntry.setCrc(entry.getCrc());
                newEntry.setSize(entry.getSize());
            }
        } else {
            newEntry = new ZipEntry(entry.getName().substring(entry.getName().indexOf("\\") + 1));
            if (newEntry.getName().contains("raw\\") || newEntry.getName().contains("assets\\")) {
                newEntry.setMethod(ZipEntry.STORED);
                newEntry.setCrc(entry.getCrc());
                newEntry.setSize(entry.getSize());
            }
        }
        out.putNextEntry(newEntry);
        in = MergeExcutorServices.sZipPatch.getInputStream(entry);
        write(in, out, buffer);
        bo.flush();
    }
    /**
         * 差量更新需要做dex merge
         */
    if (isDiff) {
        // Merge patch dex with origin dex
        if (isPatchHasDex && isSourceHasDex) {
            //发出merge申请
            //                File outDexDir = new File(patch, "out");
            ByteArrayOutputStream outDexStream = new ByteArrayOutputStream();
            dexMerge(patchBundleName, source, patchDex, outDexStream, prepareCallBack);
            //                if (outDexStream.exists()) {
            //                    /**
            //                     * caculate the merged dex md5 and report
            //                     */
            //                    String md5 = Md5Utils.getFileMD5String(outDex);
            //                    MonitorReport.getInstance().trace(source.getName(), md5, "" + outDex.length());
            //                }
            //                zip(out, outDex, outDex.getName(), bo);
            ByteArrayInputStream swapStream = new ByteArrayInputStream(outDexStream.toByteArray());
            ZipEntry entry = new ZipEntry("classes.dex");
            out.putNextEntry(entry);
            write(swapStream, out, buffer);
            bo.flush();
        } else if (isSourceHasDex) {
            // Patch has no classes.dex, just use the original dex
            //                outDex = new File(patch, "classes.dex");
            //                inputStreamToFile(source.getInputStream(originalDex), outDex);
            //                zip(out, outDex, outDex.getName(), bo);
            ZipEntry entry = new ZipEntry("classes.dex");
            out.putNextEntry(entry);
            in = source.getInputStream(source.getEntry("classes.dex"));
            write(in, out, buffer);
            bo.flush();
        }
    } else {
        MergeExcutorServices.successCount.incrementAndGet();
    }
    closeQuitely(out);
    closeQuitely(bo);
}
Also used : ZipEntry(java.util.zip.ZipEntry) java.util(java.util) ZipOutputStream(java.util.zip.ZipOutputStream) ZipFile(java.util.zip.ZipFile)

Example 53 with ZipFile

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

the class MergeTool method createNewMainApkInternal.

private static void createNewMainApkInternal(ZipFile sourceZip, List<ZipEntry> entryList, File tempFile, boolean isDiff, MergeExcutorServices.PrepareCallBack prepareCallBack) throws IOException {
    byte[] buffer = new byte[BUFFEREDSIZE];
    ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(tempFile)));
    BufferedOutputStream bo = new BufferedOutputStream(out);
    InputStream in;
    //先写入source中未变的文件
    java.util.Enumeration e = sourceZip.entries();
    int i = 1;
    File mainDexFile = new File(tempFile.getParentFile(), "libcom_taobao_maindex.zip");
    inputStreamToFile(MergeExcutorServices.sZipPatch.getInputStream(entryList.get(0)), mainDexFile);
    ZipFile zipFile = new ZipFile(mainDexFile);
    Dex patchDex = new Dex(zipFile.getInputStream(zipFile.getEntry("classes.dex")));
    Iterator<ClassDef> iterators = patchDex.classDefs().iterator();
    List<String> patchClassNames = new ArrayList<String>();
    while (iterators.hasNext()) {
        ClassDef classDef = iterators.next();
        int typeIndex = classDef.getTypeIndex();
        Log.e("MergeTool", "merge class:" + patchDex.typeNames().get(typeIndex));
        patchClassNames.add(patchDex.typeNames().get(typeIndex));
    }
    while (e.hasMoreElements()) {
        ZipEntry zipEnt = (ZipEntry) e.nextElement();
        String name = zipEnt.getName();
        if (isDiff && name.endsWith(".dex")) {
            i++;
            InputStream inputStream = sourceZip.getInputStream(zipEnt);
            Dex dex = processDex(inputStream, patchClassNames);
            ZipEntry zipEntry = new ZipEntry(name);
            out.putNextEntry(zipEntry);
            write(new ByteArrayInputStream(dex.getBytes()), out, buffer);
            bo.flush();
        }
    }
    Enumeration entries = zipFile.entries();
    while (entries.hasMoreElements()) {
        ZipEntry zipEnt = (ZipEntry) entries.nextElement();
        String name = zipEnt.getName();
        if (name.endsWith(".dex")) {
            ZipEntry zipEntry = new ZipEntry(String.format("%s%s%s", "classes", i, ".dex"));
            out.putNextEntry(zipEntry);
            write(zipFile.getInputStream(zipEnt), out, buffer);
            bo.flush();
            i++;
            continue;
        }
        ZipEntry newEntry = new ZipEntry(name);
        if (name.contains("raw/") || name.contains("assets/")) {
            newEntry.setMethod(ZipEntry.STORED);
            newEntry.setCrc(zipEnt.getCrc());
            newEntry.setSize(zipEnt.getSize());
        }
        out.putNextEntry(newEntry);
        in = zipFile.getInputStream(zipEnt);
        write(in, out, buffer);
        bo.flush();
    }
    mainDexFile.delete();
    zipFile.close();
    closeQuitely(out);
    closeQuitely(bo);
}
Also used : ZipEntry(java.util.zip.ZipEntry) ClassDef(com.taobao.atlas.dex.ClassDef) java.util(java.util) ZipFile(java.util.zip.ZipFile) ZipOutputStream(java.util.zip.ZipOutputStream) Dex(com.taobao.atlas.dex.Dex) ZipFile(java.util.zip.ZipFile)

Example 54 with ZipFile

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

the class FileOperation method zipFile.

private static void zipFile(File resFile, ZipOutputStream zipout, String rootpath) throws IOException {
    rootpath = rootpath + (rootpath.trim().length() == 0 ? "" : File.separator) + resFile.getName();
    if (resFile.isDirectory()) {
        File[] fileList = resFile.listFiles();
        for (File file : fileList) {
            zipFile(file, zipout, rootpath);
        }
    } else {
        final byte[] fileContents = readContents(resFile);
        //linux format!!
        if (rootpath.contains("\\")) {
            rootpath = rootpath.replace("\\", "/");
        }
        ZipEntry entry = new ZipEntry(rootpath);
        //            if (compressMethod == ZipEntry.DEFLATED) {
        entry.setMethod(ZipEntry.DEFLATED);
        //            } else {
        //                entry.setMethod(ZipEntry.STORED);
        //                entry.setSize(fileContents.length);
        //                final CRC32 checksumCalculator = new CRC32();
        //                checksumCalculator.update(fileContents);
        //                entry.setCrc(checksumCalculator.getValue());
        //            }
        zipout.putNextEntry(entry);
        zipout.write(fileContents);
        zipout.flush();
        zipout.closeEntry();
    }
}
Also used : ZipEntry(java.util.zip.ZipEntry) File(java.io.File) ZipFile(java.util.zip.ZipFile)

Example 55 with ZipFile

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

the class FileOperation method unZipAPk.

@SuppressWarnings("rawtypes")
public static void unZipAPk(String fileName, String filePath) throws IOException {
    checkDirectory(filePath);
    ZipFile zipFile = new ZipFile(fileName);
    Enumeration enumeration = zipFile.entries();
    try {
        while (enumeration.hasMoreElements()) {
            ZipEntry entry = (ZipEntry) enumeration.nextElement();
            if (entry.isDirectory()) {
                new File(filePath, entry.getName()).mkdirs();
                continue;
            }
            BufferedInputStream bis = new BufferedInputStream(zipFile.getInputStream(entry));
            File file = new File(filePath + File.separator + entry.getName());
            File parentFile = file.getParentFile();
            if (parentFile != null && (!parentFile.exists())) {
                parentFile.mkdirs();
            }
            FileOutputStream fos = null;
            BufferedOutputStream bos = null;
            try {
                fos = new FileOutputStream(file);
                bos = new BufferedOutputStream(fos, TypedValue.BUFFER_SIZE);
                byte[] buf = new byte[TypedValue.BUFFER_SIZE];
                int len;
                while ((len = bis.read(buf, 0, TypedValue.BUFFER_SIZE)) != -1) {
                    fos.write(buf, 0, len);
                }
            } finally {
                if (bos != null) {
                    bos.flush();
                    bos.close();
                }
                if (bis != null) {
                    bis.close();
                }
            }
        }
    } finally {
        if (zipFile != null) {
            zipFile.close();
        }
    }
}
Also used : Enumeration(java.util.Enumeration) ZipFile(java.util.zip.ZipFile) BufferedInputStream(java.io.BufferedInputStream) ZipEntry(java.util.zip.ZipEntry) FileOutputStream(java.io.FileOutputStream) File(java.io.File) ZipFile(java.util.zip.ZipFile) BufferedOutputStream(java.io.BufferedOutputStream)

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