Search in sources :

Example 96 with ZipEntry

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

the class SoLoader method extractSoFromApk.

private static synchronized void extractSoFromApk(String soName) {
    AtlasFileLock.getInstance().LockExclusive(LIB_DIR);
    if (findLocalLibrary(soName) != null) {
        return;
    }
    try {
        int retryCount = 2;
        do {
            --retryCount;
            String entryName = String.format("lib/armeabi/%s", soName);
            ZipEntry targetEntry = null;
            if (ApkUtils.getApk() != null && (targetEntry = ApkUtils.getApk().getEntry(entryName)) != null) {
                File targetFile = new File(LIB_DIR, soName);
                File targetFileTmp = new File(LIB_DIR, soName + ".tmp");
                if (!targetFileTmp.exists() || targetFileTmp.length() != targetEntry.getSize()) {
                    if (targetFileTmp.exists()) {
                        targetFileTmp.delete();
                    }
                    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(targetFileTmp.getAbsolutePath()));
                    BufferedInputStream bi = new BufferedInputStream(ApkUtils.getApk().getInputStream(targetEntry));
                    byte[] readContent = new byte[512];
                    int readCount = bi.read(readContent);
                    while (readCount != -1) {
                        bos.write(readContent, 0, readCount);
                        readCount = bi.read(readContent);
                    }
                    try {
                        bos.close();
                        bi.close();
                    } catch (Throwable e) {
                    }
                }
                if (targetFileTmp.exists()) {
                    if (targetFileTmp.length() == targetEntry.getSize()) {
                        targetFileTmp.renameTo(targetFile);
                        if (!targetFile.exists()) {
                            targetFileTmp.renameTo(targetFile);
                        }
                        if (targetFile.exists()) {
                            break;
                        }
                    } else {
                        targetFileTmp.delete();
                    }
                }
            }
        } while (retryCount > 0);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        AtlasFileLock.getInstance().unLock(LIB_DIR);
    }
}
Also used : BufferedInputStream(java.io.BufferedInputStream) ZipEntry(java.util.zip.ZipEntry) FileOutputStream(java.io.FileOutputStream) ZipFile(java.util.zip.ZipFile) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) IOException(java.io.IOException)

Example 97 with ZipEntry

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

the class ProcessAwoAndroidResources method doFullTaskAction.

