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 zzipfseeko api usage.
8 #include <zzip/fseeko.h>
12 #include <zzip/__mkdir.h>
13 #include <zzip/__string.h>
14 #include <zzip/__debug.h>
15 #include <zzip/__fnmatch.h>
16 #include "unzzipcat-zip.h"
17 #include "unzzip-states.h"
19 /* Functions in unzzip.c: */
20 extern int exitcode(int);
21 extern FILE* create_fopen(char*, char*, int);
23 static void unzzip_big_entry_fprint(ZZIP_ENTRY* entry, FILE* out)
25 ZZIP_ENTRY_FILE* file = zzip_entry_fopen (entry, 0);
28 char buffer[1024]; int len;
29 while ((len = zzip_entry_fread (buffer, 1024, 1, file)))
31 DBG2("entry read %i", len);
32 fwrite (buffer, len, 1, out);
34 DBG2("entry done %s", strerror(errno));
35 zzip_entry_fclose (file);
38 DBG2("could not open entry: %s", strerror(errno));
42 static void unzzip_cat_file(FILE* disk, char* name, FILE* out)
44 ZZIP_ENTRY_FILE* file = zzip_entry_ffile (disk, name);
47 char buffer[1024]; int len;
48 while ((len = zzip_entry_fread (buffer, 1024, 1, file)))
49 fwrite (buffer, len, 1, out);
51 zzip_entry_fclose (file);
56 static int unzzip_cat (int argc, char ** argv, int extract)
62 disk = fopen (argv[1], "rb");
65 return exitcode(errno);
69 { /* print directory list */
70 ZZIP_ENTRY* entry = zzip_entry_findfirst(disk);
71 for (; entry ; entry = zzip_entry_findnext(entry))
73 char* name = zzip_entry_strdup_name (entry);
79 if (extract) out = create_fopen(name, "wb", 1);
81 if (errno != EISDIR) done = EXIT_ERRORS;
84 unzzip_cat_file (disk, name, out);
85 if (extract) fclose(out);
91 if (argc == 3 && !extract)
92 { /* list from one spec */
93 ZZIP_ENTRY* entry = 0;
94 while ((entry = zzip_entry_findmatch(disk, argv[2], entry, 0, 0)))
96 unzzip_big_entry_fprint (entry, stdout);
101 for (argn=1; argn < argc; argn++)
102 { /* list only the matching entries - each in order of commandline */
103 ZZIP_ENTRY* entry = zzip_entry_findfirst(disk);
104 for (; entry ; entry = zzip_entry_findnext(entry))
106 char* name = zzip_entry_strdup_name (entry);
107 DBG3(".. check '%s' to zip '%s'", argv[argn], name);
108 if (! _zzip_fnmatch (argv[argn], name,
109 _zzip_FNM_NOESCAPE|_zzip_FNM_PATHNAME|_zzip_FNM_PERIOD))
112 if (extract) out = create_fopen(name, "wb", 1);
114 if (errno != EISDIR) done = EXIT_ERRORS;
117 unzzip_cat_file (disk, name, out);
118 if (extract) fclose(out);
119 break; /* match loop */
127 int unzzip_print (int argc, char ** argv)
129 return unzzip_cat(argc, argv, 0);
132 int unzzip_extract (int argc, char ** argv)
134 return unzzip_cat(argc, argv, 1);
139 * c-file-style: "stroustrup"