zipios 2.3.4
Zipios -- a small C++ library providing 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
31#include <zipios/zipfile.hpp>
32
33#include <cstring>
34#include <iostream>
35#include <fstream>
36
37
38// static variables
39namespace
40{
41
43
44
45void usage()
46{
47 std::cout << "Usage: " << g_progname << " <output>[.zip] <input-dir>" << std::endl;
48 std::cout << "This tool creates a zip file from a directory or a file." << std::endl;
49 std::cout << "This is a way to exercise the library." << std::endl;
50 exit(1);
51}
52
53} // no name namespace
54
55
56
57
58int main(int argc, char *argv[])
59{
60 g_progname = argv[0];
61 char *e(strrchr(g_progname, '/'));
62 if(e)
63 {
64 g_progname = e + 1;
65 }
66 e = strrchr(g_progname, '\\');
67 if(e)
68 {
69 g_progname = e + 1;
70 }
71
72 int limit(256);
74 std::string in;
75 std::string out;
76 for(int i(1); i < argc; ++i)
77 {
78 if(strcmp(argv[i], "-h") == 0
79 || strcmp(argv[i], "--help") == 0)
80 {
81 usage();
82 }
83 if(strcmp(argv[i], "-V") == 0
84 || strcmp(argv[i], "--version") == 0)
85 {
86 std::cout << ZIPIOS_VERSION_STRING << std::endl;
87 exit(0);
88 }
89 if(strcmp(argv[i], "--level") == 0)
90 {
91 ++i;
92 if(i >= argc)
93 {
94 std::cerr << "error: the --level option must be followed by a level.";
95 return 1;
96 }
97 if(strcmp(argv[i], "default") == 0)
98 {
100 }
101 else if(strcmp(argv[i], "smallest") == 0)
102 {
104 }
105 else if(strcmp(argv[i], "fastest") == 0)
106 {
108 }
109 else if(strcmp(argv[i], "none") == 0)
110 {
112 }
113 else
114 {
115 level = static_cast<zipios::FileEntry::CompressionLevel>(std::atoi(argv[i]));
118 {
119 std::cerr
120 << "error: the --level parameter expects one of"
121 " \"default\", \"smallest\", \"fastest\", \"none\""
122 " or a number between "
124 << " and "
126 << ".\n";
127 return 1;
128 }
129 }
130 }
131 else if(strcmp(argv[i], "--limit") == 0)
132 {
133 ++i;
134 if(i >= argc)
135 {
136 std::cerr << "error: the --limit option must be followed by a limit.";
137 return 1;
138 }
139 if(strcmp(argv[i], "default") == 0)
140 {
141 limit = 256;
142 }
143 else
144 {
145 limit = std::atoi(argv[i]);
146 }
147 }
148 else if(out.empty())
149 {
150 out = argv[i];
151 }
152 else if(in.empty())
153 {
154 in = argv[i];
155 }
156 else
157 {
158 std::cerr << "error: unknown command line option \""
159 << argv[i]
160 << "\". Try --help for additional information.";
161 return 1;
162 }
163 }
164
165 if(out.empty())
166 {
167 std::cerr << "error: missing input directory name on command line.\n";
168 return 1;
169 }
170 if(in.empty())
171 {
172 in = out;
173 }
174
175 zipios::DirectoryCollection collection(in);
176
177 // at this time, we do not have support for any other method
178 // so no need for a command line option
179 //
181 {
182 // ignore the method if the compression is set to "none"
183 //
184 collection.setMethod(
185 limit
188 collection.setLevel(
189 limit
192 }
193 else
194 {
195 collection.setMethod(
196 limit
199 collection.setLevel(
200 limit
202 , level);
203 }
204
205 std::string zipname(out);
206 if(zipname.find(".zip", zipname.length() - 4) == std::string::npos)
207 {
208 zipname += ".zip";
209 }
210 std::ofstream output(zipname, std::ios_base::binary);
211
213
214 return 0;
215}
216
217
218// Local Variables:
219// mode: cpp
220// indent-tabs-mode: nil
221// c-basic-offset: 4
222// tab-width: 4
223// End:
224
225// vim: ts=4 sw=4 et
A collection generated from reading a directory.
void setMethod(size_t limit, StorageMethod small_storage_method, StorageMethod large_storage_method)
Change the storage method to the specified value.
void setLevel(size_t limit, FileEntry::CompressionLevel small_compression_level, FileEntry::CompressionLevel large_compression_level)
Change the compression level to the specified value.
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
static CompressionLevel const COMPRESSION_LEVEL_MAXIMUM
Definition fileentry.hpp:92
static CompressionLevel const COMPRESSION_LEVEL_DEFAULT
Definition fileentry.hpp:87
static CompressionLevel const COMPRESSION_LEVEL_NONE
Definition fileentry.hpp:90
static CompressionLevel const COMPRESSION_LEVEL_FASTEST
Definition fileentry.hpp:89
static CompressionLevel const COMPRESSION_LEVEL_SMALLEST
Definition fileentry.hpp:88
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
Define the zipios::DirectoryCollection class.
int main(int argc, char *argv[])
Definition zipdir.cpp:58
Define the zipios::ZipFile class.
#define ZIPIOS_VERSION_STRING