Blender V4.3
zstd_compress.cpp
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2024 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#include <cstdint>
6#include <fstream>
7#include <vector>
8
9#include <zstd.h>
10
11int main(int argc, const char **argv)
12{
13 if (argc < 3) {
14 return -1;
15 }
16
17 /* TODO: This might fail for non-ASCII paths on Windows... */
18 std::ifstream in(argv[1], std::ios_base::binary);
19 std::ofstream out(argv[2], std::ios_base::binary);
20 if (!in || !out) {
21 return -1;
22 }
23
24 in.seekg(0, std::ios_base::end);
25 size_t in_size = in.tellg();
26 in.seekg(0, std::ios_base::beg);
27 if (!in) {
28 return -1;
29 }
30
31 std::vector<char> in_data(in_size);
32 in.read(in_data.data(), in_size);
33 if (!in) {
34 return -1;
35 }
36
37 size_t out_size = ZSTD_compressBound(in_size);
38 if (ZSTD_isError(out_size)) {
39 return -1;
40 }
41 std::vector<char> out_data(out_size);
42
43 out_size = ZSTD_compress(out_data.data(), out_data.size(), in_data.data(), in_data.size(), 19);
44 if (ZSTD_isError(out_size)) {
45 return -1;
46 }
47
48 out.write(out_data.data(), out_size);
49 if (!out) {
50 return -1;
51 }
52
53 return 0;
54}
int main()