Skip to content

Commit

Permalink
Added gzip example app.
Browse files Browse the repository at this point in the history
  • Loading branch information
nmoinvaz committed Apr 28, 2019
1 parent 64faa25 commit 3c43816
Show file tree
Hide file tree
Showing 9 changed files with 301 additions and 6 deletions.
12 changes: 10 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ matrix:
before_deploy:
- cd Release
- 7z a -tzip minizip-$TRAVIS_OS_NAME.zip *minizip*
- 7z a -tzip minigzip-$TRAVIS_OS_NAME.zip *minigzip*
deploy: &deploy_base
provider: releases
api_key:
secure: FJaUejGb9fRijDoIwLdaekcYRKCLVwyHJ2qv3kf6oaa5OTG9np73U3GYxImj20Z03GvsL2PIFJCKt3SbZ07prHL9Hf01llvH5RpbT29mepQiYbB9UZQKTh6syeHvYxWTMfw+lxMBs1QOnrYZtRzFeYQgnOSvLG2mudAnjxeBhH3duGnh9/uMW2+1UjNzld49HnR1F+AiVdZVAfKqwcZUsQENQ3svb4oNwMfAZrDMcfb5bO3aNat9EF5RBqOvtSFqrIGX6btsU/mh3acAoP2gcCjxmMcfCNlwaFYo8lcbNi4u0vW4nPUMXYfZCyc5KyIu/h/3Lu5ddVaw0ra51f25iT9e6hCBG9kBjk41Mu6AVKhebSvL2zd9/Q41BcVUbBxmGaDGORAJlCXNjqflVkAgYhRRjW5n//kN5TqObPqrIgTTbvhKTQYADQZ5Gza0zX0lZGlTZ9aT1r+gvSrX2T7DGnvz7zSKpGP4HIixPfaB9x+ZxAjohFhi47MLURtr1UhokvS3wonF5OR6dE/jHXdbS6Wi4GiHqKsWLC9a0iYrpkNZLp37byENCiOnZib/orOSerj2PgQFIGtfwt8osunxf6H1yI9A9VpysSsihmD65RvN3ii8HfTASU/K++VQdBvX7wjt2MP+KA59nKwGcobfSkB6nd7GYJhMgfOrsVfzJ4o=
file_glob: true
file: minizip-$TRAVIS_OS_NAME.zip
file: minigzip-$TRAVIS_OS_NAME.zip
name: $TRAVIS_TAG
skip_cleanup: true
on:
Expand Down Expand Up @@ -67,9 +69,12 @@ matrix:
env: TOOL="-DMZ_OPENSSL=ON"
before_deploy:
- ls *minizip* | tar -czvf minizip-$TRAVIS_OS_NAME.tar.gz -T -
- ls *minigzip* | tar -czvf minigzip-$TRAVIS_OS_NAME.tar.gz -T -
deploy:
<<: *deploy_base
file: minizip-$TRAVIS_OS_NAME.tar.gz
file:
- minizip-$TRAVIS_OS_NAME.tar.gz
- minigzip-$TRAVIS_OS_NAME.tar.gz
- stage: linux
os: linux
compiler: gcc
Expand All @@ -78,9 +83,12 @@ matrix:
compiler: clang
before_deploy:
- ls *minizip* | tar -czvf minizip-$TRAVIS_OS_NAME.tar.gz -T -
- ls *minigzip* | tar -czvf minigzip-$TRAVIS_OS_NAME.tar.gz -T -
deploy:
<<: *deploy_base
file: minizip-$TRAVIS_OS_NAME.tar.gz
file:
- minizip-$TRAVIS_OS_NAME.tar.gz
- minigzip-$TRAVIS_OS_NAME.tar.gz
- stage: macintosh
os: osx
compiler: gcc
Expand Down
10 changes: 9 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -640,7 +640,7 @@ if(NOT SKIP_INSTALL_FILES AND NOT SKIP_INSTALL_ALL)
install(FILES ${MINIZIP_PC} DESTINATION "${INSTALL_PKGCONFIG_DIR}")
endif()

# Build test executable
# Build test executables
if(MZ_BUILD_TEST)
add_executable(minizip_cmd "minizip.c" "test/test.c" "test/test.h")
set_target_properties(minizip_cmd PROPERTIES OUTPUT_NAME ${PROJECT_NAME})
Expand All @@ -649,6 +649,14 @@ if(MZ_BUILD_TEST)
if(NOT SKIP_INSTALL_BINARIES AND NOT SKIP_INSTALL_ALL)
install(TARGETS minizip_cmd RUNTIME DESTINATION "bin")
endif()

add_executable(minigzip_cmd "minigzip.c")
set_target_properties(minigzip_cmd PROPERTIES OUTPUT_NAME minigzip)
target_link_libraries(minigzip_cmd ${PROJECT_NAME})

if(NOT SKIP_INSTALL_BINARIES AND NOT SKIP_INSTALL_ALL)
install(TARGETS minigzip_cmd RUNTIME DESTINATION "bin")
endif()
endif()

