zip4j icon indicating copy to clipboard operation
zip4j copied to clipboard

Adding a file to an existing zip can lead to corruption of the zip file

Open liufengwenyu opened this issue 2 years ago • 4 comments

A zip file has a data descriptor, and when a new file is added, there will be additional data at the end of the zip file. image struct ZIPDIRENTRY dirEntry[341] is the new file struct ZIPENDLOCATOR endLocator[0] is the End of central directory record of the new zip struct ZIPDIRENTRY dirEntry[342] is the is the last file of the old zip struct ZIPENDLOCATOR endLocator[1] is the End of central directory record of the old zip struct ZIPDIRENTRY dirEntry[342] and struct ZIPENDLOCATOR endLocator[1] are redundant

Below is my code

net.lingala.zip4j.ZipFile zipFile = null;
try {
    zipFile = new ZipFile(args[0]);
    ZipParameters parameters = new ZipParameters();
    parameters.setCompressionMethod(CompressionMethod.DEFLATE);
    parameters.setCompressionLevel(CompressionLevel.NORMAL);
    parameters.setFileNameInZip("assets/neoptionBin");
    zipFile.addFile(info, parameters);
} catch (Exception e) {
    e.printStackTrace();
} finally {
    try {
        if (zipFile != null) {
            zipFile.close();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

liufengwenyu avatar Mar 30 '23 01:03 liufengwenyu