-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_snappy.cpp
81 lines (65 loc) · 2.48 KB
/
test_snappy.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
#include <iostream>
#include <snappy.h>
#include <fstream>
class CompressedData {
public:
CompressedData(const std::string& data) : originalData(data) {
compressData();
}
void saveToFile(const std::string& filename) const {
std::ofstream file(filename, std::ios::binary);
std::cout << compressedData << std::endl;
if (file.is_open()) {
file.write(compressedData.data(), compressedData.size());
file.close();
std::cout << "Data saved to file: " << filename << std::endl;
} else {
std::cerr << "Error opening file for writing." << std::endl;
}
}
void readFromFile(const std::string& filename) {
std::ifstream file(filename, std::ios::binary | std::ios::ate);
if (file.is_open()) {
std::streamsize size = file.tellg();
file.seekg(0, std::ios::beg);
compressedData.resize(static_cast<size_t>(size));
if (file.read(compressedData.data(), size)) {
std::cout << "Data read from file: " << filename << std::endl;
decompressData();
} else {
std::cerr << "Error reading from file." << std::endl;
}
file.close();
} else {
std::cerr << "Error opening file for reading." << std::endl;
}
}
void printData() const {
std::cout << "Original Data: " << originalData << std::endl;
std::cout << "Decompressed Data: " << decompressedData << std::endl;
}
private:
std::string originalData;
std::string compressedData;
std::string decompressedData;
void compressData() {
snappy::Compress(originalData.data(), originalData.size(), &compressedData);
// snappy::Compress(originalData.data(), originalData.size(), &compressedData);
}
void decompressData() {
snappy::Uncompress(compressedData.data(), compressedData.size(), &decompressedData);
}
};
int main() {
std::string originalText = "100001100002100003100004100005100007100008100009100010";
// Create CompressedData object with Snappy compression
CompressedData compressedData(originalText);
// Save compressed data to file
compressedData.saveToFile("compressed_data.snappy");
// Read compressed data from file and decompress
CompressedData readData("");
readData.readFromFile("compressed_data.snappy");
// Print original and decompressed data
readData.printData();
return 0;
}