Debian Codegraph @

minigzip.c

raw symbols SCIP
1 /* minigzip.c
2 part of the minizip-ng project
3
4 Copyright (C) Nathan Moinvaziri
5 https://github.com/zlib-ng/minizip-ng
6
7 This program is distributed under the terms of the same license as zlib.
8 See the accompanying LICENSE file for the full text of the license.
9 */
10
11 #include "mz.h"
12 #include "mz_os.h"
13 #include "mz_strm.h"
14 #include "mz_strm_os.h"
15 #include "mz_strm_zlib.h"
16
17 #include <stdio.h> /* printf */
18
19 /***************************************************************************/
20
21 #define MZ_GZIP_COMPRESS (1)
22 #define MZ_GZIP_DECOMPRESS (2)
23
24 int32_t minigzip_banner(void);
25 int32_t minigzip_help(void);
26
27 /***************************************************************************/
28
29 int32_t minigzip_banner(void) {
30 printf("Minigzip %s - https://github.com/zlib-ng/minizip-ng\n", MZ_VERSION);
31 printf("---------------------------------------------------\n");
32 return MZ_OK;
33 }
34
35 int32_t minigzip_help(void) {
36 printf(
37 "Usage: minigzip [-x] [-d] [-0 to -9] [files]\n\n"
38 " -x Extract file\n"
39 " -d Destination directory\n"
40 " -0 Store only\n"
41 " -1 Compress faster\n"
42 " -9 Compress better\n\n");
43 return MZ_OK;
44 }
45
46 /***************************************************************************/
47
48 int32_t minigzip_copy(const char *path, const char *destination, int16_t operation, int16_t level) {
49 void *target_stream = NULL;
50 void *source_stream = NULL;
51 void *zlib_stream = NULL;
52 const char *filename = NULL;
53 char target_path[1024];
54 int32_t err = 0;
55
56 memset(target_path, 0, sizeof(target_path));
57
58 if (destination) {
59 if (mz_os_file_exists(destination) != MZ_OK)
60 mz_dir_make(destination);
61 }
62
63 if (operation == MZ_GZIP_COMPRESS) {
64 mz_path_combine(target_path, path, sizeof(target_path));
65 strncat(target_path, ".gz", sizeof(target_path) - strlen(target_path) - 1);
66 printf("Compressing to %s\n", target_path);
67 } else if (operation == MZ_GZIP_DECOMPRESS) {
68 if (destination)
69 mz_path_combine(target_path, destination, sizeof(target_path));
70
71 if (mz_path_get_filename(path, &filename) != MZ_OK)
72 filename = path;
73
74 mz_path_combine(target_path, filename, sizeof(target_path));
75 mz_path_remove_extension(target_path);
76 printf("Decompressing to %s\n", target_path);
77 }
78
79 zlib_stream = mz_stream_zlib_create();
80 mz_stream_zlib_set_prop_int64(zlib_stream, MZ_STREAM_PROP_COMPRESS_WINDOW, 15 + 16);
81
82 source_stream = mz_stream_os_create();
83 err = mz_stream_os_open(source_stream, path, MZ_OPEN_MODE_READ);
84
85 if (err == MZ_OK) {
86 target_stream = mz_stream_os_create();
87 err = mz_stream_os_open(target_stream, target_path, MZ_OPEN_MODE_CREATE | MZ_OPEN_MODE_WRITE);
88
89 if (err == MZ_OK) {
90 if (operation == MZ_GZIP_COMPRESS) {
91 mz_stream_zlib_set_prop_int64(zlib_stream, MZ_STREAM_PROP_COMPRESS_LEVEL, level);
92 mz_stream_zlib_open(zlib_stream, target_path, MZ_OPEN_MODE_WRITE);
93 mz_stream_set_base(zlib_stream, target_stream);
94 err = mz_stream_copy_to_end(zlib_stream, source_stream);
95 } else if (operation == MZ_GZIP_DECOMPRESS) {
96 mz_stream_zlib_open(zlib_stream, path, MZ_OPEN_MODE_READ);
97 mz_stream_set_base(zlib_stream, source_stream);
98 err = mz_stream_copy_to_end(target_stream, zlib_stream);
99 }
100
101 if (err != MZ_OK)
102 printf("Error %d in zlib stream (%d)\n", err, mz_stream_zlib_error(zlib_stream));
103 else
104 printf("Operation completed successfully\n");
105
106 mz_stream_zlib_close(zlib_stream);
107 } else {
108 printf("Error %d opening target path %s\n", err, target_path);
109 }
110
111 mz_stream_os_close(target_stream);
112 mz_stream_os_delete(&target_stream);
113 } else {
114 printf("Error %d opening source path %s\n", err, path);
115 }
116
117 mz_stream_os_close(source_stream);
118 mz_stream_os_delete(&source_stream);
119
120 mz_stream_zlib_delete(&zlib_stream);
121 return err;
122 }
123
124 /***************************************************************************/
125
126 #if !defined(MZ_ZIP_NO_MAIN)
127 int main(int argc, const char *argv[]) {
128 int16_t operation_level = MZ_COMPRESS_LEVEL_DEFAULT;
129 int32_t path_arg = 0;
130 int32_t err = 0;
131 int32_t i = 0;
132 uint8_t operation = MZ_GZIP_COMPRESS;
133 const char *path = NULL;
134 const char *destination = NULL;
135
136 minigzip_banner();
137 if (argc == 1) {
138 minigzip_help();
139 return 0;
140 }
141
142 /* Parse command line options */
143 for (i = 1; i < argc; i += 1) {
144 printf("%s ", argv[i]);
145 if (argv[i][0] == '-') {
146 char c = argv[i][1];
147 if ((c == 'x') || (c == 'X'))
148 operation = MZ_GZIP_DECOMPRESS;
149 else if ((c >= '0') && (c <= '9'))
150 operation_level = (c - '0');
151 else if (((c == 'd') || (c == 'D')) && (i + 1 < argc)) {
152 destination = argv[i + 1];
153 printf("%s ", argv[i + 1]);
154 i += 1;
155 } else {
156 err = MZ_SUPPORT_ERROR;
157 }
158 } else if (path_arg == 0) {
159 path_arg = i;
160 break;
161 }
162 }
163 printf("\n");
164
165 if (err == MZ_SUPPORT_ERROR) {
166 printf("Feature not supported\n");
167 return err;
168 }
169
170 if (path_arg == 0) {
171 minigzip_help();
172 return 0;
173 }
174
175 path = argv[path_arg];
176 err = minigzip_copy(path, destination, operation, operation_level);
177
178 return err;
179 }
180 #endif