zipios  2.3.2
Zipios -- a small C++ library that provides easy access to .zip files.
zipdir.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) 2015-2022 Made to Order Software Corp. All Rights Reserved
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Lesser General Public
8  License as published by the Free Software Foundation; either
9  version 2.1 of the License, or (at your option) any later version.
10 
11  This library is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  Lesser General Public License for more details.
15 
16  You should have received a copy of the GNU Lesser General Public
17  License along with this library; if not, write to the Free Software
18  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20 
32 #include <zipios/zipfile.hpp>
33 
34 #include <cstring>
35 #include <iostream>
36 #include <fstream>
37 
38 
39 // static variables
40 namespace
41 {
42 
43 char *g_progname;
44 
45 
46 void usage()
47 {
48  std::cout << "Usage: " << g_progname << " <output>[.zip] <input-dir>" << std::endl;
49  std::cout << "This tool creates a zip file from a directory or a file." << std::endl;
50  std::cout << "This is a way to exercise the library." << std::endl;
51  exit(1);
52 }
53 
54 } // no name namespace
55 
56 
57 
58 
59 int main(int argc, char *argv[])
60 {
61  g_progname = argv[0];
62  char *e(strrchr(g_progname, '/'));
63  if(e)
64  {
65  g_progname = e + 1;
66  }
67  e = strrchr(g_progname, '\\');
68  if(e)
69  {
70  g_progname = e + 1;
71  }
72 
73  int limit(256);
75  std::string in;
76  std::string out;
77  for(int i(1); i < argc; ++i)
78  {
79  if(strcmp(argv[i], "-h") == 0
80  || strcmp(argv[i], "--help") == 0)
81  {
82  usage();
83  }
84  if(strcmp(argv[i], "-V") == 0
85  || strcmp(argv[i], "--version") == 0)
86  {
87  std::cout << ZIPIOS_VERSION_STRING << std::endl;
88  exit(0);
89  }
90  if(strcmp(argv[i], "--level") == 0)
91  {
92  ++i;
93  if(i >= argc)
94  {
95  std::cerr << "error: the --level option must be followed by a level.";
96  return 1;
97  }
98  if(strcmp(argv[i], "default") == 0)
99  {
101  }
102  else if(strcmp(argv[i], "smallest") == 0)
103  {
105  }
106  else if(strcmp(argv[i], "fastest") == 0)
107  {
109  }
110  else if(strcmp(argv[i], "none") == 0)
111  {
113  }
114  else
115  {
116  level = static_cast<zipios::FileEntry::CompressionLevel>(std::atoi(argv[i]));
119  {
120  std::cerr
121  << "error: the --level parameter expects one of"
122  " \"default\", \"smallest\", \"fastest\", \"none\""
123  " or a number between "
125  << " and "
127  << ".\n";
128  return 1;
129  }
130  }
131  }
132  else if(strcmp(argv[i], "--limit") == 0)
133  {
134  ++i;
135  if(i >= argc)
136  {
137  std::cerr << "error: the --limit option must be followed by a limit.";
138  return 1;
139  }
140  if(strcmp(argv[i], "default") == 0)
141  {
142  limit = 256;
143  }
144  else
145  {
146  limit = std::atoi(argv[i]);
147  }
148  }
149  else if(out.empty())
150  {
151  out = argv[i];
152  }
153  else if(in.empty())
154  {
155  in = argv[i];
156  }
157  else
158  {
159  std::cerr << "error: unknown command line option \""
160  << argv[i]
161  << "\". Try --help for additional information.";
162  return 1;
163  }
164  }
165 
166  if(out.empty())
167  {
168  std::cerr << "error: missing input directory name on command line.\n";
169  return 1;
170  }
171  if(in.empty())
172  {
173  in = out;
174  }
175 
176  zipios::DirectoryCollection collection(in);
177 
178  // at this time, we do not have support for any other method
179  // so no need for a command line option
180  //
182  {
183  // ignore the method if the compression is set to "none"
184  //
185  collection.setMethod(
186  limit
189  collection.setLevel(
190  limit
193  }
194  else
195  {
196  collection.setMethod(
197  limit
200  collection.setLevel(
201  limit
203  , level);
204  }
205 
206  std::string zipname(out);
207  if(zipname.find(".zip", zipname.length() - 4) == std::string::npos)
208  {
209  zipname += ".zip";
210  }
211  std::ofstream output(zipname, std::ios_base::binary);
212 
213  zipios::ZipFile::saveCollectionToArchive(output, collection);
214 
215  return 0;
216 }
217 
218 
219 // Local Variables:
220 // mode: cpp
221 // indent-tabs-mode: nil
222 // c-basic-offset: 4
223 // tab-width: 4
224 // End:
225 
226 // vim: ts=4 sw=4 et
static void saveCollectionToArchive(std::ostream &os, FileCollection &collection, std::string const &zip_comment=std::string())
Create a Zip archive from the specified FileCollection.
Definition: zipfile.cpp:578
int main(int argc, char *argv[])
Definition: zipdir.cpp:59
Define the zipios::DirectoryCollection class.
A collection generated from reading a directory.
static CompressionLevel const COMPRESSION_LEVEL_NONE
Definition: fileentry.hpp:90
static CompressionLevel const COMPRESSION_LEVEL_DEFAULT
Definition: fileentry.hpp:87
int CompressionLevel
The compression level to be used to save an entry.
Definition: fileentry.hpp:85
static CompressionLevel const COMPRESSION_LEVEL_MINIMUM
Definition: fileentry.hpp:91
Define the zipios::ZipFile class.
static CompressionLevel const COMPRESSION_LEVEL_MAXIMUM
Definition: fileentry.hpp:92
void setLevel(size_t limit, FileEntry::CompressionLevel small_compression_level, FileEntry::CompressionLevel large_compression_level)
Change the compression level to the specified value.
static CompressionLevel const COMPRESSION_LEVEL_FASTEST
Definition: fileentry.hpp:89
static CompressionLevel const COMPRESSION_LEVEL_SMALLEST
Definition: fileentry.hpp:88
#define ZIPIOS_VERSION_STRING
void setMethod(size_t limit, StorageMethod small_storage_method, StorageMethod large_storage_method)
Change the storage method to the specified value.