zip4j
zip4j copied to clipboard
Add an option to use an existing file when creating new zip archive, or provide a utility method for generating Temp archives
trafficstars
A very common usecase is to zip some files into a temporary archive. The most straightforward way to do this would be to create a temp file and then feed it to zip4j:
Path tmpFilePath = Files.createTempFile("tmpFilePrefix", ".zip");
ZipFile zipFile = new ZipFile(tmpTestDownload.toFile());
This however fails with:
net.lingala.zip4j.exception.ZipException: Zip file size less than minimum expected zip file size. Probably not a zip file or a corrupted zip file
at net.lingala.zip4j.headers.HeaderReader.readAllHeaders(HeaderReader.java:70)
at net.lingala.zip4j.ZipFile.readZipInfo(ZipFile.java:1135)
at net.lingala.zip4j.ZipFile.addStream(ZipFile.java:418)
because the API expects the passed file to be a valid ZIP archive. This is really a pitfall of trying to create opaque API that magically tries to either open a zip file or create one based on hidden variables.
Nevertheless, there are two possible ways to solve this problem:
- Add a flag to the
ZipFileconstructor to allow reuse of existing files by means of overwriting them - Adding another constructor, or a factory method, to create a
ZipFilebacked by a new temporary file, e.g.ZipFile.createTempArchive(String prefix)