Skip to content

Instantly share code, notes, and snippets.

@chenxiaolong
Created January 1, 2018 21:14
Show Gist options
  • Save chenxiaolong/dbab3fbef51b9d0fa969e220dbb85967 to your computer and use it in GitHub Desktop.
Save chenxiaolong/dbab3fbef51b9d0fa969e220dbb85967 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include "mz.h"
#include "mz_os.h"
#include "mz_strm_buf.h"
#include "mz_zip.h"
static int32_t list_files(void *handle)
{
int32_t ret = mz_zip_goto_first_entry(handle);
if (ret != MZ_OK && ret != MZ_END_OF_LIST) {
fprintf(stderr, "Failed to seek to first entry: %d\n", ret);
return ret;
}
do {
mz_zip_file *file_info;
ret = mz_zip_entry_get_info(handle, &file_info);
if (ret != MZ_OK) {
fprintf(stderr, "Failed to get entry info: %d\n", ret);
return ret;
}
printf("%s\n", file_info->filename);
} while ((ret = mz_zip_goto_next_entry(handle)) == MZ_OK);
if (ret != MZ_END_OF_LIST) {
fprintf(stderr, "Failed to seek to next entry: %d\n", ret);
return ret;
}
return MZ_OK;
}
int main(int argc, char *argv[])
{
void *handle = NULL;
void *file_stream = NULL;
void *buf_stream = NULL;
int ret;
int exit_code = EXIT_FAILURE;
if (argc == 1) {
fprintf(stderr, "Usage: %s <file>\n", argv[0]);
goto done;
}
if (!mz_stream_os_create(&file_stream)) {
fprintf(stderr, "Failed to create OS stream\n");
goto done;
}
if (!mz_stream_buffered_create(&buf_stream)) {
fprintf(stderr, "Failed to create buffered stream\n");
goto destroy_file_stream;
}
ret = mz_stream_set_base(buf_stream, file_stream);
if (ret != MZ_OK) {
fprintf(stderr, "Failed to set underlying stream for buffered stream: %d\n", ret);
goto destroy_buf_stream;
}
ret = mz_stream_open(buf_stream, argv[1], MZ_OPEN_MODE_READ);
if (ret != MZ_OK) {
fprintf(stderr, "Failed to open file: %d\n", ret);
goto destroy_buf_stream;
}
handle = mz_zip_open(buf_stream, MZ_OPEN_MODE_READ);
if (!handle) {
fprintf(stderr, "Failed to open zip\n");
goto close_stream;
}
ret = list_files(handle);
if (ret != MZ_OK) {
goto close_zip;
}
exit_code = EXIT_SUCCESS;
close_zip:
ret = mz_zip_close(handle);
if (ret != MZ_OK) {
fprintf(stderr, "Failed to close zip: %d\n", ret);
exit_code = EXIT_FAILURE;
}
close_stream:
ret = mz_stream_close(buf_stream);
if (ret != MZ_OK) {
fprintf(stderr, "Failed to close stream: %d\n", ret);
exit_code = EXIT_FAILURE;
}
destroy_buf_stream:
mz_stream_delete(&buf_stream);
destroy_file_stream:
mz_stream_delete(&file_stream);
done:
return exit_code;
}
@zcrhappy
Copy link

mz_zip_open has changed api for 3 args

@chenxiaolong
Copy link
Author

Thanks, though I don't intend to keep this snippet up to date. I only wrote it to demonstrate a bug in minizip that has since been fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment