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