1 ///////////////////////////////////////////////////////////////////////////////
3 /// \file 11_file_info.c
4 /// \brief Get uncompressed size of .xz file(s)
6 /// Usage: ./11_file_info INFILE1.xz [INFILEn.xz]...
8 /// Example: ./11_file_info foo.xz
10 // Author: Lasse Collin
12 // This file has been put into the public domain.
13 // You can do whatever you want with this file.
15 ///////////////////////////////////////////////////////////////////////////////
27 print_file_size(lzma_stream
*strm
, FILE *infile
, const char *filename
)
29 // Get the file size. In standard C it can be done by seeking to
30 // the end of the file and then getting the file position.
31 // In POSIX one can use fstat() and then st_size from struct stat.
32 // Also note that fseek() and ftell() use long and thus don't support
33 // large files on 32-bit systems (POSIX versions fseeko() and
34 // ftello() can support large files).
35 if (fseek(infile
, 0, SEEK_END
)) {
36 fprintf(stderr
, "Error seeking the file `%s': %s\n",
37 filename
, strerror(errno
));
41 const long file_size
= ftell(infile
);
43 // The decoder wants to start from the beginning of the .xz file.
46 // Initialize the decoder.
48 lzma_ret ret
= lzma_file_info_decoder(strm
, &i
, UINT64_MAX
,
52 // Initialization succeeded.
56 fprintf(stderr
, "Out of memory when initializing "
57 "the .xz file info decoder\n");
62 fprintf(stderr
, "Unknown error, possibly a bug\n");
66 // This example program reuses the same lzma_stream structure
67 // for multiple files, so we need to reset this when starting
71 // Buffer for input data.
72 uint8_t inbuf
[BUFSIZ
];
74 // Pass data to the decoder and seek when needed.
76 if (strm
->avail_in
== 0) {
77 strm
->next_in
= inbuf
;
78 strm
->avail_in
= fread(inbuf
, 1, sizeof(inbuf
),
83 "Error reading from `%s': %s\n",
84 filename
, strerror(errno
));
88 // We don't need to care about hitting the end of
89 // the file so no need to check for feof().
92 ret
= lzma_code(strm
, LZMA_RUN
);
98 case LZMA_SEEK_NEEDED
:
99 // The cast is safe because liblzma won't ask us to
100 // seek past the known size of the input file which
101 // did fit into a long.
103 // NOTE: Remember to change these to off_t if you
104 // switch fseeko() or lseek().
105 if (fseek(infile
, (long)(strm
->seek_pos
), SEEK_SET
)) {
106 fprintf(stderr
, "Error seeking the "
108 filename
, strerror(errno
));
112 // The old data in the inbuf is useless now. Set
113 // avail_in to zero so that we will read new input
114 // from the new file position on the next iteration
119 case LZMA_STREAM_END
:
120 // File information was successfully decoded.
121 // See <lzma/index.h> for functions that can be
122 // used on it. In this example we just print
123 // the uncompressed size (in bytes) of
124 // the .xz file followed by its file name.
125 printf("%10" PRIu64
" %s\n",
126 lzma_index_uncompressed_size(i
),
129 // Free the memory of the lzma_index structure.
130 lzma_index_end(i
, NULL
);
134 case LZMA_FORMAT_ERROR
:
135 // .xz magic bytes weren't found.
136 fprintf(stderr
, "The file `%s' is not "
137 "in the .xz format\n", filename
);
140 case LZMA_OPTIONS_ERROR
:
141 fprintf(stderr
, "The file `%s' has .xz headers that "
142 "are not supported by this liblzma "
143 "version\n", filename
);
146 case LZMA_DATA_ERROR
:
147 fprintf(stderr
, "The file `%s' is corrupt\n",
152 fprintf(stderr
, "Memory allocation failed when "
153 "decoding the file `%s'\n", filename
);
156 // LZMA_MEMLIMIT_ERROR shouldn't happen because we used
157 // UINT64_MAX as the limit.
159 // LZMA_BUF_ERROR shouldn't happen because we always provide
160 // new input when the input buffer is empty. The decoder
161 // knows the input file size and thus won't try to read past
162 // the end of the file.
163 case LZMA_MEMLIMIT_ERROR
:
165 case LZMA_PROG_ERROR
:
167 fprintf(stderr
, "Unknown error, possibly a bug\n");
172 // This line is never reached.
177 main(int argc
, char **argv
)
180 lzma_stream strm
= LZMA_STREAM_INIT
;
182 for (int i
= 1; i
< argc
; ++i
) {
183 FILE *infile
= fopen(argv
[i
], "rb");
185 if (infile
== NULL
) {
186 fprintf(stderr
, "Cannot open the file `%s': %s\n",
187 argv
[i
], strerror(errno
));
191 success
&= print_file_size(&strm
, infile
, argv
[i
]);
193 (void)fclose(infile
);
198 // Close stdout to catch possible write errors that can occur
199 // when pending data is flushed from the stdio buffers.
200 if (fclose(stdout
)) {
201 fprintf(stderr
, "Write error: %s\n", strerror(errno
));
205 return success
? EXIT_SUCCESS
: EXIT_FAILURE
;