beta-0.89.2
[luatex.git] / source / libs / zziplib / zziplib-0.13.62 / bins / zziptest.c
blob2fde5d27bcf43350cc0dd63af48930bbe87d38c3
1 /*
2 * Author:
3 * Guido Draheim <guidod@gmx.de>
4 * Tomi Ollila <Tomi.Ollila@iki.fi>
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
14 #include <stdio.h>
15 #include <string.h>
17 #include <zzip/lib.h>
19 #if defined ZZIP_HAVE_WINDOWS_H
20 #define WIN32_LEAN_AND_MEAN
21 #include <windows.h>
22 #define sleep Sleep
23 #endif
25 #ifdef ZZIP_HAVE_UNISTD_H
26 #include <unistd.h> /* sleep */
27 #endif
29 #ifndef O_BINARY
30 #define O_BINARY 0
31 #endif
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
43 int main(int argc, char ** argv)
45 ZZIP_DIR * dir;
46 const char * name = "test.zip";
47 zzip_error_t rv;
48 int i;
50 if (argc > 1 && argv[1] != NULL)
52 if (! strcmp (argv[1], "--help")) {
53 printf ("zziptest [testfile]\n - selftest defaults to 'test.zip'");
54 return 0;
55 }else if (! strcmp (argv[1], "--version")) {
56 printf (__FILE__" version "ZZIP_PACKAGE" "ZZIP_VERSION"\n");
57 return 0;
58 }else{
59 name = argv[1];
60 argv++; argc--;
64 printf("Opening zip file `%s'... ", name);
65 { int fd = open (name, O_RDONLY|O_BINARY);
66 if (fd == -1) { perror ("could not open input file"); return 0; }
67 if (! (dir = zzip_dir_fdopen(fd, &rv)))
69 printf("error %d.\n", rv);
70 return 0;
72 } printf("OK.\n");
74 #if 1
75 printf("{check...\n");
76 { struct zzip_dir_hdr * hdr = dir->hdr0;
78 if (hdr == NULL)
79 { printf ("could not find first header in dir_hdr"); }
80 else
82 while (1)
84 printf("\ncompression method: %d", hdr->d_compr);
85 if (hdr->d_compr == 0) printf(" (stored)");
86 else if (hdr->d_compr == 8) printf(" (deflated)");
87 else printf(" (unknown)");
88 printf("\ncrc32: %x\n", hdr->d_crc32);
89 printf("compressed size: %d\n", hdr->d_csize);
90 printf("uncompressed size: %d\n", hdr->d_usize);
91 printf("offset of file in archive: %d\n", hdr->d_off);
92 printf("filename: %s\n\n", hdr->d_name);
94 if (hdr->d_reclen == 0) break;
95 I_(char *, hdr, += hdr->d_reclen);
96 sleep(1);
99 } printf ("\n}\n");
100 #endif
101 #if 1
102 { printf("{contents...\n");
103 for (i = 0; i < 2; i++)
105 ZZIP_DIRENT* d;
107 while((d=zzip_readdir(dir)))
109 printf(" name \"%s\", compr %d, size %d, ratio %2d\n",
110 d->d_name, d->d_compr, d->st_size,
111 100 - (d->d_csize|1)*100/(d->st_size|1));
113 printf(" %d. time ---------------\n", i + 1);
114 zzip_rewinddir(dir);
116 printf("}...OK\n");
118 #endif
120 { ZZIP_FILE * fp;
121 char buf[17];
122 const char * name = argv[1]? argv[1]: "README";
125 printf("Opening file `%s' in zip archive... ", name);
126 fp = zzip_file_open(dir, (char *)name, ZZIP_CASEINSENSITIVE);
128 if (! fp)
129 { printf("error %d: %s\n", zzip_error(dir), zzip_strerror_of(dir)); }
130 else
132 printf("OK.\n");
133 printf("Contents of the file:\n");
135 while (0 < (i = zzip_file_read(fp, buf, 16)))
137 buf[i] = '\0';
138 /*printf("\n*** read %d ***\n", i); fflush(stdout); */
139 printf("%s", buf);
140 /*write(1, buf, i);*/ /* Windows does not have write !!! */
142 if (i < 0) printf("error %d\n", zzip_error(dir));
146 return 0;
150 * Local variables:
151 * c-file-style: "stroustrup"
152 * End: