Skip to content
LeviMC
English
AutoLightDark
LeviLamina
Loading...
Searching...
No Matches
CompressUtils.h
1#pragma once
2
3#include <cstddef>
4#include <filesystem>
5#include <functional>
6#include <memory>
7#include <string>
8#include <string_view>
9
10#include "ll/api/Expected.h"
11#include "ll/api/base/Macro.h"
12
13namespace ll::inline utils::compress_utils {
14
15enum class CompressFormat : int {
16 Gzip,
17 Zlib,
18};
19
20enum class CompressLevel : int {
21 Store,
22 Fastest,
23 Default,
24 Best,
25};
26
27LLNDAPI std::string_view extensionOf(CompressFormat format);
28
29using SinkFn = std::function<Expected<>(std::string_view)>;
30
31class Compressor {
32 struct Impl;
33 std::unique_ptr<Impl> impl;
34
35public:
36 LLAPI
37 Compressor(SinkFn sink, CompressFormat format = CompressFormat::Gzip, CompressLevel level = CompressLevel::Default);
38
39 LLAPI ~Compressor();
40
41 LLAPI Compressor(Compressor&&) noexcept;
42 LLAPI Compressor& operator=(Compressor&&) noexcept;
43
44 LLNDAPI Expected<> status() const;
45
46 LLAPI Expected<> write(std::string_view data);
47
48 LLAPI Expected<> finish();
49};
50
51class Decompressor {
52 struct Impl;
53 std::unique_ptr<Impl> impl;
54
55public:
56 LLAPI Decompressor(SinkFn sink, CompressFormat format = CompressFormat::Gzip);
57
58 LLAPI ~Decompressor();
59
60 LLAPI Decompressor(Decompressor&&) noexcept;
61 LLAPI Decompressor& operator=(Decompressor&&) noexcept;
62
63 LLNDAPI Expected<> status() const;
64
65 LLAPI Expected<> write(std::string_view data);
66
67 LLAPI Expected<> finish();
68};
69
70LLNDAPI Expected<std::string> compress(
71 std::string_view data,
72 CompressFormat format = CompressFormat::Gzip,
73 CompressLevel level = CompressLevel::Default
74);
75
76LLNDAPI Expected<std::string>
77 decompress(std::string_view data, CompressFormat format = CompressFormat::Gzip, size_t maxSize = 0);
78
79LLNDAPI Expected<> compressFile(
80 std::filesystem::path const& src,
81 std::filesystem::path const& dst,
82 CompressFormat format = CompressFormat::Gzip,
83 CompressLevel level = CompressLevel::Default
84);
85
86LLNDAPI Expected<> decompressFile(
87 std::filesystem::path const& src,
88 std::filesystem::path const& dst,
89 CompressFormat format = CompressFormat::Gzip
90);
91
92} // namespace ll::inline utils::compress_utils