zipios 2.3.4
Zipios -- a small C++ library providing easy access to .zip files.
zipios.cpp
Go to the documentation of this file.
1/*
2 Zipios -- a small C++ library that provides easy access to .zip files.
3
4 Copyright (C) 2000-2007 Thomas Sondergaard
5 Copyright (c) 2015-2022 Made to Order Software Corp. All Rights Reserved
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20*/
21
33#include "zipios/zipfile.hpp"
35
36#include <cstring>
37
38#include <stdlib.h>
39
40
46namespace
47{
48
55
56
62void usage()
63{
64 std::cout << "Usage: " << g_progname << " [-opt] [file]" << std::endl;
65 std::cout << "Where -opt is one or more of:" << std::endl;
66 std::cout << " --count count the number of files in a .zip archive" << std::endl;
67 std::cout << " --count-directories count the number of files in a .zip archive" << std::endl;
68 std::cout << " --count-files count the number of files in a .zip archive" << std::endl;
69 std::cout << " --help show this help screen" << std::endl;
70 std::cout << " --libzipios-version print the library version and exit" << std::endl;
71 std::cout << " --version print this tool's version and exit" << std::endl;
72 exit(1);
73}
74
75
81enum class func_t
82{
89
95 COUNT,
96
103
111};
112
113} // no name namespace
114
115
116int main(int argc, char *argv[])
117{
118 // define program name
119 {
120 g_progname = argv[0];
121 char *e(strrchr(g_progname, '/'));
122 if(e)
123 {
124 g_progname = e + 1;
125 }
126 e = strrchr(g_progname, '\\');
127 if(e)
128 {
129 g_progname = e + 1;
130 }
131 }
132
133 try
134 {
135 // check the various command line options
136 std::vector<std::string> files;
137 func_t function(func_t::UNDEFINED);
138 for(int i(1); i < argc; ++i)
139 {
140 if(argv[i][0] == '-')
141 {
142 if(strcmp(argv[i], "--help") == 0)
143 {
144 usage();
145 }
146 if(strcmp(argv[i], "--libzipios-version") == 0)
147 {
148 // version of the .so library
149 std::cout << zipios::getVersion() << std::endl;
150 exit(0);
151 }
152 if(strcmp(argv[i], "--version") == 0)
153 {
154 // version of this tool (compiled with this version)
155 // it should be the same as the --version
156 std::cout << ZIPIOS_VERSION_STRING << std::endl;
157 exit(0);
158 }
159 if(strcmp(argv[i], "--count") == 0)
160 {
161 function = func_t::COUNT;
162 }
163 else if(strcmp(argv[i], "--count-directories") == 0)
164 {
165 function = func_t::COUNT_DIRECTORIES;
166 }
167 else if(strcmp(argv[i], "--count-files") == 0)
168 {
169 function = func_t::COUNT_FILES;
170 }
171 }
172 else
173 {
174 files.push_back(argv[i]);
175 }
176 }
177
178 switch(function)
179 {
180 case func_t::COUNT:
181 for(auto it(files.begin()); it != files.end(); ++it)
182 {
183 if(files.size() > 1)
184 {
185 // write filename in case there is more than one file
186 std::cout << *it << ": ";
187 }
188 zipios::ZipFile zf(*it);
189 std::cout << zf.entries().size() << std::endl;
190 }
191 break;
192
194 for(auto it(files.begin()); it != files.end(); ++it)
195 {
196 if(files.size() > 1)
197 {
198 // write filename in case there is more than one file
199 std::cout << *it << ": ";
200 }
201 zipios::ZipFile zf(*it);
202 int count(0);
204 for(auto entry(entries.begin()); entry != entries.end(); ++entry)
205 {
206 if((*entry)->isDirectory())
207 {
208 ++count;
209 }
210 }
211 std::cout << count << std::endl;
212 }
213 break;
214
216 for(auto it(files.begin()); it != files.end(); ++it)
217 {
218 if(files.size() > 1)
219 {
220 // write filename in case there is more than one file
221 std::cout << *it << ": ";
222 }
223 zipios::ZipFile zf(*it);
224 int count(0);
226 for(auto entry(entries.begin()); entry != entries.end(); ++entry)
227 {
228 if(!(*entry)->isDirectory())
229 {
230 ++count;
231 }
232 }
233 std::cout << count << std::endl;
234 }
235 break;
236
237 default:
238 std::cerr << g_progname << ":error: undefined function." << std::endl;
239 usage();
240 break;
241
242 }
243 }
244 catch(zipios::Exception const & e)
245 {
246 std::cerr << g_progname << ":error: an exception occurred: "
247 << e.what() << std::endl;
248 }
249
250 return 0;
251}
252
253
254// Local Variables:
255// mode: cpp
256// indent-tabs-mode: nil
257// c-basic-offset: 4
258// tab-width: 4
259// End:
260
261// vim: ts=4 sw=4 et
Base exception of the zipios environment.
virtual FileEntry::vector_t entries() const
Retrieve the array of entries.
std::vector< pointer_t > vector_t
Definition fileentry.hpp:79
The ZipFile class represents a collection of files.
Definition zipfile.hpp:48
void usage()
Usage of the zipios tool.
Definition zipios.cpp:62
func_t
The function to apply.
Definition zipios.cpp:82
@ COUNT
Count the number of files in a Zip archive.
@ COUNT_DIRECTORIES
Count the number of directories in a Zip archive.
@ COUNT_FILES
Count the number of regular files in a Zip archive.
char * g_progname
Name of the program.
Definition zipios.cpp:54
char const * getVersion()
Define the zipios::ZipFile class.
#define ZIPIOS_VERSION_STRING
int main(int argc, char *argv[])
Definition zipios.cpp:116
Various exceptions used throughout the Zipios library, all based on zipios::Exception.