2 * Copyright (c) 2003 Guido Draheim <guidod@gmx.de>
3 * Use freely under the restrictions of the ZLIB license.
5 * This file is used as an example to clarify zzip api usage.
13 #include <zzip/__mkdir.h>
14 #include <zzip/__string.h>
15 #include <zzip/__fnmatch.h>
16 #include <zzip/__debug.h>
17 #include "unzzipcat-zip.h"
18 #include "unzzip-states.h"
20 #ifdef ZZIP_HAVE_UNISTD_H
27 /* Functions in unzzip.c: */
28 extern int exitcode(int);
29 extern FILE* create_fopen(char*, char*, int);
31 static void unzzip_cat_file(ZZIP_DIR* disk, char* name, FILE* out)
33 ZZIP_FILE* file = zzip_file_open (disk, name, 0);
36 char buffer[1024]; int len;
37 while ((len = zzip_file_read (file, buffer, 1024)))
39 fwrite (buffer, 1, len, out);
42 zzip_file_close (file);
46 static int unzzip_cat (int argc, char ** argv, int extract)
55 printf (__FILE__ " version " ZZIP_PACKAGE_NAME " " ZZIP_PACKAGE_VERSION "\n");
56 return EXIT_OK; /* better provide an archive argument */
59 disk = zzip_dir_open (argv[1], &error);
61 fprintf(stderr, "%s: %s\n", argv[1], zzip_strerror(error));
62 return exitcode(error);
68 while(zzip_dir_read(disk, &entry))
70 char* name = entry.d_name;
72 if (extract) out = create_fopen(name, "wb", 1);
74 DBG3("fopen' %s : %s", name, strerror(errno));
75 if (errno != EISDIR) done = EXIT_ERRORS;
78 unzzip_cat_file (disk, name, out);
79 if (extract) fclose(out);
83 { /* list only the matching entries - in order of zip directory */
85 while(zzip_dir_read(disk, &entry))
87 char* name = entry.d_name;
88 for (argn=1; argn < argc; argn++)
90 if (! _zzip_fnmatch (argv[argn], name,
91 _zzip_FNM_NOESCAPE|_zzip_FNM_PATHNAME|_zzip_FNM_PERIOD))
94 if (extract) out = create_fopen(name, "wb", 1);
96 DBG3("fopen. %s : %s", name, strerror(errno));
97 if (errno != EISDIR) done = EXIT_ERRORS;
100 unzzip_cat_file (disk, name, out);
101 if (extract) fclose(out);
102 break; /* match loop */
107 zzip_dir_close(disk);
111 int unzzip_print (int argc, char ** argv)
113 return unzzip_cat(argc, argv, 0);
116 int unzzip_extract (int argc, char ** argv)
118 return unzzip_cat(argc, argv, 1);
123 * c-file-style: "stroustrup"