|
2 | 2 |
|
3 | 3 | [](https://github.com/trufae/mzip/actions/workflows/ci.yml) |
4 | 4 |
|
5 | | -This is a minimalistic implementation of the ZIP file format, designed to be small, portable, and dependency-free. |
6 | | - |
7 | | -**NOTE** This code has been initially written by Claude Code |
| 5 | +A minimalistic, dependency-free ZIP implementation with support for multiple compression algorithms. |
8 | 6 |
|
9 | 7 | ## Features |
10 | 8 |
|
11 | | -- Small library with minimal dependencies |
12 | | -- Full read/write support for ZIP archives |
13 | | -- Zero dependencies (includes a tiny deflate implementation) |
14 | | -- Simple API compatible with a subset of libzip |
15 | | -- Suitable for embedded systems and small applications |
16 | | - |
17 | | -## Configuration |
18 | | - |
19 | | -The library can be configured by editing `config.h`. You can enable or disable specific compression algorithms based on your needs: |
20 | | - |
21 | | -```c |
22 | | -/* Enable/disable compression algorithms */ |
23 | | -#define MZIP_ENABLE_STORE 1 /* Always include store method */ |
24 | | -#define MZIP_ENABLE_DEFLATE 1 /* Include DEFLATE compression */ |
25 | | -#define MZIP_ENABLE_ZSTD 1 /* Include ZSTD compression */ |
26 | | -#define MZIP_ENABLE_LZFSE 1 /* LZFSE compression support */ |
27 | | -/* #define MZIP_ENABLE_LZ4 1 */ /* LZ4 compression (commented out by default) */ |
28 | | -#define MZIP_ENABLE_LZMA 1 /* LZMA compression support */ |
29 | | -#define MZIP_ENABLE_BROTLI 1 /* Brotli compression support */ |
30 | | -``` |
31 | | -
|
32 | | -## Components |
33 | | -
|
34 | | -The implementation consists of the following main parts: |
35 | | -
|
36 | | -1. `mzip.c` - Minimalistic libzip subset replacement |
37 | | -2. `deflate.inc.c` - Minimalistic deflate (RFC 1951) implementation compatible with zlib API |
38 | | -3. `zstd.inc.c` - Minimalistic zstd implementation |
39 | | -4. `lzma.inc.c` - Lightweight LZMA compression implementation |
40 | | -5. `brotli.inc.c` - Brotli compression implementation |
41 | | -6. `lzfse.inc.c` - Apple's LZFSE compression implementation |
42 | | -7. `config.h` - Configuration options for enabling/disabling compression methods |
| 9 | +- Small, portable library with zero dependencies |
| 10 | +- Full ZIP read/write support |
| 11 | +- Multiple compression algorithms (DEFLATE, ZSTD, LZMA, Brotli, LZFSE) |
| 12 | +- Simple API compatible with libzip subset |
| 13 | +- Suitable for embedded systems |
43 | 14 |
|
44 | 15 | ## Building |
45 | 16 |
|
46 | | -To build the library: |
47 | | -
|
48 | 17 | ```bash |
49 | | -# Configure which algorithms to include in config.h first |
50 | | -# Edit config.h to enable or disable specific compression methods |
| 18 | +# Build default binary |
| 19 | +make |
| 20 | + |
| 21 | +# Build with all compression algorithms |
| 22 | +make all-compression |
| 23 | + |
| 24 | +# Install |
| 25 | +make install |
51 | 26 |
|
52 | | -# Then compile |
53 | | -gcc -std=c99 -c -DMZIP_IMPLEMENTATION mzip.c |
54 | | -gcc -o mzip mzip.o |
| 27 | +# Run tests |
| 28 | +make -C test |
55 | 29 | ``` |
56 | 30 |
|
57 | 31 | ## Usage |
58 | 32 |
|
| 33 | +### Library API |
| 34 | + |
59 | 35 | ```c |
60 | | -#include "mzip.c" // This includes deflate.c internally |
| 36 | +#include "mzip.h" |
61 | 37 |
|
62 | | -// Open a zip file |
63 | | -int err = 0; |
| 38 | +// Open ZIP file |
64 | 39 | zip_t *za = zip_open("archive.zip", ZIP_RDONLY, &err); |
65 | 40 |
|
66 | 41 | // Read files |
67 | | -zip_uint64_t n = zip_get_num_files(za); |
68 | | -for (zip_uint64_t i = 0; i < n; ++i) { |
| 42 | +zip_uint64_t num_files = zip_get_num_files(za); |
| 43 | +for (zip_uint64_t i = 0; i < num_files; ++i) { |
69 | 44 | zip_file_t *zf = zip_fopen_index(za, i, 0); |
70 | | - // Use zf->data and zf->size |
| 45 | + // Process zf->data and zf->size |
71 | 46 | zip_fclose(zf); |
72 | 47 | } |
73 | 48 |
|
74 | | -// Add files |
| 49 | +// Create ZIP file |
75 | 50 | zip_t *za_write = zip_open("new.zip", ZIP_CREATE, &err); |
76 | | -void *buffer = malloc(size); |
77 | | -// Fill buffer with data |
78 | 51 | zip_source_t *src = zip_source_buffer(za_write, buffer, size, 1); |
79 | | -zip_int64_t index = zip_file_add(za_write, "filename.txt", src, 0); |
80 | | - |
81 | | -// Set compression method (you can use any supported method) |
82 | | -zip_set_file_compression(za_write, index, MZIP_METHOD_ZSTD, 0); // For zstd compression |
83 | | -// Other available methods: |
84 | | -// zip_set_file_compression(za_write, index, MZIP_METHOD_LZMA, 0); // For LZMA compression |
85 | | -// zip_set_file_compression(za_write, index, MZIP_METHOD_BROTLI, 0); // For Brotli compression |
86 | | -// zip_set_file_compression(za_write, index, MZIP_METHOD_LZFSE, 0); // For LZFSE compression |
87 | | -// zip_set_file_compression(za_write, index, MZIP_METHOD_STORE, 0); // For no compression |
| 52 | +zip_file_add(za_write, "file.txt", src, 0); |
| 53 | +zip_set_file_compression(za_write, index, MZIP_METHOD_ZSTD, 0); |
88 | 54 | zip_close(za_write); |
89 | | - |
90 | | -// Close when done |
91 | | -zip_close(za); |
92 | 55 | ``` |
93 | 56 |
|
94 | | -## Tool Usage |
95 | | -
|
96 | | -The included `mzip` tool demonstrates the library's functionality: |
| 57 | +### Command Line Tool |
97 | 58 |
|
98 | 59 | ```bash |
99 | 60 | # List contents |
100 | 61 | ./mzip -l archive.zip |
101 | 62 |
|
102 | | -# Extract all files |
| 63 | +# Extract files |
103 | 64 | ./mzip -x archive.zip |
104 | 65 |
|
105 | | -# Create new archive |
| 66 | +# Create archive |
106 | 67 | ./mzip -c archive.zip file1 file2 |
107 | 68 |
|
108 | | -# Add files to existing archive |
109 | | -./mzip -a archive.zip file3 file4 |
| 69 | +# Add files |
| 70 | +./mzip -a archive.zip file3 |
110 | 71 | ``` |
111 | 72 |
|
112 | | -## Limitations |
113 | | - |
114 | | -- No support for encryption, ZIP64, or data descriptors |
115 | | -- Current implementation has limited support for extracting multiple files |
116 | | -- Limited testing with third-party ZIP utilities when using non-standard compression methods |
117 | | - |
118 | | -## License |
119 | | - |
120 | | -MIT / 0-BSD – do whatever you want; attribution appreciated. |
| 73 | +## Configuration |
121 | 74 |
|
122 | | -## Compression |
| 75 | +Edit `config.h` to enable/disable compression algorithms: |
123 | 76 |
|
124 | | - mzip now supports the following compression algorithms used by Apple and Google for mobile app packaging: |
| 77 | +```c |
| 78 | +#define MZIP_ENABLE_DEFLATE 1 |
| 79 | +#define MZIP_ENABLE_ZSTD 1 |
| 80 | +#define MZIP_ENABLE_LZMA 1 |
| 81 | +#define MZIP_ENABLE_BROTLI 1 |
| 82 | +#define MZIP_ENABLE_LZFSE 1 |
| 83 | +``` |
125 | 84 |
|
126 | | - 1. **DEFLATE** (Method ID: 8): Used in both APK and IPA files |
127 | | - - Default ZIP compression algorithm |
128 | | - - Good compatibility with all ZIP tools |
129 | | - - Standard in most ZIP implementations |
| 85 | +## Supported Compression Algorithms |
130 | 86 |
|
131 | | - 2. **Zstandard (zstd)** (Method ID: 93): Increasingly adopted by Google (Android 12+) |
132 | | - - Better balance of compression ratio and speed |
133 | | - - Significantly faster decompression than DEFLATE |
134 | | - - Scalable compression levels |
| 87 | +- **DEFLATE** (ID: 8): Standard ZIP compression |
| 88 | +- **ZSTD** (ID: 93): Fast, high compression |
| 89 | +- **LZMA** (ID: 14): High compression ratio |
| 90 | +- **Brotli** (ID: 97): Better than DEFLATE for static content |
| 91 | +- **LZFSE** (ID: 100): Apple's mobile-optimized algorithm |
| 92 | +- **STORE** (ID: 0): No compression |
135 | 93 |
|
136 | | - 3. **LZFSE** (Method ID: 100): Apple's proprietary algorithm used in newer IPA files |
137 | | - - Optimized for mobile hardware |
138 | | - - Battery efficient |
139 | | - - Good balance of compression and speed |
| 94 | +## Limitations |
140 | 95 |
|
141 | | - 4. **Brotli** (Method ID: 97): Used by Google in newer Android versions |
142 | | - - Better compression ratios than DEFLATE (20-26% better) |
143 | | - - Good for static content in apps |
| 96 | +- No encryption or ZIP64 support |
| 97 | +- Limited multi-file extraction |
| 98 | +- Non-standard methods may not work with all ZIP tools |
144 | 99 |
|
145 | | - 5. **LZMA** (Method ID: 14): Occasionally used for specific resources |
146 | | - - Higher compression ratios |
147 | | - - Slower decompression |
148 | | - |
149 | | - 6. **STORE** (Method ID: 0): No compression |
150 | | - - Raw file storage without compression |
151 | | - - Used as a fallback when compression doesn't reduce file size |
| 100 | +## License |
152 | 101 |
|
153 | | - Support for **LZ4** (Method ID: 94) is included in the codebase but disabled by default. It can be enabled in config.h. |
| 102 | +MIT / 0-BSD |
0 commit comments