So I followed the block of code here: http://commons.apache.org/proper/commons-compress/examples.html, where is said to simply make a ZipArchiveEntry and then insert the data. As you can see by my code below.
public void insertFile(File apkFile, File insert, String method)
throws AndrolibException {
ZipArchiveOutputStream out = null;
ZipArchiveEntry entry;
try {
byte[] data = Files.toByteArray(insert);
out = new ZipArchiveOutputStream(new FileOutputStream(apkFile, true));
out.setMethod(Integer.parseInt(method));
CRC32 crc = new CRC32();
crc.update(data);
entry = new ZipArchiveEntry(insert.getName());
entry.setSize(data.length);
entry.setTime(insert.lastModified());
entry.setCrc(crc.getValue());
out.putArchiveEntry(entry);
out.write(data);
out.closeArchiveEntry();
out.close();
} catch (FileNotFoundException ex) {
throw new AndrolibException(ex);
} catch (IOException ex) {
throw new AndrolibException(ex);
}
}
Basically, its passed the File (apkFile) that will take the "insert" File, with another parameter dictating the compression method of that file. Running this block of code results in 0 errors, but the ZIP file only has that "new" file in it. It removes all the previous files and then inserts that new one.
Prior to commons-compresss, I had to copy the entire Zip to a temporary file, do my changes, and then copy that finalized Zip file back. I thought this library worked around that though?
You always want to close()
streams when you are done with them (i.e. out.close()
), preferably in a finally block.