MLBookProc 1.1
 
Loading...
Searching...
No Matches
LibArchive.h
1/*
2 * Copyright (C) 2024-2025 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
17#ifndef LIBARCHIVE_H
18#define LIBARCHIVE_H
19
20#include <ArchEntry.h>
21#include <ArchiveFileEntry.h>
22#include <ArchiveRemoveEntry.h>
23#include <AuxFunc.h>
24#include <archive_entry.h>
25#include <filesystem>
26#include <memory>
27#include <string>
28#include <tuple>
29#include <vector>
30
38{
39public:
44 LibArchive(const std::shared_ptr<AuxFunc> &af);
45
60 std::filesystem::path
61 unpackByPosition(const std::filesystem::path &archaddress,
62 const std::filesystem::path &outfolder,
63 const ArchEntry &entry);
64
78 std::string
79 unpackByPositionStr(const std::filesystem::path &archaddress,
80 const ArchEntry &entry);
81
95 std::filesystem::path
96 unpackByFileNameStream(const std::filesystem::path &archaddress,
97 const std::filesystem::path &outfolder,
98 const std::string &filename);
99
113 std::string
114 unpackByFileNameStreamStr(const std::filesystem::path &archaddress,
115 const std::string &filename);
116
127 int
128 fileNames(const std::filesystem::path &filepath,
129 std::vector<ArchEntry> &filenames);
130
142 int
143 fileNamesStream(const std::filesystem::path &address,
144 std::vector<ArchEntry> &filenames);
145
155 fileinfo(const std::filesystem::path &address, const std::string &filename);
156
165 int
166 libarchive_packing(const std::filesystem::path &sourcepath,
167 const std::filesystem::path &outpath);
168
184 int
185 libarchive_packing(const std::shared_ptr<archive> &a,
186 const std::filesystem::path &sourcepath,
187 const bool &rename_source,
188 const std::string &new_source_name);
189
197 libarchive_remove_init(const std::filesystem::path &sourcepath,
198 const std::filesystem::path &outpath);
199
207 int
209 const std::vector<ArchEntry> &to_remove);
210
218 void
219 libarchive_error(const std::shared_ptr<archive> &a,
220 const std::string &message, const int &error_number);
221
231 std::string
232 libarchive_read_entry_str(archive *a, archive_entry *entry);
233
244 int
245 libarchive_write_data(archive *a, const std::string &data);
246
256 std::shared_ptr<ArchiveFileEntry>
257 createArchFile(const std::filesystem::path &archaddress,
258 const la_int64_t &position = la_int64_t(0));
259
269 std::shared_ptr<archive>
270 libarchive_read_init(std::shared_ptr<ArchiveFileEntry> fl);
271
280 std::shared_ptr<archive>
281 libarchive_read_init_fallback(std::shared_ptr<ArchiveFileEntry> fl);
282
295 std::filesystem::path
296 libarchive_read_entry(archive *a, archive_entry *entry,
297 const std::filesystem::path &outfolder);
298
307 std::shared_ptr<archive>
308 libarchive_write_init(const std::filesystem::path &outpath);
309
322 int
323 writeToArchive(std::shared_ptr<archive> a,
324 const std::filesystem::path &source,
325 const std::filesystem::path &path_in_arch);
326
342 int
343 libarchive_write_directory(archive *a, archive_entry *entry,
344 const std::filesystem::path &path_in_arch,
345 const std::filesystem::path &source);
346
360 int
361 libarchive_write_file(archive *a, archive_entry *entry,
362 const std::filesystem::path &path_in_arch,
363 const std::filesystem::path &source);
364
374 int
376 const std::filesystem::path &source);
377
378private:
379 static int
380 libarchive_open_callback(archive *a, void *data);
381
382 static la_ssize_t
383 libarchive_read_callback(archive *a, void *data, const void **buffer);
384
385 static la_int64_t
386 libarchive_skip_callback(archive *a, void *data, la_int64_t request);
387
388 static la_int64_t
389 libarchive_seek_callback(archive *a, void *data, la_int64_t offset,
390 int whence);
391
392 static int
393 libarchive_close_callback(archive *a, void *data);
394
395 std::vector<std::tuple<std::filesystem::path, std::filesystem::path>>
396 dir_symlink_resolver(const std::filesystem::path &source,
397 const std::filesystem::path &append_to);
398
399 static la_ssize_t
400 libarchive_write_callback(archive *a, void *data, const void *buffer,
401 size_t length);
402
403 static int
404 libarchive_free_callback(archive *a, void *data);
405
406 static int
407 libarchive_open_callback_write(archive *a, void *data);
408
409 std::shared_ptr<AuxFunc> af;
410};
411
412#endif // LIBARCHIVE_H
The ArchEntry class.
Definition ArchEntry.h:31
The ArchiveRemoveEntry class.
Definition ArchiveRemoveEntry.h:31
std::shared_ptr< archive > libarchive_write_init(const std::filesystem::path &outpath)
Initializes writing to archive.
std::string libarchive_read_entry_str(archive *a, archive_entry *entry)
Reads archived file to stirng.
ArchEntry fileinfo(const std::filesystem::path &address, const std::string &filename)
Returns ArchEntry for particular file or directory in archive.
int libarchive_write_data(archive *a, const std::string &data)
Writes data to archive.
int libarchive_remove_entry(ArchiveRemoveEntry rm_e, const std::vector< ArchEntry > &to_remove)
Removes entry from archive.
LibArchive(const std::shared_ptr< AuxFunc > &af)
LibArchive constructor.
int libarchive_write_data_from_file(archive *a, const std::filesystem::path &source)
Writes raw data from file to archive.
std::filesystem::path unpackByPosition(const std::filesystem::path &archaddress, const std::filesystem::path &outfolder, const ArchEntry &entry)
Unpacks single entry content from zip archive.
int fileNamesStream(const std::filesystem::path &address, std::vector< ArchEntry > &filenames)
Lists all entries in archive file.
std::string unpackByFileNameStreamStr(const std::filesystem::path &archaddress, const std::string &filename)
Unpacks entry content from archive.
std::shared_ptr< archive > libarchive_read_init(std::shared_ptr< ArchiveFileEntry > fl)
Initializes archive reading.
std::filesystem::path libarchive_read_entry(archive *a, archive_entry *entry, const std::filesystem::path &outfolder)
Unpacks libarchive entry content.
void libarchive_error(const std::shared_ptr< archive > &a, const std::string &message, const int &error_number)
Prints libarchive error messages.
int writeToArchive(std::shared_ptr< archive > a, const std::filesystem::path &source, const std::filesystem::path &path_in_arch)
Writes file or directory to archive.
std::string unpackByPositionStr(const std::filesystem::path &archaddress, const ArchEntry &entry)
Unpacks single entry content from zip archive.
int fileNames(const std::filesystem::path &filepath, std::vector< ArchEntry > &filenames)
Lists all entries in archive file.
int libarchive_packing(const std::filesystem::path &sourcepath, const std::filesystem::path &outpath)
Packs file or directory into archive.
std::shared_ptr< archive > libarchive_read_init_fallback(std::shared_ptr< ArchiveFileEntry > fl)
Initializes archive reading.
int libarchive_write_directory(archive *a, archive_entry *entry, const std::filesystem::path &path_in_arch, const std::filesystem::path &source)
Writes directory to archive.
std::shared_ptr< ArchiveFileEntry > createArchFile(const std::filesystem::path &archaddress, const la_int64_t &position=la_int64_t(0))
Creates ArchiveFileEntry object.
std::filesystem::path unpackByFileNameStream(const std::filesystem::path &archaddress, const std::filesystem::path &outfolder, const std::string &filename)
Unpacks entry content from archive.
int libarchive_write_file(archive *a, archive_entry *entry, const std::filesystem::path &path_in_arch, const std::filesystem::path &source)
Writes file to archive.
ArchiveRemoveEntry libarchive_remove_init(const std::filesystem::path &sourcepath, const std::filesystem::path &outpath)
Initializes archive objects for removing entries from archive.
int libarchive_packing(const std::shared_ptr< archive > &a, const std::filesystem::path &sourcepath, const bool &rename_source, const std::string &new_source_name)
Packs file or directory into archive.