forked from pocoproject/poco
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzip.cpp
More file actions
163 lines (143 loc) · 3.92 KB
/
Copy pathzip.cpp
File metadata and controls
163 lines (143 loc) · 3.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
//
// zip.cpp
//
// $Id: //poco/1.4/Zip/samples/zip/src/zip.cpp#1 $
//
// This sample demonstrates the Compress class.
//
// Copyright (c) 2007, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#include "Poco/Util/Application.h"
#include "Poco/Util/Option.h"
#include "Poco/Util/OptionSet.h"
#include "Poco/Util/HelpFormatter.h"
#include "Poco/Util/AbstractConfiguration.h"
#include "Poco/AutoPtr.h"
#include "Poco/Zip/Compress.h"
#include "Poco/Path.h"
#include "Poco/File.h"
#include "Poco/Delegate.h"
#include <iostream>
#include <fstream>
using Poco::Util::Application;
using Poco::Util::Option;
using Poco::Util::OptionSet;
using Poco::Util::HelpFormatter;
using Poco::Util::AbstractConfiguration;
using Poco::Util::OptionCallback;
using Poco::AutoPtr;
class ZipApp: public Application
/// This sample demonstrates some of the features of the Util::Application class,
/// such as configuration file handling and command line arguments processing.
///
/// Try zip --help (on Unix platforms) or zip /help (elsewhere) for
/// more information.
{
public:
ZipApp(): _helpRequested(false)
{
}
protected:
void initialize(Application& self)
{
loadConfiguration(); // load default configuration files, if present
Application::initialize(self);
// add your own initialization code here
}
void uninitialize()
{
// add your own uninitialization code here
Application::uninitialize();
}
void reinitialize(Application& self)
{
Application::reinitialize(self);
// add your own reinitialization code here
}
void defineOptions(OptionSet& options)
{
Application::defineOptions(options);
options.addOption(
Option("help", "h", "display help information on command line arguments")
.required(false)
.repeatable(false)
.callback(OptionCallback<ZipApp>(this, &ZipApp::handleHelp)));
options.addOption(
Option("file", "f", "specifies the output zip file")
.required(true)
.repeatable(false)
.argument("filename")
.callback(OptionCallback<ZipApp>(this, &ZipApp::handleFile)));
}
void handleHelp(const std::string& name, const std::string& value)
{
_helpRequested = true;
displayHelp();
stopOptionsProcessing();
}
void handleFile(const std::string& name, const std::string& value)
{
_outFile = value;
}
void displayHelp()
{
HelpFormatter helpFormatter(options());
helpFormatter.setCommand(commandName());
helpFormatter.setUsage("OPTIONS <set of input files>");
helpFormatter.setHeader("A application that demonstrates usage of Poco::Zip::Compress class.");
helpFormatter.format(std::cout);
}
void onDone(const void*, const Poco::Zip::ZipLocalFileHeader& hdr)
{
logger().information("DONE: " + hdr.getFileName());
}
int main(const std::vector<std::string>& args)
{
if (!_helpRequested)
{
if (args.empty())
{
logger().warning("No input files specified for compression");
}
else
{
std::ofstream out(_outFile.c_str(), std::ios::binary);
Poco::Zip::Compress c(out, true);
c.EDone += Poco::Delegate<ZipApp, const Poco::Zip::ZipLocalFileHeader>(this, &ZipApp::onDone);
for (std::vector<std::string>::const_iterator it = args.begin(); it != args.end(); ++it)
{
// no WILDCARD support!
Poco::File aFile(*it);
if (!aFile.exists())
{
logger().error("File not found: " + *it);
}
else
{
Poco::Path anEntry(*it);
if (aFile.isDirectory())
{
anEntry.makeDirectory();
c.addRecursive(anEntry, Poco::Zip::ZipCommon::CL_MAXIMUM, (args.size() != 1)); // add root if we have more than one entry
}
else
{
anEntry.makeFile();
c.addFile(anEntry, anEntry);
}
}
}
c.close();
c.EDone -= Poco::Delegate<ZipApp, const Poco::Zip::ZipLocalFileHeader>(this, &ZipApp::onDone);
}
}
return Application::EXIT_OK;
}
private:
bool _helpRequested;
std::string _outFile;
};
POCO_APP_MAIN(ZipApp)