所以我在这里跟随了代码块:http://commons.apache.org/proper/commons-compress/examples.html,其中据说只需制作一个ZipArchiveEntry然后插入数据即可。正如你可以看到我的代码如下。将文件添加到现有的Zip归档文件中
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);
}
}
基本上,其传递的文件(apkFile),将采取“插入”文件,与其它参数支配该文件的压缩方法。运行这段代码会导致0错误,但ZIP文件中只包含该“新”文件。它删除所有以前的文件,然后插入新的文件。
在commons-compresss之前,我不得不将整个Zip复制到一个临时文件,执行我的更改,然后将该最终Zip文件复制回来。但我认为这个图书馆能够解决这个问题?
你是否关闭了'out'? – jtahlborn 2013-05-02 14:52:45
ahh,忘记了一件简单的事情:/添加关闭现在只是覆盖整个Zip存档到我插入的任何文件。 – 2013-05-02 14:56:25
然后,您应该编辑问题并添加该问题。另外,为什么使用'String'参数,如果你只是把它转换为'int'?为什么不使用'int'参数? – acdcjunior 2013-05-02 15:00:55