use of java.util.zip.ZipOutputStream 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.ZipOutputStream 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.ZipOutputStream in project Apktool by iBotPeaches.
the class AndrolibResources method installFramework.
public void installFramework(File frameFile, String tag) throws AndrolibException {
InputStream in = null;
ZipOutputStream out = null;
try {
ZipFile zip = new ZipFile(frameFile);
ZipEntry entry = zip.getEntry("resources.arsc");
if (entry == null) {
throw new AndrolibException("Can't find resources.arsc file");
}
in = zip.getInputStream(entry);
byte[] data = IOUtils.toByteArray(in);
ARSCData arsc = ARSCDecoder.decode(new ByteArrayInputStream(data), true, true);
publicizeResources(data, arsc.getFlagsOffsets());
File outFile = new File(getFrameworkDir(), String.valueOf(arsc.getOnePackage().getId()) + (tag == null ? "" : '-' + tag) + ".apk");
out = new ZipOutputStream(new FileOutputStream(outFile));
out.setMethod(ZipOutputStream.STORED);
CRC32 crc = new CRC32();
crc.update(data);
entry = new ZipEntry("resources.arsc");
entry.setSize(data.length);
entry.setCrc(crc.getValue());
out.putNextEntry(entry);
out.write(data);
out.closeEntry();
//Write fake AndroidManifest.xml file to support original aapt
entry = zip.getEntry("AndroidManifest.xml");
if (entry != null) {
in = zip.getInputStream(entry);
byte[] manifest = IOUtils.toByteArray(in);
CRC32 manifestCrc = new CRC32();
manifestCrc.update(manifest);
entry.setSize(manifest.length);
entry.setCompressedSize(-1);
entry.setCrc(manifestCrc.getValue());
out.putNextEntry(entry);
out.write(manifest);
out.closeEntry();
}
zip.close();
LOGGER.info("Framework installed to: " + outFile);
} catch (IOException ex) {
throw new AndrolibException(ex);
} finally {
IOUtils.closeQuietly(in);
IOUtils.closeQuietly(out);
}
}
use of java.util.zip.ZipOutputStream in project Apktool by iBotPeaches.
the class Androlib method buildUnknownFiles.
public void buildUnknownFiles(File appDir, File outFile, MetaInfo meta) throws AndrolibException {
if (meta.unknownFiles != null) {
LOGGER.info("Copying unknown files/dir...");
Map<String, String> files = meta.unknownFiles;
File tempFile = new File(outFile.getParent(), outFile.getName() + ".apktool_temp");
boolean renamed = outFile.renameTo(tempFile);
if (!renamed) {
throw new AndrolibException("Unable to rename temporary file");
}
try (ZipFile inputFile = new ZipFile(tempFile);
ZipOutputStream actualOutput = new ZipOutputStream(new FileOutputStream(outFile))) {
copyExistingFiles(inputFile, actualOutput);
copyUnknownFiles(appDir, actualOutput, files);
} catch (IOException ex) {
throw new AndrolibException(ex);
}
// Remove our temporary file.
tempFile.delete();
}
}
use of java.util.zip.ZipOutputStream in project OpenNotebook by jaltekruse.
the class NotebookPanel method zipDirectory.
/**
* Method to save the contents of a directory into a zip file.
*
* Code found online at:
* http://www.java2s.com/Code/Java/File-Input-Output/CompressfilesusingtheJavaZIPAPI.htm
*
*/
public static void zipDirectory(File dir, File zipfile) throws IOException, IllegalArgumentException {
// Check that the directory is a directory, and get its contents
String[] entries = dir.list();
// Create a buffer for copying
byte[] buffer = new byte[4096];
int bytesRead;
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipfile));
for (int i = 0; i < entries.length; i++) {
File f = new File(dir, entries[i]);
if (f.isDirectory())
//Ignore directory
continue;
// Stream to read file
FileInputStream in = new FileInputStream(f);
// Make a ZipEntry
ZipEntry entry = new ZipEntry(f.getPath());
// Store entry
out.putNextEntry(entry);
while ((bytesRead = in.read(buffer)) != -1) out.write(buffer, 0, bytesRead);
in.close();
}
out.close();
}
Aggregations