1 | // |
2 | // un7zip.cpp |
3 | // |
4 | // This sample demonstrates the Archive class. |
5 | // |
6 | // Copyright (c) 2014, Applied Informatics Software Engineering GmbH. |
7 | // and Contributors. |
8 | // |
9 | // SPDX-License-Identifier: BSL-1.0 |
10 | // |
11 | |
12 | |
13 | #include "Poco/SevenZip/Archive.h" |
14 | #include "Poco/Util/Application.h" |
15 | #include "Poco/Util/Option.h" |
16 | #include "Poco/Util/OptionSet.h" |
17 | #include "Poco/Util/HelpFormatter.h" |
18 | #include "Poco/Util/AbstractConfiguration.h" |
19 | #include "Poco/DateTimeFormatter.h" |
20 | #include "Poco/DateTimeFormat.h" |
21 | #include "Poco/Delegate.h" |
22 | #include <iostream> |
23 | #include <iomanip> |
24 | |
25 | |
26 | using Poco::Util::Application; |
27 | using Poco::Util::Option; |
28 | using Poco::Util::OptionSet; |
29 | using Poco::Util::HelpFormatter; |
30 | using Poco::Util::AbstractConfiguration; |
31 | using Poco::Util::OptionCallback; |
32 | |
33 | |
34 | class Un7zipApp: public Application |
35 | { |
36 | public: |
37 | Un7zipApp(): |
38 | _helpRequested(false), |
39 | _list(false), |
40 | _extract(false) |
41 | { |
42 | } |
43 | |
44 | protected: |
45 | void initialize(Application& self) |
46 | { |
47 | loadConfiguration(); // load default configuration files, if present |
48 | Application::initialize(self); |
49 | // add your own initialization code here |
50 | } |
51 | |
52 | void uninitialize() |
53 | { |
54 | // add your own uninitialization code here |
55 | Application::uninitialize(); |
56 | } |
57 | |
58 | void reinitialize(Application& self) |
59 | { |
60 | Application::reinitialize(self); |
61 | // add your own reinitialization code here |
62 | } |
63 | |
64 | void defineOptions(OptionSet& options) |
65 | { |
66 | Application::defineOptions(options); |
67 | |
68 | options.addOption( |
69 | Option("help" , "h" , "Display help information on command line arguments." ) |
70 | .required(false) |
71 | .repeatable(false) |
72 | .callback(OptionCallback<Un7zipApp>(this, &Un7zipApp::handleHelp))); |
73 | |
74 | options.addOption( |
75 | Option("output" , "o" , "Specify base directory for extracted files." ) |
76 | .required(false) |
77 | .repeatable(false) |
78 | .argument("path" ) |
79 | .callback(OptionCallback<Un7zipApp>(this, &Un7zipApp::handleOutput))); |
80 | |
81 | options.addOption( |
82 | Option("list" , "l" , "List files and directories in archive." ) |
83 | .required(false) |
84 | .repeatable(false) |
85 | .callback(OptionCallback<Un7zipApp>(this, &Un7zipApp::handleList))); |
86 | |
87 | options.addOption( |
88 | Option("extract" , "x" , "Extract single file or entire archive." ) |
89 | .required(false) |
90 | .repeatable(false) |
91 | .argument("file" , false) |
92 | .callback(OptionCallback<Un7zipApp>(this, &Un7zipApp::handleExtract))); |
93 | } |
94 | |
95 | void handleHelp(const std::string& name, const std::string& value) |
96 | { |
97 | _helpRequested = true; |
98 | displayHelp(); |
99 | stopOptionsProcessing(); |
100 | } |
101 | |
102 | void handleOutput(const std::string& name, const std::string& value) |
103 | { |
104 | _outputPath = value; |
105 | } |
106 | |
107 | void handleList(const std::string& name, const std::string& value) |
108 | { |
109 | _list = true; |
110 | } |
111 | |
112 | void handleExtract(const std::string& name, const std::string& value) |
113 | { |
114 | _extract = true; |
115 | _extractFile = value; |
116 | } |
117 | |
118 | void displayHelp() |
119 | { |
120 | HelpFormatter helpFormatter(options()); |
121 | helpFormatter.setCommand(commandName()); |
122 | helpFormatter.setUsage("OPTIONS archive" ); |
123 | helpFormatter.setHeader("A application that demonstrates usage of Poco::SevenZip::Archive." ); |
124 | helpFormatter.format(std::cout); |
125 | } |
126 | |
127 | void (const Poco::SevenZip::Archive::ExtractedEventArgs& args) |
128 | { |
129 | std::cout << "Extracted: " << args.entry.path() << " ===> " << args.extractedPath << std::endl; |
130 | } |
131 | |
132 | void onFailed(const Poco::SevenZip::Archive::FailedEventArgs& args) |
133 | { |
134 | std::cout << "Failed: " << args.entry.path() << ": " << args.pException->displayText() << std::endl; |
135 | } |
136 | |
137 | int main(const std::vector<std::string>& args) |
138 | { |
139 | if (!_helpRequested && !args.empty()) |
140 | { |
141 | Poco::SevenZip::Archive archive(args[0]); |
142 | |
143 | if (_list) |
144 | { |
145 | for (Poco::SevenZip::Archive::ConstIterator it = archive.begin(); it != archive.end(); ++it) |
146 | { |
147 | std::cout |
148 | << (it->isFile() ? '-' : 'd') |
149 | << " " |
150 | << Poco::DateTimeFormatter::format(it->lastModified(), Poco::DateTimeFormat::SORTABLE_FORMAT) |
151 | << " " |
152 | << std::setw(12) << it->size() |
153 | << " " |
154 | << it->path() |
155 | << std::endl; |
156 | } |
157 | } |
158 | |
159 | if (_extract) |
160 | { |
161 | archive.extracted += Poco::delegate(this, &Un7zipApp::onExtracted); |
162 | archive.failed += Poco::delegate(this, &Un7zipApp::onFailed); |
163 | |
164 | if (_extractFile.empty()) |
165 | { |
166 | archive.extract(_outputPath); |
167 | } |
168 | else |
169 | { |
170 | for (Poco::SevenZip::Archive::ConstIterator it = archive.begin(); it != archive.end(); ++it) |
171 | { |
172 | if (it->path() == _extractFile) |
173 | { |
174 | archive.extract(*it, _outputPath); |
175 | } |
176 | } |
177 | } |
178 | } |
179 | } |
180 | return Application::EXIT_OK; |
181 | } |
182 | |
183 | private: |
184 | bool _helpRequested; |
185 | std::string _outputPath; |
186 | std::string ; |
187 | bool _list; |
188 | bool ; |
189 | }; |
190 | |
191 | |
192 | POCO_APP_MAIN(Un7zipApp) |
193 | |