@Override
protected void doFullTaskAction() throws IOException {
    // we have to clean the source folder output in case the package name changed.
    File srcOut = getSourceOutputDir();
    if (srcOut != null) {
        // FileUtils.emptyFolder(srcOut);
        srcOut.delete();
        srcOut.mkdirs();
    }
    @Nullable File resOutBaseNameFile = getPackageOutputFile();
    // If are in instant run mode and we have an instant run enabled manifest
    File instantRunManifest = getInstantRunManifestFile();
    File manifestFileToPackage = instantRunBuildContext.isInInstantRunMode() && instantRunManifest != null && instantRunManifest.exists() ? instantRunManifest : getManifestFile();
    // 增加awb模块编译所需要的额外参数
    addAaptOptions();
    AaptPackageProcessBuilder aaptPackageCommandBuilder = new AaptPackageProcessBuilder(manifestFileToPackage, getAaptOptions()).setAssetsFolder(getAssetsDir()).setResFolder(getResDir()).setLibraries(getLibraries()).setPackageForR(getPackageForR()).setSourceOutputDir(absolutePath(srcOut)).setSymbolOutputDir(absolutePath(getTextSymbolOutputDir())).setResPackageOutput(absolutePath(resOutBaseNameFile)).setProguardOutput(absolutePath(getProguardOutputFile())).setType(getType()).setDebuggable(getDebuggable()).setPseudoLocalesEnabled(getPseudoLocalesEnabled()).setResourceConfigs(getResourceConfigs()).setSplits(getSplits()).setPreferredDensity(getPreferredDensity());
    @NonNull AtlasBuilder builder = (AtlasBuilder) getBuilder();
    // MergingLog mergingLog = new MergingLog(getMergeBlameLogFolder());
    //
    // ProcessOutputHandler processOutputHandler = new ParsingProcessOutputHandler(
    // new ToolOutputParser(new AaptOutputParser(), getILogger()),
    // builder.getErrorReporter());
    ProcessOutputHandler processOutputHandler = new LoggedProcessOutputHandler(getILogger());
    try {
        builder.processAwbResources(aaptPackageCommandBuilder, getEnforceUniquePackageName(), processOutputHandler, getMainSymbolFile());
        if (resOutBaseNameFile != null) {
            if (instantRunBuildContext.isInInstantRunMode()) {
                instantRunBuildContext.addChangedFile(InstantRunBuildContext.FileType.RESOURCES, resOutBaseNameFile);
                // get the new manifest file CRC
                JarFile jarFile = new JarFile(resOutBaseNameFile);
                String currentIterationCRC = null;
                try {
                    ZipEntry entry = jarFile.getEntry(SdkConstants.ANDROID_MANIFEST_XML);
                    if (entry != null) {
                        currentIterationCRC = String.valueOf(entry.getCrc());
                    }
                } finally {
                    jarFile.close();
                }
                // check the manifest file binary format.
                File crcFile = new File(instantRunSupportDir, "manifest.crc");
                if (crcFile.exists() && currentIterationCRC != null) {
                    // compare its content with the new binary file crc.
                    String previousIterationCRC = Files.readFirstLine(crcFile, Charsets.UTF_8);
                    if (!currentIterationCRC.equals(previousIterationCRC)) {
                        instantRunBuildContext.close();
                        instantRunBuildContext.setVerifierResult(InstantRunVerifierStatus.BINARY_MANIFEST_FILE_CHANGE);
                    }
                }
                // write the new manifest file CRC.
                Files.createParentDirs(crcFile);
                Files.write(currentIterationCRC, crcFile, Charsets.UTF_8);
            }
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    } catch (ProcessException e) {
        throw new RuntimeException(e);
    }
}
Also used : LoggedProcessOutputHandler(com.android.ide.common.process.LoggedProcessOutputHandler) ProcessOutputHandler(com.android.ide.common.process.ProcessOutputHandler) ZipEntry(java.util.zip.ZipEntry) IOException(java.io.IOException) JarFile(java.util.jar.JarFile) ProcessException(com.android.ide.common.process.ProcessException) AaptPackageProcessBuilder(com.android.builder.core.AaptPackageProcessBuilder) NonNull(com.android.annotations.NonNull) AtlasBuilder(com.android.builder.core.AtlasBuilder) LoggedProcessOutputHandler(com.android.ide.common.process.LoggedProcessOutputHandler) JarFile(java.util.jar.JarFile) OutputFile(org.gradle.api.tasks.OutputFile) File(java.io.File) InputFile(org.gradle.api.tasks.InputFile) Nullable(com.android.annotations.Nullable)

Example 98 with ZipEntry

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

the class DexInstallTask method zip.

private static void zip(File[] files, String baseFolder, ZipOutputStream zos) throws Exception {
    byte[] buffer = new byte[1024];
    FileInputStream fis = null;
    ZipEntry entry = null;
    int count = 0;
    for (File file : files) {
        if (file.isDirectory()) {
            zip(file.listFiles(), baseFolder + file.getName() + File.separator, zos);
            continue;
        }
        entry = new ZipEntry(baseFolder + file.getName());
        zos.putNextEntry(entry);
        fis = new FileInputStream(file);
        while ((count = fis.read(buffer, 0, buffer.length)) != -1) zos.write(buffer, 0, count);
    }
}
Also used : ZipEntry(java.util.zip.ZipEntry) OutputFile(org.gradle.api.tasks.OutputFile) InputFile(org.gradle.api.tasks.InputFile)

Example 99 with ZipEntry

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

the class ApBuildTask method addFile.

/**
     * jar文件里增加文件
     *
     * @param jos
     * @param file
     */
private void addFile(JarOutputStream jos, File file, String entryName) {
    if (null == file || !file.exists()) {
        return;
    }
    byte[] buf = new byte[8064];
    InputStream in = null;
    if (null == file || !file.exists()) {
        return;
    }
    try {
        in = new FileInputStream(file);
        ZipEntry fileEntry = new ZipEntry(entryName);
        jos.putNextEntry(fileEntry);
        // Transfer bytes from the file to the ZIP file
        int len;
        while ((len = in.read(buf)) > 0) {
            jos.write(buf, 0, len);
        }
        // Complete the entry
        jos.closeEntry();
        in.close();
    } catch (IOException e) {
        throw new RuntimeException(e.getMessage(), e);
    }
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream)

Example 100 with ZipEntry

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

the class TPatchDiffApkBuildTask method doApkBuild.

@TaskAction
public void doApkBuild() throws Exception {
    //TODO 合并2个zip包
    apkFile = getApkFile();
    diffAPkFile = getDiffAPkFile();
    File tmpDir = new File(diffAPkFile.getParentFile(), "tmp-apk");
    if (tmpDir.exists()) {
        FileUtils.deleteDirectory(tmpDir);
    }
    if (!tmpDir.exists()) {
        tmpDir.mkdirs();
    }
    Map zipEntityMap = new HashMap();
    ZipUtils.unzip(apkFile, tmpDir.getAbsolutePath(), "UTF-8", zipEntityMap, true);
    FileUtils.deleteDirectory(new File(tmpDir, "assets"));
    FileUtils.deleteDirectory(new File(tmpDir, "res"));
    new File(tmpDir, "resources.arsc").delete();
    File tmpDir2 = new File(diffAPkFile.getParentFile(), "tmp-diffApk");
    tmpDir2.mkdirs();
    ZipUtils.unzip(getResourceFile(), tmpDir2.getAbsolutePath(), "UTF-8", new HashMap<String, ZipEntry>(), true);
    new File(tmpDir2, "AndroidManifest.xml").delete();
    FileUtils.copyDirectory(tmpDir2, tmpDir);
    ZipUtils.rezip(diffAPkFile, tmpDir, zipEntityMap);
}
Also used : HashMap(java.util.HashMap) ZipEntry(java.util.zip.ZipEntry) File(java.io.File) OutputFile(org.gradle.api.tasks.OutputFile) InputFile(org.gradle.api.tasks.InputFile) HashMap(java.util.HashMap) Map(java.util.Map) MtlBaseTaskAction(com.taobao.android.builder.tasks.manager.MtlBaseTaskAction) TaskAction(org.gradle.api.tasks.TaskAction)

Aggregations

ZipEntry (java.util.zip.ZipEntry)1367 ZipFile (java.util.zip.ZipFile)479 File (java.io.File)469 IOException (java.io.IOException)361 ZipOutputStream (java.util.zip.ZipOutputStream)321 ZipInputStream (java.util.zip.ZipInputStream)300 InputStream (java.io.InputStream)282 FileOutputStream (java.io.FileOutputStream)278 FileInputStream (java.io.FileInputStream)270 Test (org.junit.Test)124 BufferedInputStream (java.io.BufferedInputStream)122 JarFile (java.util.jar.JarFile)122 BufferedOutputStream (java.io.BufferedOutputStream)99 ByteArrayOutputStream (java.io.ByteArrayOutputStream)97 ArrayList (java.util.ArrayList)84 ByteArrayInputStream (java.io.ByteArrayInputStream)78 OutputStream (java.io.OutputStream)67 JarOutputStream (java.util.jar.JarOutputStream)59 Path (java.nio.file.Path)56 Enumeration (java.util.Enumeration)56