]> wimlib.net Git - wimlib/blob - examples/compressfile.c
unix_apply.c: Honor i_not_rpfixed
[wimlib] / examples / compressfile.c
1 /*
2  * compressfile.c
3  *
4  * An example of using wimlib's compression API to compress a file.
5  *
6  * This program does *not* have anything to do with WIM files other than the
7  * fact that this makes use of compression formats that are used in WIM files.
8  * This is purely an example of using the compression API.
9  *
10  * Compile with:
11  *
12  *    $ gcc compressfile.c -o compressfile -lwim
13  *
14  * Run with:
15  *
16  *    $ ./compressfile INFILE OUTFILE [LZX | XPRESS | LZMS] [chunk size]
17  *
18  *
19  * Use the decompressfile.c program to decompress the file.
20  *
21  * For example:
22  *
23  *    $ ./compressfile book.txt book.txt.lzms LZMS 1048576
24  *    $ rm -f book.txt
25  *    $ ./decompressfile book.txt.lzms book.txt
26  *
27  * The compressed file format created here is simply a series of compressed
28  * chunks.  A real format would need to have checksums and other metadata.
29  */
30
31 #define _GNU_SOURCE
32 #define _FILE_OFFSET_BITS 64
33
34 #include <wimlib.h>
35
36 #include <errno.h>
37 #include <error.h>
38 #include <fcntl.h>
39 #include <inttypes.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <unistd.h>
43
44 static void
45 do_compress(int in_fd, const char *in_filename,
46             int out_fd, const char *out_filename,
47             uint32_t chunk_size, struct wimlib_compressor *compressor)
48 {
49         char *ubuf = malloc(chunk_size);
50         char *cbuf = malloc(chunk_size - 1);
51         uint64_t chunk_num;
52
53         for (chunk_num = 1; ; chunk_num++) {
54                 ssize_t bytes_read;
55                 size_t csize;
56                 char *out_buf;
57                 uint32_t out_size;
58                 uint32_t usize;
59
60                 /* Read next chunk of data to compress.  */
61                 bytes_read = read(in_fd, ubuf, chunk_size);
62                 if (bytes_read <= 0) {
63                         if (bytes_read == 0)
64                                 break;
65                         error(1, errno, "Error reading \"%s\"", in_filename);
66                 }
67
68                 /* Compress the chunk.  */
69                 usize = bytes_read;
70
71                 csize = wimlib_compress(ubuf, usize, cbuf, usize - 1, compressor);
72                 if (csize != 0) {
73                         /* Chunk was compressed; use the compressed data.  */
74                         out_buf = cbuf;
75                         out_size = csize;
76                 } else {
77                         /* Chunk did not compress to less than original size;
78                          * use the uncompressed data.  */
79                         out_buf = ubuf;
80                         out_size = usize;
81                 }
82
83                 printf("Chunk %"PRIu64": %"PRIu32" => %"PRIu32" bytes\n",
84                        chunk_num, usize, out_size);
85
86                 /* Output the uncompressed chunk size, the compressed chunk
87                  * size, then the chunk data.  Note: a real program would need
88                  * to output the chunk sizes in consistent endianness.  */
89                 if (write(out_fd, &usize, sizeof(uint32_t)) != sizeof(uint32_t) ||
90                     write(out_fd, &out_size, sizeof(uint32_t)) != sizeof(uint32_t) ||
91                     write(out_fd, out_buf, out_size) != out_size)
92                 {
93                         error(1, errno, "Error writing to \"%s\"",
94                               out_filename);
95                 }
96         }
97         free(ubuf);
98         free(cbuf);
99 }
100
101 int main(int argc, char **argv)
102 {
103         const char *in_filename;
104         const char *out_filename;
105         int in_fd;
106         int out_fd;
107         struct wimlib_compressor *compressor;
108         int ctype = WIMLIB_COMPRESSION_TYPE_LZX;
109         uint32_t chunk_size = 32768;
110         int ret;
111
112         if (argc < 3 || argc > 5) {
113                 fprintf(stderr, "Usage: %s INFILE OUTFILE "
114                         "[LZX | XPRESS | LZMS] [chunk size]\n", argv[0]);
115                 return 2;
116         }
117
118         in_filename = argv[1];
119         out_filename = argv[2];
120
121         /* Parse compression type (optional)  */
122         if (argc >= 4) {
123                 if (!strcmp(argv[3], "LZX"))
124                         ctype = WIMLIB_COMPRESSION_TYPE_LZX;
125                 else if (!strcmp(argv[3], "XPRESS"))
126                         ctype = WIMLIB_COMPRESSION_TYPE_XPRESS;
127                 else if (!strcmp(argv[3], "LZMS"))
128                         ctype = WIMLIB_COMPRESSION_TYPE_LZMS;
129                 else
130                         error(1, 0, "Unrecognized compression type \"%s\"", argv[3]);
131         }
132         /* Parse chunk size (optional).  */
133         if (argc >= 5)
134                 chunk_size = atoi(argv[4]);
135
136         /* Open input file and output file.  */
137         in_fd = open(in_filename, O_RDONLY);
138         if (in_fd < 0)
139                 error(1, errno, "Failed to open \"%s\"", in_filename);
140         out_fd = open(out_filename, O_WRONLY | O_TRUNC | O_CREAT, 0644);
141         if (out_fd < 0)
142                 error(1, errno, "Failed to open \"%s\"", out_filename);
143
144         /* Create a compressor for the compression type and chunk size with the
145          * default parameters.  */
146         ret = wimlib_create_compressor(ctype, chunk_size, NULL, &compressor);
147         if (ret != 0)
148                 error(1, 0, "Failed to create compressor: %s",
149                       wimlib_get_error_string(ret));
150
151         uint32_t ctype32 = ctype;
152         /* Write compression type and chunk size to the file.  */
153         if (write(out_fd, &ctype32, sizeof(uint32_t)) != sizeof(uint32_t) ||
154             write(out_fd, &chunk_size, sizeof(uint32_t)) != sizeof(uint32_t))
155         {
156                 error(1, errno, "Error writing to \"%s\"",
157                       out_filename);
158         }
159
160         /* Compress and write the data.  */
161         do_compress(in_fd, in_filename,
162                     out_fd, out_filename,
163                     chunk_size, compressor);
164
165         /* Cleanup and return.  */
166         if (close(out_fd))
167                 error(1, errno, "Error closing \"%s\"", out_filename);
168         wimlib_free_compressor(compressor);
169         return 0;
170 }