3 /* Trivial example of reading a gzip'ed file or gzip'ed standard input
4 * using stdio functions fread(), getc(), etc... fseek() is not supported.
5 * Modify according to your needs. You can easily construct the symmetric
8 * Usage: zread [file[.gz]]
9 * This programs assumes that gzip is somewhere in your path.
20 if (argc < 1 || argc > 2) {
21 fprintf(stderr, "usage: %s [file[.gz]]\n", argv[0]);
24 strcpy(cmd, "gzip -dc "); /* use "gzip -c" for zwrite */
26 strncat(cmd, argv[1], sizeof(cmd)-strlen(cmd));
28 infile = popen(cmd, "r"); /* use "w" for zwrite */
30 fprintf(stderr, "%s: popen('%s', 'r') failed\n", argv[0], cmd);
33 /* Read one byte using getc: */
41 /* Read the rest using fread: */
43 n = fread(buf, 1, BUFSIZ, infile);
45 fwrite(buf, 1, n, stdout);
47 if (pclose(infile) != 0) {
48 fprintf(stderr, "%s: pclose failed\n", argv[0]);
52 return 0; /* just to make compiler happy */