因此,我在这里遵循了以下代码块:http://commons.apache.org/proper/commons-compress/examples.html,据说在这里只需制作一个ZipArchiveEntry,然后插入数据。如下面的代码所示。

    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-compress压缩之前,我必须将整个Zip复制到一个临时文件中,进行更改,然后再将最终的Zip文件复制回去。我以为这个图书馆可以解决这个问题?

最佳答案

处理完流(即close())后,您总是希望out.close()流,最好是在finally块中。

关于java - 将文件添加到现有的Zip存档中,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16340880/

10-12 00:27
查看更多