Skip to content

Commit

Permalink
Fix zip_close (#306)
Browse files Browse the repository at this point in the history
Co-authored-by: ZN <[email protected]>
  • Loading branch information
abrasat and ZN authored Sep 6, 2023
1 parent f8e1129 commit 604d142
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/zip.c
Original file line number Diff line number Diff line change
Expand Up @@ -882,12 +882,21 @@ struct zip_t *zip_open(const char *zipname, int level, char mode) {

void zip_close(struct zip_t *zip) {
if (zip) {
mz_zip_archive *pZip = &(zip->archive);
// Always finalize, even if adding failed for some reason, so we have a
// valid central directory.
mz_zip_writer_finalize_archive(&(zip->archive));
zip_archive_truncate(&(zip->archive));
mz_zip_writer_end(&(zip->archive));
mz_zip_reader_end(&(zip->archive));
if (pZip->m_zip_mode == MZ_ZIP_MODE_WRITING) {
mz_zip_writer_finalize_archive(pZip);
}

if (pZip->m_zip_mode == MZ_ZIP_MODE_WRITING ||
pZip->m_zip_mode == MZ_ZIP_MODE_WRITING_HAS_BEEN_FINALIZED) {
zip_archive_truncate(pZip);
mz_zip_writer_end(pZip);
}
if (pZip->m_zip_mode == MZ_ZIP_MODE_READING) {
mz_zip_reader_end(pZip);
}

CLEANUP(zip);
}
Expand Down

0 comments on commit 604d142

Please sign in to comment.