MLBookProc 2.1
Loading...
Searching...
No Matches
LibArchive.h
1/*
2 * Copyright (C) 2026 Yury Bobylev <bobilev_yury@mail.ru>
3 *
4 * This program is free software: you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the Free
6 * Software Foundation, version 3.
7 *
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
13 * You should have received a copy of the GNU General Public License along with
14 * this program. If not, see <https://www.gnu.org/licenses/>.
15 */
16#ifndef LIBARCHIVE_H
17#define LIBARCHIVE_H
18
19#include <LibArchiveFileData.h>
20#include <MLBookProc.h>
21#include <archive.h>
22#include <filesystem>
23#include <istream>
24#include <memory>
25#include <string>
26#include <vector>
27
34{
35public:
40 LibArchive(const std::shared_ptr<MLBookProc> &mlbp);
41
42 virtual ~LibArchive();
43
54 void
56 const std::filesystem::path &archive_path,
57 std::vector<std::tuple<std::string, uint64_t, uint64_t>> &result);
58
70 void
72 const std::string &buffer,
73 std::vector<std::tuple<std::string, uint64_t, uint64_t>> &result);
74
87 void
89 const std::filesystem::path &archive_path,
90 std::vector<std::tuple<std::string, uint64_t, uint64_t>> &result);
91
103 void
105 const std::string &buffer,
106 std::vector<std::tuple<std::string, uint64_t, uint64_t>> &result);
107
120 std::filesystem::path
121 unpackFileToDirectory(const std::filesystem::path &archive_path,
122 const std::string &filename,
123 const std::filesystem::path &directory,
124 const size_t &offset = size_t(0));
125
138 std::filesystem::path
139 unpackBufferFileToDirectory(const std::string &buffer,
140 const std::string &filename,
141 const std::filesystem::path &directory,
142 const size_t &offset = size_t(0));
143
156 std::filesystem::path
157 unpackZipFileToDirectory(const std::filesystem::path &archive_path,
158 const std::string &filename,
159 const std::filesystem::path &directory);
160
171 std::filesystem::path
172 unpackZipBufferFileToDirectory(const std::string &buffer,
173 const std::string &filename,
174 const std::filesystem::path &directory);
175
187 std::string
188 unpackFileToBuffer(const std::filesystem::path &archive_path,
189 const std::string &filename,
190 const size_t &offset = size_t(0));
191
203 std::string
204 unpackBufferFileToBuffer(const std::string &buffer,
205 const std::string &filename,
206 const size_t &offset = size_t(0));
207
219 std::string
220 unpackZipFileToBuffer(const std::filesystem::path &archive_path,
221 const std::string &filename);
222
232 std::string
233 unpackZipBufferFileToBuffer(const std::string &buffer,
234 const std::string &filename);
235
249 void
250 writeToArchive(const std::filesystem::path &source_object,
251 const std::filesystem::path &archive_path,
252 std::string name_in_archive,
253 const std::filesystem::perms &perms
254 = std::filesystem::perms::none,
255 const bool &overwrite_existing = true);
256
269 void
270 writeBufferObjectToArchive(const std::string &buffer,
271 const std::filesystem::path &archive_path,
272 std::string name_in_archive,
273 const std::filesystem::perms &perms,
274 const bool &overwrite_existing);
275
276protected:
286 std::shared_ptr<archive>
287 initForReading(const std::shared_ptr<LibArchiveFileData> &fd);
288
298 std::shared_ptr<archive>
299 initForWriting(const std::shared_ptr<LibArchiveFileData> &fd);
300
311 void
312 archiveError(const std::shared_ptr<archive> &a, const std::string &message);
313
324 static int
325 openCallBack(archive *a, void *client_data);
326
337 static la_ssize_t
338 readCallBack(archive *a, void *client_data, const void **buffer);
339
350 static la_int64_t
351 skipCallback(archive *a, void *client_data, la_int64_t request);
352
362 static int
363 closeCallback(archive *a, void *client_data);
364
376 static la_int64_t
377 seekCallback(archive *a, void *client_data, la_int64_t offset, int whence);
378
390 static la_ssize_t
391 writeCallback(archive *a, void *client_data, const void *buffer,
392 size_t length);
393
404 std::filesystem::path
405 unpackToDirectory(std::shared_ptr<LibArchiveFileData> fd,
406 const std::string &filename,
407 const std::filesystem::path &directory);
408
418 void
419 unpackToBuffer(std::shared_ptr<LibArchiveFileData> fd,
420 const std::string &filename, std::string &result);
421
431 void
432 unpackEntryToDirectory(std::shared_ptr<archive> a,
433 std::shared_ptr<archive_entry> e,
434 const std::filesystem::path &file_path);
435
445 std::string
446 unpackEntryToBuffer(std::shared_ptr<archive> a,
447 std::shared_ptr<archive_entry> e);
448
458 void
459 writeFile(std::shared_ptr<archive> a, const std::filesystem::path &path,
460 std::string name_in_archive, const std::filesystem::perms &perms);
461
471 void
472 writeBufferToArchive(std::shared_ptr<archive> a,
473 std::shared_ptr<archive_entry> e,
474 const std::string &buf);
475
483 std::filesystem::perms
484 getPermissionsFromEntry(const std::shared_ptr<archive_entry> &e);
485
491 void
492 setUsernameGroupname(std::shared_ptr<archive_entry> e);
493
499 std::shared_ptr<MLBookProc> mlbp;
500
501private:
502 void
503 getCentralDirectory(std::shared_ptr<std::istream> f, const uint64_t &fsz,
504 std::string &result);
505
506 std::tuple<uint64_t, uint64_t>
507 parseEOCDRecord(std::shared_ptr<std::istream> f, const uint64_t &fsz);
508
509 std::tuple<uint64_t, uint64_t>
510 parseEOCDRecordZip64(std::shared_ptr<std::istream> f, const uint64_t &fsz,
511 const uint64_t &eocd_record_size);
512
513 void
514 parseCentralDirectory(
515 const std::string &central_directory,
516 std::vector<std::tuple<std::string, uint64_t, uint64_t>> &result);
517
518 void
519 parseExtraField(const std::string &extra, uint64_t &compressed_sz,
520 uint64_t &offset, const int &mask);
521
522 std::vector<std::tuple<std::filesystem::path, std::filesystem::path>>
523 symlinkWriteResolver(const std::filesystem::path &relative,
524 const std::filesystem::path &symlink);
525
526 bool
527 getUTFfilename(const std::string &extra_field, const std::string &crc_sum,
528 std::string &filename);
529
530 std::string
531 crc32Sum(unsigned char *buf, size_t len);
532
533 int ZIP64_UNCOMPRESSED = 1;
534 int ZIP64_COMPRESSED = 2;
535 int ZIP64_OFFSET = 4;
536};
537
538#endif // LIBARCHIVE_H
std::string unpackZipFileToBuffer(const std::filesystem::path &archive_path, const std::string &filename)
void archiveError(const std::shared_ptr< archive > &a, const std::string &message)
std::shared_ptr< archive > initForWriting(const std::shared_ptr< LibArchiveFileData > &fd)
void writeFile(std::shared_ptr< archive > a, const std::filesystem::path &path, std::string name_in_archive, const std::filesystem::perms &perms)
std::string unpackFileToBuffer(const std::filesystem::path &archive_path, const std::string &filename, const size_t &offset=size_t(0))
void unpackToBuffer(std::shared_ptr< LibArchiveFileData > fd, const std::string &filename, std::string &result)
static int closeCallback(archive *a, void *client_data)
std::filesystem::path unpackZipFileToDirectory(const std::filesystem::path &archive_path, const std::string &filename, const std::filesystem::path &directory)
std::string unpackEntryToBuffer(std::shared_ptr< archive > a, std::shared_ptr< archive_entry > e)
std::string unpackZipBufferFileToBuffer(const std::string &buffer, const std::string &filename)
void writeToArchive(const std::filesystem::path &source_object, const std::filesystem::path &archive_path, std::string name_in_archive, const std::filesystem::perms &perms=std::filesystem::perms::none, const bool &overwrite_existing=true)
static la_ssize_t readCallBack(archive *a, void *client_data, const void **buffer)
void unpackEntryToDirectory(std::shared_ptr< archive > a, std::shared_ptr< archive_entry > e, const std::filesystem::path &file_path)
static int openCallBack(archive *a, void *client_data)
void listFilesInArchiveBuffer(const std::string &buffer, std::vector< std::tuple< std::string, uint64_t, uint64_t > > &result)
std::filesystem::path unpackBufferFileToDirectory(const std::string &buffer, const std::string &filename, const std::filesystem::path &directory, const size_t &offset=size_t(0))
std::filesystem::path unpackFileToDirectory(const std::filesystem::path &archive_path, const std::string &filename, const std::filesystem::path &directory, const size_t &offset=size_t(0))
std::filesystem::perms getPermissionsFromEntry(const std::shared_ptr< archive_entry > &e)
std::filesystem::path unpackToDirectory(std::shared_ptr< LibArchiveFileData > fd, const std::string &filename, const std::filesystem::path &directory)
void listFilesInZipBuffer(const std::string &buffer, std::vector< std::tuple< std::string, uint64_t, uint64_t > > &result)
void listFilesInArchive(const std::filesystem::path &archive_path, std::vector< std::tuple< std::string, uint64_t, uint64_t > > &result)
static la_int64_t skipCallback(archive *a, void *client_data, la_int64_t request)
std::shared_ptr< MLBookProc > mlbp
Definition LibArchive.h:499
void writeBufferObjectToArchive(const std::string &buffer, const std::filesystem::path &archive_path, std::string name_in_archive, const std::filesystem::perms &perms, const bool &overwrite_existing)
void setUsernameGroupname(std::shared_ptr< archive_entry > e)
static la_ssize_t writeCallback(archive *a, void *client_data, const void *buffer, size_t length)
std::shared_ptr< archive > initForReading(const std::shared_ptr< LibArchiveFileData > &fd)
std::string unpackBufferFileToBuffer(const std::string &buffer, const std::string &filename, const size_t &offset=size_t(0))
void listFilesInZip(const std::filesystem::path &archive_path, std::vector< std::tuple< std::string, uint64_t, uint64_t > > &result)
std::filesystem::path unpackZipBufferFileToDirectory(const std::string &buffer, const std::string &filename, const std::filesystem::path &directory)
static la_int64_t seekCallback(archive *a, void *client_data, la_int64_t offset, int whence)
LibArchive(const std::shared_ptr< MLBookProc > &mlbp)
LibArchive constructor.
void writeBufferToArchive(std::shared_ptr< archive > a, std::shared_ptr< archive_entry > e, const std::string &buf)