java - Adding file into existing Zip Archive -
so followed block of code here: http://commons.apache.org/proper/commons-compress/examples.html, said make ziparchiveentry , insert data. can see 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, passed file (apkfile) take "insert" file, parameter dictating compression method of file. running block of code results in 0 errors, zip file has "new" file in it. removes previous files , inserts new one.
prior commons-compresss, had copy entire zip temporary file, changes, , copy finalized zip file back. thought library worked around though?
you always want close()
streams when done them (i.e. out.close()
), preferably in block.
Comments
Post a Comment