-
Notifications
You must be signed in to change notification settings - Fork 317
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add an option to use an existing file when creating new zip archive, or provide a utility method for generating Temp archives #476
Comments
My solution for avoiding this problem
It's a little ridiculous, but works for me |
if the zip file does not exist, zip4j will not try to read or open it. If the zip file exists, it has to be in a valid zip format, otherwise zip4j cannot add more content to the zip file. My guess is that your temp file already exists, and is not a valid zip file, and you get this error. Try using the suggestion above: delete the temp file (if your use case permits) before adding content to it. But if the file exists, it has to a valid zip file. |
Umm, what? This is not a question, I was reporting a UX bug. The behaviour you describe is exactly the problem here. It should be fixed, like I describe in the OP. The API is badly designed, because the assumption of using only a nonexistent file to facilitate writing is just flawed. If you delete a file before writing to it - why even create it in the first place? Those are not atomic operations, someone might take up the filename in the meantime, while you fiddle with deleting and recreating the file. Once again, there are 2 backwards-compatible solutions to this:
|
Fixed in v2.11.5 released today. Zip4j now allows overriding empty files and doesn't throw that exception as explained here @mdindoffer It really helps if people are polite around here. I am trying to do my best to help people in my extremely limited spare time. If you have any problem using this library, feel free to use a different one or you can contribute with a pull request. |
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:
This however fails with:
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:
ZipFile
constructor to allow reuse of existing files by means of overwriting themZipFile
backed by a new temporary file, e.g.ZipFile.createTempArchive(String prefix)
The text was updated successfully, but these errors were encountered: