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);
}
}
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);
}
}
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);
}
}
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);
}
}
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);
}
Aggregations