dl
dl_tool_archiver.h
浏览该文件的文档.
1
12#pragma once
13
14#include <memory/dl_buffer.h>
17#include <io/dl_file.h>
18#include <memory/dl_crypto.h>
19#include <base/dl_string.h>
20
21
22namespace dl::Tool
23{
24 constexpr uint16_t ARCHIVER_TYPE = 0x4002;
25 constexpr uint8_t ARCHIVER_VERSION = 1;
26
29{
30public:
31 struct Head
32 {
33 uint16_t _type;
34 uint8_t _version;
36 uint32_t _sizeFileList; // 文件列表(压缩前大小)
37 uint32_t _sizeFileListCompress; // 文件列表(压缩后大小)
38 uint64_t _sizeFileBlock; // 文件块大小(压缩前)
39 };
40 struct FileInfo
41 {
42 uint64_t _offset; // 内存偏移
43 uint32_t _size; // 大小
44 uint32_t _crc; // crc
45 };
46 using FileList = std::unordered_map<std::string, FileInfo>;
47
48 struct Param
49 {
50 std::string _src;
51 std::string _out;
53 bool _bCrc;
54 };
55
56 static void Compress(const Param& p)
57 {
58 std::vector<std::string> vec_file;
59 File::GetPathFileAll(p._src, vec_file);
60 if (vec_file.empty())
61 {
62 log_warn("文件数量为0!");
63 }
64 FileList all_file;
65 // 文件块
67 for (auto& iter : vec_file)
68 {
69 Buffer buf = File::GetBuffer(iter);
70 uint32_t crc = 0;
71 if (p._bCrc)
72 crc = Crypto::HashCrc32(buf);
73
74 FileInfo& f = all_file[File::GetRelative(iter, p._src)];
75 f._offset = s.GetPos();
76 f._size = (uint32_t)buf.size();
77 f._crc = crc;
78
79 s.Write(&buf);
80 }
81 // 文件列表
82 StreamOutput s1;
83 s1.Write(&all_file);
84
85 // 头
86 Head head;
87 head._type = ARCHIVER_TYPE;
90 head._sizeFileList = (uint32_t)s1.GetPos();
92 head._sizeFileListCompress = (uint32_t)s1.GetPos();
93 head._sizeFileBlock = s.GetPos();
94
96
97 StreamOutput s_all;
98 s_all.Write(&head);
99 s_all.Write(s1.GetBuffer(), s1.GetPos());
100 s_all.Write(s.GetBuffer(), s.GetPos());
101
102 s_all.Save(p._out);
103 }
104 static bool Dempress(BufferView buf, std::string_view out)
105 {
106 StreamInput s_all;
107 s_all.LoadFromMemory(buf);
108
109 Head head;
110 s_all.Read(&head);
111
112 if (head._type != ARCHIVER_TYPE)
113 {
114 log_err("文件类型错误!");
115 return false;
116 }
117 if (head._version != ARCHIVER_VERSION)
118 {
119 log_err("存档版本错误!");
120 return false;
121 }
122 // 文件列表
123 FileList all_file;
124 {
125 Buffer buf_temp;
126 BufferView buf;
127 if (head._modeCompress == Memory::E_LZ4)
128 {
129 buf_temp = Memory::DecompressLz4(
130 { s_all.GetCurrent(), head._sizeFileListCompress }, (size_t)head._sizeFileList);
131 buf = buf_temp;
132 }
133#ifndef DL_DISABLE_ZLIB
134 else if (head._modeCompress == Memory::E_ZIP)
135 {
136 buf_temp = Memory::DecompressZip(
137 { s_all.GetCurrent(), head._sizeFileListCompress }, (size_t)head._sizeFileList);
138 buf = buf_temp;
139 }
140#endif
141 else
142 buf = { s_all.GetCurrent(), head._sizeFileListCompress };
143 StreamInput s1;
144 s1.LoadFromMemory(buf);
145 s1.Read(&all_file);
146 }
147 // 文件块
148 {
149 Buffer buf_temp;
150 BufferView buf;
151 if (head._modeCompress == Memory::E_LZ4)
152 {
153 buf_temp = Memory::DecompressLz4(
154 { s_all.GetCurrent() + head._sizeFileListCompress, s_all.GetRemain() }, (size_t)head._sizeFileBlock);
155 buf = buf_temp;
156 }
157#ifndef DL_DISABLE_ZLIB
158 else if (head._modeCompress == Memory::E_ZIP)
159 {
160 buf_temp = Memory::DecompressZip(
161 { s_all.GetCurrent() + head._sizeFileListCompress, s_all.GetRemain() }, (size_t)head._sizeFileBlock);
162 buf = buf_temp;
163 }
164#endif
165 else
166 buf = { s_all.GetCurrent() + head._sizeFileListCompress, s_all.GetRemain() };
167
168 size_t count = 0;
169 for (auto& [name, info] : all_file)
170 {
171 std::byte* p = buf.data() + info._offset;
172 std::size_t size_file = (std::size_t)*(uint64_t*)p;
173 if (info._size != size_file)
174 {
175 log_err("文件大小不匹配:{}", size_file);
176 continue;
177 }
178 p += sizeof(uint64_t);
179
180 std::string path_name = out + name;
181 if (!File::CreateFolder(File::GetFilePath(path_name)))
182 {
183 log_err("路径创建失败:{}", File::GetFilePath(path_name));
184 continue;
185 }
186 if (!File::SaveBuffer(
187 { p, size_file }, path_name))
188 {
189 log_err("文件保存失败:{}", path_name);
190 continue;
191 }
192 ++count;
193 }
194 return count == all_file.size();
195 }
196 }
197 static bool Dempress(std::string_view src, std::string_view out)
198 {
199 Buffer buf = File::GetBuffer(src);
200 if (buf.empty())
201 {
202 log_err("加载文件失败:{}", src);
203 return false;
204 }
205 return Dempress(buf, out);
206 }
207 static bool GetFileList(BufferView buf, FileList& all_file)
208 {
209 StreamInput s_all;
210 s_all.LoadFromMemory(buf);
211
212 Head head;
213 s_all.Read(&head);
214
215 if (head._type != ARCHIVER_TYPE)
216 {
217 log_err("文件类型错误!");
218 return false;
219 }
220 if (head._version != ARCHIVER_VERSION)
221 {
222 log_err("存档版本错误!");
223 return false;
224 }
225 {
226 Buffer buf_temp;
227 BufferView buf;
228 if (head._modeCompress == Memory::E_LZ4)
229 {
230 buf_temp = Memory::DecompressLz4(
231 { s_all.GetCurrent(), head._sizeFileListCompress }, (size_t)head._sizeFileList);
232 buf = buf_temp;
233 }
234#ifndef DL_DISABLE_ZLIB
235 else if (head._modeCompress == Memory::E_ZIP)
236 {
237 buf_temp = Memory::DecompressZip(
238 { s_all.GetCurrent(), head._sizeFileListCompress }, (size_t)head._sizeFileList);
239 buf = buf_temp;
240 }
241#endif
242 else
243 buf = { s_all.GetCurrent(), head._sizeFileListCompress };
244 StreamInput s1;
245 s1.LoadFromMemory(buf);
246 s1.Read(&all_file);
247 }
248 return true;
249 }
250private:
251 // 头
252
253 // 文件列表(压缩)
254 // name、FileInfo
255
256 // 文件数据(压缩)
257 // buffer
258};
259
260}
二进制输入流
void LoadFromMemory(BufferView buf, bool decompress=false)
二进制输出流
void Compress(Memory::CompressMode mode)
压缩当前内存
const std::byte * GetBuffer()
返回指向的内存指针
bool Save(std::string_view path_name, bool compress=false)
保存到文件
size_t GetPos()
返回 流位置
void Write(const T *data)
写入一个对象
文件夹打包解包器
std::unordered_map< std::string, FileInfo > FileList
static void Compress(const Param &p)
static bool GetFileList(BufferView buf, FileList &all_file)
static bool Dempress(BufferView buf, std::string_view out)
static bool Dempress(std::string_view src, std::string_view out)
表示一段内存
密码算法
文件系统
#define log_warn(...)
#define log_err(...)
字符串相关
uint32_t HashCrc32(BufferView buf, uint32_t crc=0)
crc32哈希
void GetPathFileAll(std::string_view path, std::vector< std::string > &ret, std::string_view regex="", bool recursion=true)
获取文件夹所有文件
bool SaveBuffer(BufferView buf, std::string_view path_name)
将内存保存为文件
std::string GetFilePath(std::string_view path_name)
返回文件所在路径
bool CreateFolder(std::string_view path)
创建目录
std::string GetRelative(std::string_view path_name, std::string_view path_base)
返回相对路径
Buffer GetBuffer(std::string_view path_name)
返回文件内存
Buffer DecompressLz4(BufferView src, size_t size_origin=0)
解压缩数据,使用lz4算法
Buffer DecompressZip(BufferView src, size_t size_origin=0)
解压缩数据,使用libz
constexpr uint16_t ARCHIVER_TYPE
constexpr uint8_t ARCHIVER_VERSION
std::span< std::byte > BufferView
std::vector< std::byte > Buffer
Memory::CompressMode _compressMode