8000 minizip/src/minizip.c at dev · tempbottle/minizip · GitHub
[go: up one dir, main page]

Skip to content

Latest commit

 

History

History
762 lines (637 loc) · 21.1 KB

minizip.c

File metadata and controls

762 lines (637 loc) · 21.1 KB
}
int32_t minizip_extract_onefile(void *handle, const char *filename, const char *destination, const char *password, minizip_opt *options)
{
int32_t err = mz_zip_locate_entry(handle, filename, NULL);
if (err != MZ_OK)
{
printf("File %s not found in the zip file\n", filename);
return err;
}
return minizip_extract_currentfile(handle, destination, password, options);
}
/***************************************************************************/
#ifndef NOMAIN
int main(int argc, char *argv[])
{
void *handle = NULL;
void *file_stream = NULL;
void *split_stream = NULL;
void *buf_stream = NULL;
char *path = NULL;
char *password = NULL;
char *destination = NULL;
char *filename_to_extract = NULL;
minizip_opt options;
int64_t disk_size = 0;
int32_t path_arg = 0;
uint8_t do_list = 0;
uint8_t do_extract = 0;
uint8_t buffered = 0;
int16_t mode = 0;
uint8_t append = 0;
int32_t err_close = 0;
int32_t err = 0;
int32_t i = 0;
minizip_banner();
if (argc == 1)
{
minizip_help();
return 0;
}
memset(&options, 0, sizeof(options));
options.compress_method = MZ_COMPRESS_METHOD_DEFLATE;
options.compress_level = MZ_COMPRESS_LEVEL_DEFAULT;
// Parse command line options
for (i = 1; i < argc; i += 1)
{
if ((*argv[i]) == '-')
{
const char *p = argv[i] + 1;
while ((*p) != '\0')
{
char c = *(p++);
if ((c == 'l') || (c == 'L'))
do_list = 1;
if ((c == 'x') || (c == 'X'))
do_extract = 1;
if ((c == 'a') || (c == 'A'))
append = 1;
if ((c == 'u') || (c == 'U'))
buffered = 1;
if ((c == 'o') || (c == 'O'))
options.overwrite = 1;
if ((c == 'j') || (c == 'J'))
options.exclude_path = 1;
if ((c >= '0') && (c <= '9'))
{
options.compress_level = (c - '0');
if (options.compress_level == 0)
options.compress_method = MZ_COMPRESS_METHOD_RAW;
}
#ifdef HAVE_BZIP2
if ((c == 'b') || (c == 'B'))
options.compress_method = MZ_COMPRESS_METHOD_BZIP2;
#endif
#ifdef HAVE_LZMA
if ((c == 'm') || (c == 'M'))
options.compress_method = MZ_COMPRESS_METHOD_LZMA;
#endif
#ifdef HAVE_AES
if ((c == 's') || (c == 'S'))
options.aes = 1;
#endif
if (((c == 'k') || (c == 'K')) && (i + 1 < argc))
{
disk_size = atoi(argv[i + 1]) * 1024;
i += 1;
}
if (((c == 'd') || (c == 'D')) && (i + 1 < argc))
{
destination = argv[i + 1];
i += 1;
}
if (((c == 'p') || (c == 'P')) && (i + 1 < argc))
{
password = argv[i + 1];
i += 1;
}
}
continue;
}
if (path_arg == 0)
path_arg = i;
}
if (path_arg == 0)
{
minizip_help();
return 0;
}
path = argv[path_arg];
mode = MZ_OPEN_MODE_READ;
if ((do_list == 0) && (do_extract == 0))
{
mode |= MZ_OPEN_MODE_WRITE;
if (mz_os_file_exists(path) != MZ_OK)
{
// If the file doesn't exist, we don't append file
mode |= MZ_OPEN_MODE_CREATE;
}
else if (append == 1)
{
mode |= MZ_OPEN_MODE_APPEND;
}
else if (options.overwrite == 0)
{
// If ask the user what to do because append and overwrite args not set
char rep = 0;
do
{
char answer[128];
printf("The file %s exists. Overwrite ? [y]es, [n]o, [a]ppend : ", path);
if (scanf("%1s", answer) != 1)
exit(EXIT_FAILURE);
rep = answer[0];
if ((rep >= 'a') && (rep <= 'z'))
rep -= 0x20;
}
while ((rep != 'Y') && (rep != 'N') && (rep != 'A'));
if (rep == 'A')
{
mode |= MZ_OPEN_MODE_APPEND;
}
else if (rep == 'Y')
{
mode |= MZ_OPEN_MODE_CREATE;
}
else if (rep == 'N')
{
minizip_help();
return 0;
}
}
}
mz_stream_os_create(&file_stream);
mz_stream_buffered_create(&buf_stream);
mz_stream_split_create(&split_stream);
if (buffered)
{
mz_stream_set_base(buf_stream, file_stream);
mz_stream_set_base(split_stream, buf_stream);
}
else
{
mz_stream_set_base(split_stream, file_stream);
}
mz_stream_split_set_prop_int64(split_stream, MZ_STREAM_PROP_DISK_SIZE, disk_size);
err = mz_stream_open(split_stream, path, mode);
if (err != MZ_OK)
{
printf("Error opening file %s\n", path);
}
else
{
handle = mz_zip_open(split_stream, mode);
if (handle == NULL)
{
printf("Error opening zip %s\n", path);
err = MZ_FORMAT_ERROR;
}
if (do_list)
{
err = minizip_list(handle);
}
else if (do_extract)
{
// Create target directory if it doesn't exist
if (destination != NULL)
mz_make_dir(destination);
if (argc > path_arg + 1)
filename_to_extract = argv[path_arg + 1];
if (filename_to_extract == NULL)
err = minizip_extract_all(handle, destination, password, &options);
else
err = minizip_extract_onefile(handle, filename_to_extract, destination, password, &options);
}
else
{
printf("Creating %s\n", path);
// Go through command line args looking for files to add to zip
for (i = path_arg + 1; (i < argc) && (err == MZ_OK); i += 1)
err = minizip_add(handle, argv[i], password, &options, 1);
mz_zip_set_version_madeby(handle, MZ_VERSION_MADEBY);
}
err_close = mz_zip_close(handle);
if (err_close != MZ_OK)
{
printf("Error in closing %s (%d)\n", path, err_close);
err = err_close;
}
mz_stream_close(split_stream);
}
mz_stream_split_delete(&split_stream);
mz_stream_buffered_delete(&buf_stream);
mz_stream_os_delete(&file_stream);
return err;
}
#endif
0