-
Notifications
You must be signed in to change notification settings - Fork 1
/
zipper.cpp
101 lines (83 loc) · 2.51 KB
/
zipper.cpp
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
#include <iostream>
#include <fstream>
#include <vector>
#include <cstdio>
#include <filesystem>
class Zipper {
public:
Zipper() = default;
bool embedZipInImage(const std::string& zipFilePath,
const std::string& imagePath,
const std::string& outputImagePath) {
if (!fileExists(zipFilePath)) {
std::cerr << "ZIP file does not exist: " << zipFilePath << std::endl;
return false;
}
if (!fileExists(imagePath)) {
std::cerr << "Image file does not exist: " << imagePath << std::endl;
return false;
}
if (!appendZipToImage(imagePath, zipFilePath, outputImagePath)) {
std::cerr << "Failed to append ZIP file to the image.\n";
return false;
}
return true;
}
private:
bool fileExists(const std::string& path) {
return std::filesystem::exists(path);
}
bool appendZipToImage(const std::string& imagePath, const std::string& zipFilePath, const std::string& outputFilePath) {
try {
std::ifstream imageFile(imagePath, std::ios::binary);
std::ifstream zipFile(zipFilePath, std::ios::binary);
if (!imageFile.is_open()) {
std::cerr << "Failed to open image file: " << imagePath << std::endl;
return false;
}
if (!zipFile.is_open()) {
std::cerr << "Failed to open ZIP file: " << zipFilePath << std::endl;
return false;
}
std::ofstream outputFile(outputFilePath, std::ios::binary);
if (!outputFile.is_open()) {
std::cerr << "Failed to open output file: " << outputFilePath << std::endl;
return false;
}
std::vector<char> buffer(1024 * 1024); // 1mb
while (!imageFile.eof()) {
imageFile.read(buffer.data(), buffer.size());
std::streamsize size = imageFile.gcount();
outputFile.write(buffer.data(), size);
}
while (!zipFile.eof()) {
zipFile.read(buffer.data(), buffer.size());
std::streamsize size = zipFile.gcount();
outputFile.write(buffer.data(), size);
}
}
catch (const std::ios_base::failure& e) {
std::cerr << "File operation failed: " << e.what() << std::endl;
return false;
}
return true;
}
};
int main(int argc, char* argv[]) {
if (argc != 4) {
std::cerr << "Usage: " << argv[0] << " <image_path> <output_image_path> <zip_file_path>\n";
return 1;
}
std::string imagePath = argv[1];
std::string outputImagePath = argv[2];
std::string zipFilePath = argv[3];
Zipper zipper;
if (zipper.embedZipInImage(zipFilePath, imagePath, outputImagePath)) {
std::cout << "ZIP file appended successfully to the image!\n";
}
else {
std::cerr << "Failed to append ZIP file to the image.\n";
return 1;
}
return 0;
}