Debian Codegraph @

libs/zziplib/zziplib-src/bins/unzzipshow.c

raw
1 /*
2 * Copyright (c) 2003 Guido Draheim <[email protected]>
3 * Use freely under the restrictions of the ZLIB license.
4 *
5 * This file is used as an example to clarify zzipfseeko api usage.
6 */
7
8 #include <zzip/fseeko.h>
9 #include <zzip/__fnmatch.h>
10 #include <stdlib.h>
11 #include <string.h>
12
13 static const char usage[] =
14 {
15 "unzzipshow <zip> [names].. \n"
16 " - unzzip data content of files contained in a zip archive.\n"
17 };
18
19 static void zzip_entry_fprint(ZZIP_ENTRY* entry, FILE* out)
20 {
21 ZZIP_ENTRY_FILE* file = zzip_entry_fopen (entry, 0);
22 if (file)
23 {
24 char buffer[1024]; int len;
25 while (0 < (len = zzip_entry_fread (buffer, 1024, 1, file)))
26 fwrite (buffer, len, 1, out);
27
28 zzip_entry_fclose (file);
29 }
30 }
31
32 static void zzip_cat_file(FILE* disk, char* name, FILE* out)
33 {
34 ZZIP_ENTRY_FILE* file = zzip_entry_ffile (disk, name);
35 if (file)
36 {
37 char buffer[1024]; int len;
38 while (0 < (len = zzip_entry_fread (buffer, 1024, 1, file)))
39 fwrite (buffer, len, 1, out);
40
41 zzip_entry_fclose (file);
42 }
43 }
44
45 int
46 main (int argc, char ** argv)
47 {
48 int argn;
49 FILE* disk;
50
51 if (argc <= 1 || ! strcmp (argv[1], "--help"))
52 {
53 printf (usage);
54 return 0;
55 }
56 if (! strcmp (argv[1], "--version"))
57 {
58 printf (__FILE__ " version " ZZIP_PACKAGE_NAME " " ZZIP_PACKAGE_VERSION "\n");
59 return 0;
60 }
61
62 disk = fopen (argv[1], "r");
63 if (! disk) {
64 perror(argv[1]);
65 return -1;
66 }
67
68 if (argc == 2)
69 { /* print directory list */
70 ZZIP_ENTRY* entry = zzip_entry_findfirst(disk);
71 if (! entry) puts("no first entry!\n");
72 for (; entry ; entry = zzip_entry_findnext(entry))
73 {
74 char* name = zzip_entry_strdup_name (entry);
75 printf ("%s\n", name);
76 free (name);
77 }
78 return 0;
79 }
80
81 if (argc == 3)
82 { /* list from one spec */
83 ZZIP_ENTRY* entry = 0;
84 while ((entry = zzip_entry_findmatch(disk, argv[2], entry, 0, 0)))
85 zzip_entry_fprint (entry, stdout);
86
87 return 0;
88 }
89
90 for (argn=1; argn < argc; argn++)
91 { /* list only the matching entries - each in order of commandline */
92 ZZIP_ENTRY* entry = zzip_entry_findfirst(disk);
93 for (; entry ; entry = zzip_entry_findnext(entry))
94 {
95 char* name = zzip_entry_strdup_name (entry);
96 if (! _zzip_fnmatch (argv[argn], name,
97 _zzip_FNM_NOESCAPE|_zzip_FNM_PATHNAME|_zzip_FNM_PERIOD))
98 zzip_cat_file (disk, name, stdout);
99 free (name);
100 }
101 }
102 return 0;
103 }
104
105 /*
106 * Local variables:
107 * c-file-style: "stroustrup"
108 * End:
109 */