1 /* zpipe.c: example of proper use of zlib's inflate() and deflate()
2 Not copyrighted -- provided to the public domain
3 Version 1.2 9 November 2004 Mark Adler */
6 1.0 30 Oct 2004 First version
7 1.1 8 Nov 2004 Add void casting for unused return values
8 Use switch statement for inflate() return values
9 1.2 9 Nov 2004 Add assertions to document zlib guarantees
10 1.3 6 Apr 2005 Remove incorrect assertion in inf()
20 /* Compress from file source to file dest until EOF on source.
21 def() returns Z_OK on success, Z_MEM_ERROR if memory could not be
22 allocated for processing, Z_STREAM_ERROR if an invalid compression
23 level is supplied, Z_VERSION_ERROR if the version of zlib.h and the
24 version of the library linked do not match, or Z_ERRNO if there is
25 an error reading or writing the files. */
26 int def(FILE *source
, FILE *dest
, int level
)
34 /* allocate deflate state */
38 ret
= deflateInit(&strm
, level
);
42 /* compress until end of file */
44 strm
.avail_in
= fread(in
, 1, CHUNK
, source
);
46 (void)deflateEnd(&strm
);
49 flush
= feof(source
) ? Z_FINISH
: Z_NO_FLUSH
;
52 /* run deflate() on input until output buffer not full, finish
53 compression if all of source has been read in */
55 strm
.avail_out
= CHUNK
;
57 ret
= deflate(&strm
, flush
); /* no bad return value */
58 assert(ret
!= Z_STREAM_ERROR
); /* state not clobbered */
59 have
= CHUNK
- strm
.avail_out
;
60 if (fwrite(out
, 1, have
, dest
) != have
|| ferror(dest
)) {
61 (void)deflateEnd(&strm
);
64 } while (strm
.avail_out
== 0);
65 assert(strm
.avail_in
== 0); /* all input will be used */
67 /* done when last data in file processed */
68 } while (flush
!= Z_FINISH
);
69 assert(ret
== Z_STREAM_END
); /* stream will be complete */
71 /* clean up and return */
72 (void)deflateEnd(&strm
);
76 /* Decompress from file source to file dest until stream ends or EOF.
77 inf() returns Z_OK on success, Z_MEM_ERROR if memory could not be
78 allocated for processing, Z_DATA_ERROR if the deflate data is
79 invalid or incomplete, Z_VERSION_ERROR if the version of zlib.h and
80 the version of the library linked do not match, or Z_ERRNO if there
81 is an error reading or writing the files. */
82 int inf(FILE *source
, FILE *dest
)
90 /* allocate inflate state */
95 strm
.next_in
= Z_NULL
;
96 ret
= inflateInit(&strm
);
100 /* decompress until deflate stream ends or end of file */
102 strm
.avail_in
= fread(in
, 1, CHUNK
, source
);
103 if (ferror(source
)) {
104 (void)inflateEnd(&strm
);
107 if (strm
.avail_in
== 0)
111 /* run inflate() on input until output buffer not full */
113 strm
.avail_out
= CHUNK
;
115 ret
= inflate(&strm
, Z_NO_FLUSH
);
116 assert(ret
!= Z_STREAM_ERROR
); /* state not clobbered */
119 ret
= Z_DATA_ERROR
; /* and fall through */
122 (void)inflateEnd(&strm
);
125 have
= CHUNK
- strm
.avail_out
;
126 if (fwrite(out
, 1, have
, dest
) != have
|| ferror(dest
)) {
127 (void)inflateEnd(&strm
);
130 } while (strm
.avail_out
== 0);
132 /* done when inflate() says it's done */
133 } while (ret
!= Z_STREAM_END
);
135 /* clean up and return */
136 (void)inflateEnd(&strm
);
137 return ret
== Z_STREAM_END
? Z_OK
: Z_DATA_ERROR
;
140 /* report a zlib or i/o error */
143 fputs("zpipe: ", stderr
);
147 fputs("error reading stdin\n", stderr
);
149 fputs("error writing stdout\n", stderr
);
152 fputs("invalid compression level\n", stderr
);
155 fputs("invalid or incomplete deflate data\n", stderr
);
158 fputs("out of memory\n", stderr
);
160 case Z_VERSION_ERROR
:
161 fputs("zlib version mismatch!\n", stderr
);
165 /* compress or decompress from stdin to stdout */
166 int main(int argc
, char **argv
)
170 /* do compression if no arguments */
172 ret
= def(stdin
, stdout
, Z_DEFAULT_COMPRESSION
);
178 /* do decompression if -d specified */
179 else if (argc
== 2 && strcmp(argv
[1], "-d") == 0) {
180 ret
= inf(stdin
, stdout
);
186 /* otherwise, report usage */
188 fputs("zpipe usage: zpipe [-d] < source > dest\n", stderr
);