4 * An example of using wimlib's compression API to compress a file.
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.
12 * $ gcc compressfile.c -o compressfile -lwim
16 * $ ./compressfile INFILE OUTFILE [LZX | XPRESS | LZMS] [chunk size]
19 * Use the decompressfile.c program to decompress the file.
23 * $ ./compressfile book.txt book.txt.lzms LZMS 1048576
25 * $ ./decompressfile book.txt.lzms book.txt
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.
32 #define _FILE_OFFSET_BITS 64
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)
49 char *ubuf = malloc(chunk_size);
50 char *cbuf = malloc(chunk_size - 1);
53 for (chunk_num = 1; ; chunk_num++) {
60 /* Read next chunk of data to compress. */
61 bytes_read = read(in_fd, ubuf, chunk_size);
62 if (bytes_read <= 0) {
65 error(1, errno, "Error reading \"%s\"", in_filename);
68 /* Compress the chunk. */
71 csize = wimlib_compress(ubuf, usize, cbuf, usize - 1, compressor);
73 /* Chunk was compressed; use the compressed data. */
77 /* Chunk did not compress to less than original size;
78 * use the uncompressed data. */
83 printf("Chunk %"PRIu64": %"PRIu32" => %"PRIu32" bytes\n",
84 chunk_num, usize, out_size);
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)
93 error(1, errno, "Error writing to \"%s\"",
101 int main(int argc, char **argv)
103 const char *in_filename;
104 const char *out_filename;
107 struct wimlib_compressor *compressor;
108 int ctype = WIMLIB_COMPRESSION_TYPE_LZX;
109 uint32_t chunk_size = 32768;
112 if (argc < 3 || argc > 5) {
113 fprintf(stderr, "Usage: %s INFILE OUTFILE "
114 "[LZX | XPRESS | LZMS] [chunk size]\n", argv[0]);
118 in_filename = argv[1];
119 out_filename = argv[2];
121 /* Parse compression type (optional) */
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;
130 error(1, 0, "Unrecognized compression type \"%s\"", argv[3]);
132 /* Parse chunk size (optional). */
134 chunk_size = atoi(argv[4]);
136 /* Open input file and output file. */
137 in_fd = open(in_filename, O_RDONLY);
139 error(1, errno, "Failed to open \"%s\"", in_filename);
140 out_fd = open(out_filename, O_WRONLY | O_TRUNC | O_CREAT, 0644);
142 error(1, errno, "Failed to open \"%s\"", out_filename);
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);
148 error(1, 0, "Failed to create compressor: %s",
149 wimlib_get_error_string(ret));
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))
156 error(1, errno, "Error writing to \"%s\"",
160 /* Compress and write the data. */
161 do_compress(in_fd, in_filename,
162 out_fd, out_filename,
163 chunk_size, compressor);
165 /* Cleanup and return. */
167 error(1, errno, "Error closing \"%s\"", out_filename);
168 wimlib_free_compressor(compressor);