From: Guido Draheim Date: Wed, 13 Aug 2003 22:44:22 +0000 (+0000) Subject: This commit was generated by cvs2svn to compensate for changes in r85, which X-Git-Tag: v0.13.36~52 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=104d6ac923ae42d94359a49554fc30472c00d39c;p=zziplib This commit was generated by cvs2svn to compensate for changes in r85, which included commits to RCS files with non-trunk default branches. --- diff --git a/bins/unzzip.c b/bins/unzzip.c new file mode 100644 index 0000000..48710ee --- /dev/null +++ b/bins/unzzip.c @@ -0,0 +1,106 @@ +/* + * Copyright (c) 2003 Guido Draheim + * Use freely under the restrictions of the ZLIB license. + * + * This file is used as an example to clarify zzip api usage. + */ + +#include +#include +#include + +#ifdef ZZIP_HAVE_UNISTD_H +#include +#endif +#ifdef ZZIP_HAVE_IO_H +#include +#endif + +#ifndef O_BINARY +#define O_BINARY 0 +#endif + +static const char usage[] = +{ + "unzzip .. \n" + " - unzzip the files contained in a zip archive." +}; + +int +main (int argc, char ** argv) +{ + int argn; + int exitcode = 0; + zzip_error_t error; + + if (argc <= 1) + { + printf (usage); + exit(0); + } + + for (argn=1; argn < argc; argn++) + { + ZZIP_DIR * dir; + ZZIP_DIRENT d; + + dir = zzip_dir_open(argv[argn], &error); + if (! dir) + { + fprintf (stderr, "did not open %s: \n", argv[argn]); + fprintf (stderr, "%s: %s\n", argv[argn], zzip_strerror(error)); + exitcode++; + continue; + } + + if (argc > 2) printf ("%s: \n", argv[argn]); + + /* read each dir entry and show one line of info per file */ + while (zzip_dir_read (dir, &d)) + { + int output; + ZZIP_FILE* input = zzip_file_open (dir, d.d_name, O_RDONLY); + if (! input) + { + fprintf (stderr, "|did not open %s: \n", d.d_name); + fprintf (stderr, "|%s: %s\n", d.d_name, zzip_strerror_of(dir)); + continue; + } + + output = creat (d.d_name, 0664); + if (output == -1) + { + fprintf (stderr, "|output file %s: \n", d.d_name); + perror(d.d_name); + zzip_file_close (input); + continue; + } + + printf("%s\n", d.d_name); + + { + char buf[17]; zzip_ssize_t n; + /* read chunks of 16 bytes into buf */ + while (0 < (n = zzip_read (input, buf, 16))) + { + write (output, buf, n); + } + + if (n == -1) + perror (d.d_name); + } + close (output); + zzip_file_close (input); + } + + zzip_dir_close(dir); + } + + return exitcode; +} + +/* + * Local variables: + * c-file-style: "stroustrup" + * End: + */