if(MZ_BUILD_UNIT_TEST)
Expand Down
206 changes: 206 additions & 0 deletions minigzip.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
/* minigzip.c
Version 2.8.6, April 8, 2019
part of the MiniZip project
Copyright (C) 2010-2019 Nathan Moinvaziri
https://github.com/nmoinvaz/minizip
This program is distributed under the terms of the same license as zlib.
See the accompanying LICENSE file for the full text of the license.
*/


#include "mz.h"
#include "mz_os.h"
#include "mz_strm.h"
#include "mz_strm_os.h"
#include "mz_strm_zlib.h"

#include <stdio.h> /* printf */

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

#define MZ_GZIP_COMPRESS (1)
#define MZ_GZIP_DECOMPRESS (2)

int32_t minigzip_banner(void);
int32_t minigzip_help(void);

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

int32_t minigzip_banner(void)
{
printf("Minigzip %s - https://github.com/nmoinvaz/minizip\n", MZ_VERSION);
printf("---------------------------------------------------\n");
return MZ_OK;
}

int32_t minigzip_help(void)
{
printf("Usage : minigzip [-x] [-d] [-0 to -9] [files]\n\n" \
" -x Extract file\n" \
" -d Destination directory\n" \
" -0 Store only\n" \
" -1 Compress faster\n" \
" -9 Compress better\n\n");
return MZ_OK;
}

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

int32_t minigzip_copy(const char *path, const char *destination, int16_t operation, int16_t level)
{
void *target_stream = NULL;
void *source_stream = NULL;
void *zlib_stream = NULL;
const char *filename = NULL;
char target_path[1024] = { 0 };
int32_t err = 0;


if (destination != NULL)
{
if (mz_os_file_exists(destination) != MZ_OK)
mz_dir_make(destination);
mz_path_combine(target_path, destination, sizeof(target_path));
mz_path_get_filename(path, &filename);
mz_path_combine(target_path, filename, sizeof(target_path));
}
else
{
mz_path_combine(target_path, path, sizeof(target_path));
}

if (operation == MZ_GZIP_COMPRESS)
{
strncat(target_path, ".gz", sizeof(target_path) - strlen(target_path) - 1);
printf("Compressing to %s\n", target_path);
}
else if (operation == MZ_GZIP_DECOMPRESS)
{
mz_path_remove_extension(target_path);
printf("Decompressing to %s\n", target_path);
}

mz_stream_zlib_create(&zlib_stream);
mz_stream_zlib_set_prop_int64(zlib_stream, MZ_STREAM_PROP_COMPRESS_WINDOW, 15 + 16);

mz_stream_os_create(&source_stream);
err = mz_stream_os_open(source_stream, path, MZ_OPEN_MODE_READ);

if (err == MZ_OK)
{
mz_stream_os_create(&target_stream);
err = mz_stream_os_open(target_stream, target_path, MZ_OPEN_MODE_CREATE | MZ_OPEN_MODE_WRITE);

if (err == MZ_OK)
{
if (operation == MZ_GZIP_COMPRESS)
{
mz_stream_zlib_open(zlib_stream, target_path, MZ_OPEN_MODE_WRITE);
mz_stream_set_base(zlib_stream, target_stream);
err = mz_stream_copy_to_end(zlib_stream, source_stream);
}
else if (operation == MZ_GZIP_DECOMPRESS)
{
mz_stream_zlib_open(zlib_stream, path, MZ_OPEN_MODE_READ);
mz_stream_set_base(zlib_stream, source_stream);
err = mz_stream_copy_to_end(target_stream, zlib_stream);
}

if (err != MZ_OK)
printf("Error %d in zlib stream (%d)\n", err, mz_stream_zlib_error(zlib_stream));
else
printf("Operation completed successfully\n");

mz_stream_zlib_close(zlib_stream);
}
else
{
printf("Error %d opening target path %s\n", err, target_path);
}

mz_stream_os_close(target_stream);
mz_stream_os_delete(&target_stream);
}
else
{
printf("Error %d opening source path %s\n", err, path);
}

mz_stream_os_close(source_stream);
mz_stream_os_delete(&source_stream);

mz_stream_zlib_delete(&zlib_stream);
return err;
}

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

