Skip to content

Commit

Permalink
Move zip cd logic into zip reader/writer.
Browse files Browse the repository at this point in the history
Fixed compiler error due to #define.
  • Loading branch information
nmoinvaz committed Oct 28, 2018
1 parent cb9c40c commit 37b4da0
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 28 deletions.
19 changes: 2 additions & 17 deletions minizip.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,11 @@ int32_t minizip_list(const char *path)
struct tm tmu_date;
const char *string_method = NULL;
char crypt = ' ';

void *reader = NULL;


mz_zip_reader_create(&reader);
err = mz_zip_reader_open_file(reader, path);
if (err == MZ_OK)
err = mz_zip_reader_unzip_cd(reader);
if (err != MZ_OK)
{
printf("Error %d opening zip file %s\n", err, path);
Expand Down Expand Up @@ -304,8 +301,7 @@ int32_t minizip_add(const char *path, const char *password, minizip_opt *options
mz_zip_writer_set_overwrite_cb(writer, options, minizip_add_overwrite_cb);
mz_zip_writer_set_progress_cb(writer, options, minizip_add_progress_cb);
mz_zip_writer_set_entry_cb(writer, options, minizip_add_entry_cb);
if (options->zip_cd)
mz_zip_writer_set_flags(writer, MZ_ZIP_FLAG_MASK_LOCAL_INFO);
mz_zip_writer_set_zip_cd(writer, options->zip_cd);
if (options->cert_path != NULL)
mz_zip_writer_set_certificate(writer, options->cert_path, options->cert_pwd);

Expand All @@ -327,13 +323,6 @@ int32_t minizip_add(const char *path, const char *password, minizip_opt *options
printf("Error %d opening zip for writing\n", err);
}

if (options->zip_cd)
{
if (password != NULL)
flags = MZ_ZIP_FLAG_ENCRYPTED;
mz_zip_writer_zip_cd(writer, options->compress_method, flags);
}

err_close = mz_zip_writer_close(writer);
if (err_close != MZ_OK)
{
Expand Down Expand Up @@ -434,9 +423,7 @@ int32_t minizip_extract(const char *path, const char *pattern, const char *desti
}
else
{
err = mz_zip_reader_unzip_cd(reader);
if (err == MZ_OK)
err = mz_zip_reader_save_all(reader, destination);
err = mz_zip_reader_save_all(reader, destination);
if (err == MZ_END_OF_LIST && pattern != NULL)
printf("Files matching %s not found in zip file\n", pattern);
if (err != MZ_OK)
Expand Down Expand Up @@ -469,8 +456,6 @@ int32_t minizip_erase(const char *src_path, const char *target_path, int32_t arg
mz_zip_writer_create(&writer);

err = mz_zip_reader_open_file(reader, src_path);
if (err == MZ_OK)
err = mz_zip_reader_unzip_cd(reader);
if (err != MZ_OK)
{
printf("Error %d opening zip for reading %s\n", err, src_path);
Expand Down
28 changes: 19 additions & 9 deletions mz_zip_rw.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ int32_t mz_zip_reader_open(void *handle, void *stream)
return MZ_STREAM_ERROR;
}

mz_zip_reader_unzip_cd(reader);
return MZ_OK;
}

Expand All @@ -114,7 +115,6 @@ int32_t mz_zip_reader_open_file(void *handle, const char *path)
err = mz_stream_open(reader->split_stream, path, MZ_OPEN_MODE_READ);
if (err == MZ_OK)
err = mz_zip_reader_open(handle, reader->split_stream);

return err;
}

Expand Down Expand Up @@ -1051,7 +1051,7 @@ typedef struct mz_zip_writer_s {
const char *cert_pwd;
uint16_t compress_method;
int16_t compress_level;
int32_t flags;
uint8_t zip_cd;
uint8_t aes;
uint8_t raw;
uint8_t buffer[UINT16_MAX];
Expand Down Expand Up @@ -1197,6 +1197,10 @@ int32_t mz_zip_writer_close(void *handle)
mz_zip_writer *writer = (mz_zip_writer *)handle;
int32_t err = MZ_OK;


if (writer->zip_cd)
mz_zip_writer_zip_cd(writer);

if (writer->zip_handle != NULL)
{
mz_zip_set_version_madeby(writer->zip_handle, MZ_VERSION_MADEBY);
Expand Down Expand Up @@ -1227,7 +1231,7 @@ int32_t mz_zip_writer_close(void *handle)

/***************************************************************************/

int32_t mz_zip_writer_zip_cd(void *handle, uint16_t compress_method, int32_t flags)
int32_t mz_zip_writer_zip_cd(void *handle)
{
mz_zip_writer *writer = (mz_zip_writer *)handle;
mz_zip_file cd_file;
Expand All @@ -1250,9 +1254,12 @@ int32_t mz_zip_writer_zip_cd(void *handle, uint16_t compress_method, int32_t fla
cd_file.filename = MZ_ZIP_CD_FILENAME;
cd_file.modified_date = time(NULL);
cd_file.version_madeby = MZ_VERSION_MADEBY;
cd_file.compression_method = compress_method;
cd_file.compression_method = writer->compress_method;
cd_file.uncompressed_size = (int32_t)cd_mem_length;
cd_file.flag = MZ_ZIP_FLAG_UTF8 | flags;
cd_file.flag = MZ_ZIP_FLAG_UTF8;

if (writer->password != NULL)
cd_file.flag |= MZ_ZIP_FLAG_ENCRYPTED;

mz_stream_mem_create(&file_extra_stream);
mz_stream_mem_open(file_extra_stream, NULL, MZ_OPEN_MODE_CREATE);
Expand Down Expand Up @@ -1370,8 +1377,8 @@ int32_t mz_zip_writer_entry_close(void *handle)
mz_stream_mem_get_buffer_length(writer->file_extra_stream, &extrafield_size);

mz_zip_entry_set_extrafield(writer->zip_handle, extrafield, (uint16_t)extrafield_size);
#endif
}
#endif

if (writer->raw)
err = mz_zip_entry_close_raw(writer->zip_handle, writer->file_info.uncompressed_size,
Expand Down Expand Up @@ -1603,7 +1610,10 @@ int32_t mz_zip_writer_add_file(void *handle, const char *path, const char *filen
file_info.compression_method = writer->compress_method;
file_info.filename = filename;
file_info.uncompressed_size = mz_os_get_file_size(path);
file_info.flag = MZ_ZIP_FLAG_UTF8 | writer->flags;
file_info.flag = MZ_ZIP_FLAG_UTF8;

if (writer->zip_cd)
file_info.flag |= MZ_ZIP_FLAG_MASK_LOCAL_INFO;

#ifdef HAVE_AES
if (writer->aes)
Expand Down Expand Up @@ -1805,10 +1815,10 @@ void mz_zip_writer_set_compress_level(void *handle, int16_t compress_level)
writer->compress_level = compress_level;
}

void mz_zip_writer_set_flags(void *handle, int32_t flags)
void mz_zip_writer_set_zip_cd(void *handle, uint8_t zip_cd)
{
mz_zip_writer *writer = (mz_zip_writer *)handle;
writer->flags = flags;
writer->zip_cd = zip_cd;
}

void mz_zip_writer_set_certificate(void *handle, const char *cert_path, const char *cert_pwd)
Expand Down
4 changes: 2 additions & 2 deletions mz_zip_rw.h
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ int32_t mz_zip_writer_close(void *handle);

/***************************************************************************/

int32_t mz_zip_writer_zip_cd(void *handle, uint16_t compress_method, int32_t flags);
int32_t mz_zip_writer_zip_cd(void *handle);
// Zip the central directory

/***************************************************************************/
Expand Down Expand Up @@ -239,7 +239,7 @@ void mz_zip_writer_set_compress_method(void *handle, uint16_t compress_method
void mz_zip_writer_set_compress_level(void *handle, int16_t compress_level);
// Sets the compression level when adding files in zip

void mz_zip_writer_set_flags(void *handle, int32_t flags);
void mz_zip_writer_set_zip_cd(void *handle, uint8_t flags);
// Sets additional flags to be set when adding files in zip

void mz_zip_writer_set_certificate(void *handle, const char *cert_path, const char *cert_pwd);
Expand Down

0 comments on commit 37b4da0

Please sign in to comment.