From 604d14200b4ce8d206113f1304d869a3838b2eaa Mon Sep 17 00:00:00 2001 From: abrasat Date: Wed, 6 Sep 2023 20:45:44 +0200 Subject: [PATCH] Fix zip_close (#306) Co-authored-by: ZN --- src/zip.c | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/zip.c b/src/zip.c index 3d64dd49..31182970 100644 --- a/src/zip.c +++ b/src/zip.c @@ -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); }