game_launcher 1.1.0
Modern Linux game launcher with Steam / Proton / Wine integration
Loading...
Searching...
No Matches
archive_extract_service.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// ==============================================================================
3// archive_extract_service.h
4// ==============================================================================
5// Archive extraction service supporting multiple formats
6// ==============================================================================
7// Provides archive extraction using system tools (tar, unzip, 7z, etc.)
8// Supports: ZIP, TAR (gz/bz2/xz), 7z, RAR, CAB, InnoSetup, NSIS
9// ==============================================================================
10
11#pragma once
12
13#include <cstdint>
14#include <functional>
15#include <string>
16#include <vector>
17
18// ==============================================================================
19// Archive Types
20// ==============================================================================
21
22enum class ArchiveType
23{
29 TarZst, // tar + zstd (GNU tar --zstd; vkd3d-proton releases ship as .tar.zst)
34 InnoSetup, // Windows InnoSetup installers (.exe)
35 NSIS, // Nullsoft installers (.exe)
36 Exe, // Generic self-extracting exe
37 Gzip, // Single-file gzip
38 Bzip2, // Single-file bzip2
39 Xz // Single-file xz
40};
41
42// ==============================================================================
43// Extract Request
44// ==============================================================================
45
47{
48 std::string archivePath;
49 std::string destinationDir;
50 ArchiveType type = ArchiveType::Unknown; // Auto-detect if Unknown
51 std::vector<std::string> includePatterns; // Only extract matching files (optional)
52 std::vector<std::string> excludePatterns; // Skip matching files (optional)
54 bool overwriteExisting = true;
55 std::string password; // For encrypted archives
56};
57
58// ==============================================================================
59// Extract Result
60// ==============================================================================
61
63{
64 bool success = false;
65 std::string destinationDir;
66 std::vector<std::string> extractedFiles;
67 uint64_t totalBytesExtracted = 0;
68 std::string errorMessage;
69 int exitCode = 0;
70};
71
72// ==============================================================================
73// Archive Info
74// ==============================================================================
75
77{
79 std::string typeName;
80 uint64_t compressedSize = 0;
81 uint64_t uncompressedSize = 0;
82 int fileCount = 0;
83 bool isEncrypted = false;
84 bool isMultiVolume = false;
85 std::vector<std::string> contents; // File listing
86};
87
88// ==============================================================================
89// Archive Extract Service
90// ==============================================================================
91
92/**
93 * @class ArchiveExtractService
94 * @brief Provides archive extraction using system tools
95 *
96 * Uses external command-line tools for extraction:
97 * - tar: .tar, .tar.gz, .tar.bz2, .tar.xz, .tar.zst
98 * - unzip: .zip
99 * - 7z: .7z, .rar, and many other formats
100 * - innoextract: InnoSetup .exe installers
101 * - cabextract: .cab files
102 *
103 * Falls back gracefully if tools are not installed.
104 */
106{
107 public:
108 using ProgressCallback = std::function<void(int percent, const std::string& currentFile)>;
109
110 // =========================================================================
111 // Extraction
112 // =========================================================================
113
114 /**
115 * @brief Extract an archive
116 * @param request Extraction configuration
117 * @param progressCallback Called with extraction progress (if available)
118 * @return Result of the extraction
119 */
120 static ExtractResult extract(const ExtractRequest& request,
121 const ProgressCallback& progressCallback = nullptr);
122
123 /**
124 * @brief Extract an archive asynchronously
125 * @param request Extraction configuration
126 * @param onComplete Called when extraction finishes
127 * @param onProgress Called with extraction progress
128 */
129 static void extractAsync(const ExtractRequest& request,
130 const std::function<void(const ExtractResult&)>& onComplete,
131 const ProgressCallback& onProgress = nullptr);
132
133 // =========================================================================
134 // Archive Information
135 // =========================================================================
136
137 /**
138 * @brief Detect archive type from file
139 * @param filePath Path to the archive
140 * @return Detected archive type
141 */
142 static ArchiveType detectType(const std::string& filePath);
143
144 /**
145 * @brief Get human-readable name for archive type
146 * @param type Archive type enum
147 * @return Type name string
148 */
149 static std::string getTypeName(ArchiveType type);
150
151 /**
152 * @brief Get archive information without extracting
153 * @param archivePath Path to the archive
154 * @return Archive information
155 */
156 static ArchiveInfo getInfo(const std::string& archivePath);
157
158 /**
159 * @brief List archive contents without extracting
160 * @param archivePath Path to the archive
161 * @return List of file paths in the archive
162 */
163 static std::vector<std::string> listContents(const std::string& archivePath);
164
165 // =========================================================================
166 // Tool Availability
167 // =========================================================================
168
169 /**
170 * @brief Check if extraction for a type is supported
171 * @param type Archive type
172 * @return True if the required tool is installed
173 */
174 static bool isSupported(ArchiveType type);
175
176 /**
177 * @brief Get the command-line tool used for a type
178 * @param type Archive type
179 * @return Tool name (e.g., "tar", "unzip", "7z")
180 */
181 static std::string getToolName(ArchiveType type);
182
183 /**
184 * @brief Check if a tool is installed
185 * @param toolName Tool name (e.g., "7z", "innoextract")
186 * @return True if tool is available in PATH
187 */
188 static bool isToolInstalled(const std::string& toolName);
189
190 /**
191 * @brief Get installation command for a tool
192 * @param toolName Tool name
193 * @return Package manager command to install (or empty if unknown)
194 */
195 static std::string getInstallCommand(const std::string& toolName);
196
197 private:
198 // Type-specific extractors
199 static ExtractResult extractZip(const ExtractRequest& request,
200 const ProgressCallback& progress);
201 static ExtractResult extractTar(const ExtractRequest& request,
202 const ProgressCallback& progress);
203 static ExtractResult extractSevenZip(const ExtractRequest& request,
204 const ProgressCallback& progress);
205 static ExtractResult extractRar(const ExtractRequest& request,
206 const ProgressCallback& progress);
207 static ExtractResult extractCab(const ExtractRequest& request,
208 const ProgressCallback& progress);
209 static ExtractResult extractInnoSetup(const ExtractRequest& request,
210 const ProgressCallback& progress);
211 static ExtractResult extractNSIS(const ExtractRequest& request,
212 const ProgressCallback& progress);
213 static ExtractResult extractGzip(const ExtractRequest& request,
214 const ProgressCallback& progress);
215
216 // Helpers
217 static ArchiveType detectFromExtension(const std::string& filePath);
218 static ArchiveType detectFromMagicBytes(const std::string& filePath);
219 static std::vector<std::string> parseFileList(const std::string& output, ArchiveType type);
220 static int runCommand(const std::string& command, std::string& output);
221
222 /**
223 * @brief Reject archives containing path-traversal / absolute members.
224 *
225 * SECURITY (CWE-22 Zip-Slip / CWE-59): lists the archive members with the
226 * same tool used for extraction (so NSIS/EXE routed to 7z are covered) and
227 * returns true if any entry is absolute or contains a ".." component. The
228 * extractors delegate to external tools (notably p7zip, which does not
229 * reliably reject "../" members), so this is the containment gate.
230 * @return true if the archive is unsafe and must not be extracted.
231 */
232 static bool hasUnsafeEntries(const std::string& archivePath, ArchiveType type);
233};
Provides archive extraction using system tools.
static std::string getToolName(ArchiveType type)
Get the command-line tool used for a type.
static std::string getTypeName(ArchiveType type)
Get human-readable name for archive type.
static void extractAsync(const ExtractRequest &request, const std::function< void(const ExtractResult &)> &onComplete, const ProgressCallback &onProgress=nullptr)
Extract an archive asynchronously.
std::function< void(int percent, const std::string &currentFile)> ProgressCallback
static ExtractResult extract(const ExtractRequest &request, const ProgressCallback &progressCallback=nullptr)
Extract an archive.
static bool isSupported(ArchiveType type)
Check if extraction for a type is supported.
static ArchiveType detectType(const std::string &filePath)
Detect archive type from file.
static ArchiveInfo getInfo(const std::string &archivePath)
Get archive information without extracting.
static bool isToolInstalled(const std::string &toolName)
Check if a tool is installed.
static std::vector< std::string > listContents(const std::string &archivePath)
List archive contents without extracting.
static std::string getInstallCommand(const std::string &toolName)
Get installation command for a tool.
std::vector< std::string > contents
std::vector< std::string > includePatterns
std::vector< std::string > excludePatterns
std::vector< std::string > extractedFiles