zipios++  2.0.2
Zipios++ – a small C++ library that provides 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 Made to Order Software Corporation
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 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21 
29 #include "zipios++/zipfile.hpp"
30 
31 #include <cstring>
32 
33 #include <stdlib.h>
34 
35 
41 namespace
42 {
43 
49 char *g_progname;
50 
51 
57 void usage()
58 {
59  std::cout << "Usage: " << g_progname << " [-opt] [file]" << std::endl;
60  std::cout << "Where -opt is one or more of:" << std::endl;
61  std::cout << " --count count the number of files in a .zip archive" << std::endl;
62  std::cout << " --count-directories count the number of files in a .zip archive" << std::endl;
63  std::cout << " --count-files count the number of files in a .zip archive" << std::endl;
64  std::cout << " --help show this help screen" << std::endl;
65  std::cout << " --version print the library version and exit" << std::endl;
66  std::cout << " --version-tool print the tool version and exit" << std::endl;
67  exit(1);
68 }
69 
70 
76 enum class func_t
77 {
83  UNDEFINED,
84 
90  COUNT,
91 
98 
106 };
107 
108 } // no name namespace
109 
110 
111 int main(int argc, char *argv[])
112 {
113  // define program name
114  g_progname = argv[0];
115  char *e(strrchr(g_progname, '/'));
116  if(e)
117  {
118  g_progname = e + 1;
119  }
120  e = strrchr(g_progname, '\\');
121  if(e)
122  {
123  g_progname = e + 1;
124  }
125 
126  // check the various command line options
127  std::vector<std::string> files;
128  func_t function(func_t::UNDEFINED);
129  for(int i(1); i < argc; ++i)
130  {
131  if(argv[i][0] == '-')
132  {
133  if(strcmp(argv[i], "--help") == 0)
134  {
135  usage();
136  }
137  if(strcmp(argv[i], "--version") == 0)
138  {
139  // version of the .so library
140  std::cout << zipios::getVersion() << std::endl;
141  exit(0);
142  }
143  if(strcmp(argv[i], "--version-tool") == 0)
144  {
145  // version of this tool (compiled with this version)
146  // it should be the same as the --version
147  std::cout << ZIPIOS_VERSION_STRING << std::endl;
148  exit(0);
149  }
150  if(strcmp(argv[i], "--count") == 0)
151  {
152  function = func_t::COUNT;
153  }
154  else if(strcmp(argv[i], "--count-directories") == 0)
155  {
156  function = func_t::COUNT_DIRECTORIES;
157  }
158  else if(strcmp(argv[i], "--count-files") == 0)
159  {
160  function = func_t::COUNT_FILES;
161  }
162  }
163  else
164  {
165  files.push_back(argv[i]);
166  }
167  }
168 
169  switch(function)
170  {
171  case func_t::COUNT:
172  for(auto it(files.begin()); it != files.end(); ++it)
173  {
174  zipios::ZipFile zf(*it);
175  if(files.size() > 1)
176  {
177  // write filename in case there is more than one file
178  std::cout << *it << ": ";
179  }
180  std::cout << zf.entries().size() << std::endl;
181  }
182  break;
183 
184  case func_t::COUNT_DIRECTORIES:
185  for(auto it(files.begin()); it != files.end(); ++it)
186  {
187  zipios::ZipFile zf(*it);
188  if(files.size() > 1)
189  {
190  // write filename in case there is more than one file
191  std::cout << *it << ": ";
192  }
193  int count(0);
194  zipios::FileEntry::vector_t entries(zf.entries());
195  for(auto entry(entries.begin()); entry != entries.end(); ++entry)
196  {
197  if((*entry)->isDirectory())
198  {
199  ++count;
200  }
201  }
202  std::cout << count << std::endl;
203  }
204  break;
205 
206  case func_t::COUNT_FILES:
207  for(auto it(files.begin()); it != files.end(); ++it)
208  {
209  zipios::ZipFile zf(*it);
210  if(files.size() > 1)
211  {
212  // write filename in case there is more than one file
213  std::cout << *it << ": ";
214  }
215  int count(0);
216  zipios::FileEntry::vector_t entries(zf.entries());
217  for(auto entry(entries.begin()); entry != entries.end(); ++entry)
218  {
219  if(!(*entry)->isDirectory())
220  {
221  ++count;
222  }
223  }
224  std::cout << count << std::endl;
225  }
226  break;
227 
228  default:
229  std::cerr << g_progname << ":error: undefined function." << std::endl;
230  usage();
231  break;
232 
233  }
234 
235  return 0;
236 }
237 
238 
239 // Local Variables:
240 // mode: cpp
241 // indent-tabs-mode: nil
242 // c-basic-offset: 4
243 // tab-width: 4
244 // End:
245 
246 // vim: ts=4 sw=4 et
The ZipFile class represents a collection of files.
Definition: zipfile.hpp:47
Count the number of files in a Zip archive.
int main(int argc, char *argv[])
Definition: zipios.cpp:111
char const * getVersion()
virtual FileEntry::vector_t entries() const
Retrieve the array of entries.
char * g_progname
Name of the program.
Definition: zipios.cpp:49
func_t
The function to apply.
Definition: zipios.cpp:76
Define the zipios::ZipFile class.
Count the number of regular files in a Zip archive.
void usage()
Usage of the zipios tool.
Definition: zipios.cpp:57
Count the number of directories in a Zip archive.
#define ZIPIOS_VERSION_STRING
std::vector< pointer_t > vector_t
Definition: fileentry.hpp:78