-
Notifications
You must be signed in to change notification settings - Fork 0
/
zstd_test.cpp
58 lines (51 loc) · 1.35 KB
/
zstd_test.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
#include <cstdint>
#include <exception>
#include <fstream>
#include <iostream>
#include <vector>
#include <zstd.h>
class ex : public std::exception
{
std::string msg;
public:
ex(std::string m) : msg(m) {}
~ex() = default;
const char* what() const _NOEXCEPT override { return msg.c_str(); }
};
//
int
main(int argc, const char** args)
{
try
{
if (argc < 2)
throw ex{"no input file: <infile>"};
auto fname = std::string{args[1]};
auto infst = std::ifstream(fname);
if (!infst)
throw ex{"input file open failed: " + fname};
// input file read
infst.seekg(0, std::ios::end);
auto insz = infst.tellg();
auto readbuff = std::vector<char>(insz);
infst.seekg(0);
infst.read(readbuff.data(), insz);
// compress
auto cbsz = ZSTD_compressBound(insz);
auto cbuff = std::vector<char>(cbsz);
auto csz = ZSTD_compress(cbuff.data(), cbsz, readbuff.data(), insz, 3);
std::cout << "F:" << fname << ":" << insz << "->" << csz << std::endl;
// decompress
auto dbsz = ZSTD_getFrameContentSize(cbuff.data(), csz);
auto dbuff = std::vector<char>(dbsz);
auto dsz = ZSTD_decompress(dbuff.data(), dbsz, cbuff.data(), csz);
std::cout << "O: " << dbsz << "/" << dsz << std::endl;
}
catch (std::exception& e)
{
std::cerr << e.what() << std::endl;
return 1;
}
return 0;
}
//