zip4j icon indicating copy to clipboard operation
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

Open mdindoffer opened this issue 3 years ago • 0 comments
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:

  1. Add a flag to the ZipFile constructor to allow reuse of existing files by means of overwriting them
  2. Adding another constructor, or a factory method, to create a ZipFile backed by a new temporary file, e.g. ZipFile.createTempArchive(String prefix)

mdindoffer avatar Sep 16 '22 13:09 mdindoffer