1//
2// unzip.cpp
3//
4// This sample demonstrates the Decompress 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/Decompress.h"
20#include "Poco/Zip/ZipLocalFileHeader.h"
21#include "Poco/Zip/ZipArchive.h"
22#include "Poco/Path.h"
23#include "Poco/File.h"
24#include "Poco/Delegate.h"
25#include <iostream>
26#include <fstream>
27
28
29using Poco::Util::Application;
30using Poco::Util::Option;
31using Poco::Util::OptionSet;
32using Poco::Util::HelpFormatter;
33using Poco::Util::AbstractConfiguration;
34using Poco::Util::OptionCallback;
35using Poco::Zip::ZipLocalFileHeader;
36using Poco::AutoPtr;
37
38
39class DecompressHandler
40{
41public:
42 DecompressHandler()
43 {
44 }
45
46 ~DecompressHandler()
47 {
48 }
49
50 void onError(const void*, std::pair<const ZipLocalFileHeader, const std::string>& info)
51 {
52 Poco::Util::Application::instance().logger().error("ERR: " + info.second);
53 }
54
55 void onOk(const void*, std::pair<const ZipLocalFileHeader, const Poco::Path>& info)
56 {
57 Poco::Util::Application::instance().logger().information("OK: " + info.second.toString(Poco::Path::PATH_UNIX));
58 }
59};
60
61
62class UnzipApp: public Application
63 /// This sample demonstrates some of the features of the Util::Application class,
64 /// such as configuration file handling and command line arguments processing.
65 ///
66 /// Try zip --help (on Unix platforms) or zip /help (elsewhere) for
67 /// more information.
68{
69public:
70 UnzipApp(): _helpRequested(false)
71 {
72 }
73
74protected:
75 void initialize(Application& self)
76 {
77 loadConfiguration(); // load default configuration files, if present
78 Application::initialize(self);
79 // add your own initialization code here
80 }
81
82 void uninitialize()
83 {
84 // add your own uninitialization code here
85 Application::uninitialize();
86 }
87
88 void reinitialize(Application& self)
89 {
90 Application::reinitialize(self);
91 // add your own reinitialization code here
92 }
93
94 void defineOptions(OptionSet& options)
95 {
96 Application::defineOptions(options);
97
98 options.addOption(
99 Option("help", "h", "display help information on command line arguments")
100 .required(false)
101 .repeatable(false)
102 .callback(OptionCallback<UnzipApp>(this, &UnzipApp::handleHelp)));
103
104 options.addOption(
105 Option("file", "f", "specifies the input zip file")
106 .required(true)
107 .repeatable(false)
108 .argument("filename")
109 .callback(OptionCallback<UnzipApp>(this, &UnzipApp::handleFile)));
110 }
111
112 void handleHelp(const std::string& name, const std::string& value)
113 {
114 _helpRequested = true;
115 displayHelp();
116 stopOptionsProcessing();
117 }
118
119 void handleFile(const std::string& name, const std::string& value)
120 {
121 _zipFile = value;
122 }
123
124 void displayHelp()
125 {
126 HelpFormatter helpFormatter(options());
127 helpFormatter.setCommand(commandName());
128 helpFormatter.setUsage("OPTIONS outdir");
129 helpFormatter.setHeader("A application that demonstrates usage of Poco::Zip::Decompress class.");
130 helpFormatter.format(std::cout);
131 }
132
133
134 int main(const std::vector<std::string>& args)
135 {
136 if (!_helpRequested)
137 {
138 Poco::Path outputDir;
139 if (!args.empty())
140 outputDir.parseDirectory(args[0]);
141
142 std::ifstream in(_zipFile.c_str(), std::ios::binary);
143 Poco::Zip::Decompress c(in, outputDir);
144 DecompressHandler handler;
145 c.EError += Poco::Delegate<DecompressHandler, std::pair<const ZipLocalFileHeader, const std::string> >(&handler, &DecompressHandler::onError);
146 c.EOk +=Poco::Delegate<DecompressHandler, std::pair<const ZipLocalFileHeader, const Poco::Path> >(&handler, &DecompressHandler::onOk);
147 c.decompressAllFiles();
148 c.EError -= Poco::Delegate<DecompressHandler, std::pair<const ZipLocalFileHeader, const std::string> >(&handler, &DecompressHandler::onError);
149 c.EOk -=Poco::Delegate<DecompressHandler, std::pair<const ZipLocalFileHeader, const Poco::Path> >(&handler, &DecompressHandler::onOk);
150 }
151 return Application::EXIT_OK;
152 }
153
154private:
155 bool _helpRequested;
156 std::string _zipFile;
157};
158
159
160POCO_APP_MAIN(UnzipApp)
161