#if !defined(MZ_ZIP_NO_MAIN)
int main(int argc, const char *argv[])
{
int16_t operation_level = MZ_COMPRESS_LEVEL_DEFAULT;
int32_t path_arg = 0;
int32_t err = 0;
int32_t i = 0;
uint8_t operation = MZ_GZIP_COMPRESS;
const char *path = NULL;
const char *destination = NULL;


minigzip_banner();
if (argc == 1)
{
minigzip_help();
return 0;
}

/* Parse command line options */
for (i = 1; i < argc; i += 1)
{
printf("%s ", argv[i]);
if (argv[i][0] == '-')
{
char c = argv[i][1];
if ((c == 'x') || (c == 'X'))
operation = MZ_GZIP_DECOMPRESS;
else if ((c >= '0') && (c <= '9'))
operation_level = (c - '0');
else if (((c == 'd') || (c == 'D')) && (i + 1 < argc))
{
destination = argv[i + 1];
printf("%s ", argv[i + 1]);
i += 1;
}
else
{
err = MZ_SUPPORT_ERROR;
}
}
else if (path_arg == 0)
{
path_arg = i;
break;
}
}
printf("\n");

if (err == MZ_SUPPORT_ERROR)
{
printf("Feature not supported\n");
return err;
}

if (path_arg == 0)
{
minigzip_help();
return 0;
}

path = argv[path_arg];
err = minigzip_copy(path, destination, operation, operation_level);

return err;
}
#endif
28 changes: 28 additions & 0 deletions mz_os.c
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,34 @@ int32_t mz_path_remove_filename(char *path)
return MZ_OK;
}

int32_t mz_path_remove_extension(char *path)
{
char *path_ptr = NULL;

if (path == NULL)
return MZ_PARAM_ERROR;

path_ptr = path + strlen(path) - 1;

while (path_ptr > path)
{
if ((*path_ptr == '/') || (*path_ptr == '\\'))
break;
if (*path_ptr == '.')
{
*path_ptr = 0;
break;
}

path_ptr -= 1;
}

if (path_ptr == path)
*path_ptr = 0;

return MZ_OK;
}

int32_t mz_path_get_filename(const char *path, const char **filename)
{
const char *match = NULL;
Expand Down
3 changes: 3 additions & 0 deletions mz_os.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ int32_t mz_path_resolve(const char *path, char *target, int32_t max_target);
int32_t mz_path_remove_filename(char *path);
/* Remove the filename from a path */

int32_t mz_path_remove_extension(char *path);
/* Remove the extension from a path */

int32_t mz_path_get_filename(const char *path, const char **filename);
/* Get the filename from a path */

Expand Down
32 changes: 32 additions & 0 deletions mz_strm.c
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,11 @@ int32_t mz_stream_copy(void *target, void *source, int32_t len)
return mz_stream_copy_stream(target, NULL, source, NULL, len);
}

int32_t mz_stream_copy_to_end(void *target, void *source)
{
return mz_stream_copy_stream_to_end(target, NULL, source, NULL);
}

int32_t mz_stream_copy_stream(void *target, mz_stream_write_cb write_cb, void *source,
mz_stream_read_cb read_cb, int32_t len)
{
Expand Down Expand Up @@ -206,6 +211,33 @@ int32_t mz_stream_copy_stream(void *target, mz_stream_write_cb write_cb, void *s
return MZ_OK;
}

int32_t mz_stream_copy_stream_to_end(void *target, mz_stream_write_cb write_cb, void *source,
mz_stream_read_cb read_cb)
{
uint8_t buf[16384];
int32_t read = 0;
int32_t written = 0;

if (write_cb == NULL)
write_cb = mz_stream_write;
if (read_cb == NULL)
read_cb = mz_stream_read;

read = read_cb(source, buf, sizeof(buf));
while (read > 0)
{
written = write_cb(target, buf, read);
if (written != read)
return MZ_STREAM_ERROR;
read = read_cb(source, buf, sizeof(buf));
}

if (read < 0)
return MZ_STREAM_ERROR;

return MZ_OK;
}

int64_t mz_stream_tell(void *stream)
{
mz_stream *strm = (mz_stream *)stream;
Expand Down
3 changes: 3 additions & 0 deletions mz_strm.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ extern "C" {
#define MZ_STREAM_PROP_DISK_NUMBER (8)
#define MZ_STREAM_PROP_COMPRESS_LEVEL (9)
#define MZ_STREAM_PROP_COMPRESS_ALGORITHM (10)
#define MZ_STREAM_PROP_COMPRESS_WINDOW (11)

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

Expand Down Expand Up @@ -89,7 +90,9 @@ int32_t mz_stream_write_uint32(void *stream, uint32_t value);
int32_t mz_stream_write_int64(void *stream, int64_t value);
int32_t mz_stream_write_uint64(void *stream, uint64_t value);
int32_t mz_stream_copy(void *target, void *source, int32_t len);
int32_t mz_stream_copy_to_end(void *target, void *source);
int32_t mz_stream_copy_stream(void *target, mz_stream_write_cb write_cb, void *source, mz_stream_read_cb read_cb, int32_t len);
int32_t mz_stream_copy_stream_to_end(void *target, mz_stream_write_cb write_cb, void *source, mz_stream_read_cb read_cb);
int64_t mz_stream_tell(void *stream);
int32_t mz_stream_seek(void *stream, int64_t offset, int32_t origin);
int32_t mz_stream_find(void *stream, const void *find, int32_t find_size, int64_t max_seek, int64_t *position);
Expand Down
Loading

0 comments on commit 3c43816

Please sign in to comment.