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