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