Skip to content
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

mz_zip_add_mem_to_archive_file_in_place() creates duplicate file entry if file being added already exists in archive #278

Open
nyq opened this issue Aug 13, 2023 · 3 comments

Comments

@nyq
Copy link
Contributor

nyq commented Aug 13, 2023

mz_zip_add_mem_to_archive_file_in_place() function seems to work great adding a new file into archive. However, if a file with the same name already exists in the archive, it creates a second entry with the same name instead of overwriting existing file in the archive. I think a better behavior would be:

  1. Introduce MZ_ZIP_FLAG_ALLOW_OVERWRITE flag.
  2. When mz_zip_add_mem_to_archive_file_in_place() is called with this flag, overwrite a file if it exists in the archive.
  3. mz_zip_add_mem_to_archive_file_in_place without MZ_ZIP_FLAG_ALLOW_OVERWRITE flag, refuse to overwrite a file if it exists in the archive and return false.

Alternatively, it would be nice to add a high-level function to delete an existing file from the archive.

@rebirth123
Copy link

rebirth123 commented Aug 17, 2023

I just started using this version of miniz and have also discovered this problem. During my tests I called add to zip file (with the same "pArchive_name") a number of times. mz_zip_reader_get_num_files even includes the duplicates into the count. This doesn't feel like it's the proper behaviour. "_in_place" suggests it should overwrite if it doesn't exist.

I'm trying to figure out a workaround, but like I've said, I've only been using it for a couple of hours and only to create, add and read files from and to zip archives. A hint would be welcomed.

@rebirth123
Copy link

rebirth123 commented Aug 18, 2023

I got around it the hard way:

Try and open the zip file with "mz_zip_reader_init_file". If successful read the archive names and compare them to the new archive names to be added. If no duplicate found add them to a list of indices to keep. Create a write zip with "mz_zip_writer_init_file"using a temporary filename and clone the indices to keep from the read zip into the write_zip using "mz_zip_writer_add_from_zip_reader". When done, add the new data using either "mz_zip_writer_add_file" or "mz_zip_writer_add_mem"(if working from memory). Finally call "mz_zip_writer_finalize_archive" and then call mz_zip_writer_end and mz_zip_reader_end.

Then just delete the original zip file and rename the new zip file.

@ell1e
Copy link
Contributor

ell1e commented Nov 26, 2023

For what it's worth, I vaguely remember having similar issues with Python. And since stuff shifts around when replacing entries, I assume solving it on miniz's side might not be that easy either. So maybe the best solution is to just rewrite changing archives like @rebirth123 did.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants