]> granicus.if.org Git - zziplib/blob - bins/unzzip.c
This commit was generated by cvs2svn to compensate for changes in r85, which
[zziplib] / bins / unzzip.c
1 /*
2  *      Copyright (c) 2003 Guido Draheim <guidod@gmx.de>
3  *      Use freely under the restrictions of the ZLIB license.
4  *
5  *      This file is used as an example to clarify zzip api usage.
6  */
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <zzip/zzip.h>
11
12 #ifdef ZZIP_HAVE_UNISTD_H
13 #include <unistd.h>
14 #endif
15 #ifdef ZZIP_HAVE_IO_H
16 #include <io.h>
17 #endif
18
19 #ifndef O_BINARY
20 #define O_BINARY 0
21 #endif
22
23 static const char usage[] = 
24 {
25     "unzzip <dir>.. \n"
26     "  - unzzip the files contained in a zip archive."
27 };
28
29 int 
30 main (int argc, char ** argv)
31 {
32     int argn;
33     int exitcode = 0;
34     zzip_error_t error;
35
36     if (argc <= 1)
37     {
38         printf (usage);
39         exit(0);
40     }
41     
42     for (argn=1; argn < argc; argn++)
43     {
44         ZZIP_DIR * dir;
45         ZZIP_DIRENT d;
46   
47         dir = zzip_dir_open(argv[argn], &error);
48         if (! dir)
49         {
50             fprintf (stderr, "did not open %s: \n", argv[argn]);
51             fprintf (stderr, "%s: %s\n", argv[argn], zzip_strerror(error));
52             exitcode++;
53             continue;
54         }
55   
56         if (argc > 2) printf ("%s: \n", argv[argn]);
57
58         /* read each dir entry and show one line of info per file */
59         while (zzip_dir_read (dir, &d))
60         {
61             int output;
62             ZZIP_FILE* input = zzip_file_open (dir, d.d_name, O_RDONLY);
63             if (! input)
64             {
65                 fprintf (stderr, "|did not open %s: \n", d.d_name);
66                 fprintf (stderr, "|%s: %s\n", d.d_name, zzip_strerror_of(dir));
67                 continue;
68             }
69
70             output = creat (d.d_name, 0664);
71             if (output == -1)
72             {
73                 fprintf (stderr, "|output file %s: \n", d.d_name);
74                 perror(d.d_name);
75                 zzip_file_close (input);
76                 continue;
77             }
78
79             printf("%s\n", d.d_name);
80             
81             { 
82                 char buf[17]; zzip_ssize_t n;
83                 /* read chunks of 16 bytes into buf */
84                 while (0 < (n = zzip_read (input, buf, 16)))
85                 {
86                     write (output, buf, n);
87                 }
88
89                 if (n == -1)
90                     perror (d.d_name);
91             }
92             close (output);
93             zzip_file_close (input);
94         }
95
96         zzip_dir_close(dir);
97     }
98     
99     return exitcode;
100
101
102 /* 
103  * Local variables:
104  * c-file-style: "stroustrup"
105  * End:
106  */