1 /*
2  * Author:
3  *	Guido Draheim <guidod@gmx.de>
4  *	Tomi Ollila <Tomi.Ollila@iki.fi>
5  *
6  *	Copyright (c) 1999,2000,2001,2002,2003 Guido Draheim
7  * 	    All rights reserved,
8  *	    use under the restrictions of the
9  *	    Lesser GNU General Public License
10  *          or alternatively the restrictions
11  *          of the Mozilla Public License 1.1
12  */
13 
14 #include <stdio.h>
15 #include <string.h>
16 
17 #include <zzip/lib.h>
18 
19 #if defined ZZIP_HAVE_WINDOWS_H
20 #define WIN32_LEAN_AND_MEAN
21 #include <windows.h>
22 #define sleep Sleep
23 #endif
24 
25 #ifdef ZZIP_HAVE_UNISTD_H
26 #include <unistd.h> /* sleep */
27 #endif
28 
29 #ifndef O_BINARY
30 #define O_BINARY 0
31 #endif
32 
33 #if __GNUC__+0 >= 3 && __GNUC_MINOR__+0 >= 3
34 # ifdef DEBUG
35 # warning suppress a warning where the compiler should have optimized instead.
36 # endif
37 #define I_(_T,_L,_R) do { _T _l = (_T) _L; \
38                           _l _R; _L = (typeof(_L)) _l; } while(0)
39 #else
40 #define I_(_T,_L,_R) _L _R
41 #endif
42 
main(int argc,char ** argv)43 int main(int argc, char ** argv)
44 {
45     ZZIP_DIR * dir;
46     const char * name = "test.zip";
47     zzip_error_t rv;
48     int i;
49     int quick = 0;
50 
51     if (argc > 1 && argv[1] != NULL)
52     {
53 	if (! strcmp (argv[1], "--help")) {
54 	    printf ("zziptest [testfile]\n - selftest defaults to 'test.zip'");
55 	    return 0;
56 	}else if (! strcmp (argv[1], "--version")) {
57 	    printf (__FILE__ " version " ZZIP_PACKAGE_NAME " " ZZIP_PACKAGE_VERSION "\n");
58 	    return 0;
59 	}else if (! strcmp (argv[1], "--quick")) {
60 	    quick = 1;
61 	    name = argv[2];
62 	    argv++; argc--;
63 	    argv++; argc--;
64 	}else{
65 	    name = argv[1];
66 	    argv++; argc--;
67 	}
68     }
69 
70     printf("Opening zip file `%s'... ", name);
71     { 	int fd = open (name, O_RDONLY|O_BINARY);
72         if (fd == -1) { perror ("could not open input file"); return 0; }
73         if (! (dir = zzip_dir_fdopen(fd, &rv)))
74         {
75             printf("error %d.\n", rv);
76             return 0;
77         }
78     } printf("OK.\n");
79 
80 #if 1
81     printf("{check...\n");
82     { struct zzip_dir_hdr * hdr = dir->hdr0;
83 
84         if (hdr == NULL)
85           { printf ("could not find first header in dir_hdr"); }
86         else
87         {
88             while (1)
89             {
90                 printf("\ncompression method: %d", hdr->d_compr);
91                 if (hdr->d_compr == 0) printf(" (stored)");
92                 else if (hdr->d_compr == 8) printf(" (deflated)");
93                 else printf(" (unknown)");
94                 printf("\ncrc32: %x\n", hdr->d_crc32);
95                 printf("compressed size: %d\n", hdr->d_csize);
96                 printf("uncompressed size: %d\n", hdr->d_usize);
97                 printf("offset of file in archive: %d\n", hdr->d_off);
98                 printf("filename: %s\n\n", hdr->d_name);
99 
100                 if (hdr->d_reclen == 0) break;
101                 I_(char *, hdr, += hdr->d_reclen);
102                 if (! quick) sleep(1);
103             }
104         }
105     } printf ("\n}\n");
106 #endif
107 #if 1
108     { 	printf("{contents...\n");
109         for (i = 0; i < 2; i++)
110         {
111             ZZIP_DIRENT* d;
112 
113             while((d=zzip_readdir(dir)))
114             {
115                 printf(" name \"%s\", compr %d, size %d, ratio %2d\n",
116                     d->d_name, d->d_compr, d->st_size,
117                     100 - (d->d_csize|1)*100/(d->st_size|1));
118             }
119             printf(" %d. time ---------------\n", i + 1);
120             zzip_rewinddir(dir);
121         }
122         printf("}...OK\n");
123     }
124 #endif
125 
126     {   ZZIP_FILE * fp;
127         char buf[17];
128         const char * name = argv[1]? argv[1]: "README";
129 
130 
131         printf("Opening file `%s' in zip archive... ", name);
132         fp = zzip_file_open(dir, (char *)name, ZZIP_CASEINSENSITIVE);
133 
134         if (! fp)
135           { printf("error %d: %s\n", zzip_error(dir), zzip_strerror_of(dir)); }
136         else
137         {
138             printf("OK.\n");
139             printf("Contents of the file:\n");
140 
141             while (0 < (i = zzip_file_read(fp, buf, 16)))
142             {
143                 buf[i] = '\0';
144                 /*printf("\n*** read %d ***\n", i); fflush(stdout); */
145                 printf("%s", buf);
146                 /*write(1, buf, i);*/ /* Windows does not have write !!! */
147             }
148             if (i < 0) printf("error %d\n", zzip_error(dir));
149         }
150     }
151 
152     return 0;
153 }
154 
155 /*
156  * Local variables:
157  * c-file-style: "stroustrup"
158  * End:
159  */
160