The LibArchive class. More...
#include <LibArchive.h>
Public Member Functions | |
LibArchive (const std::shared_ptr< AuxFunc > &af) | |
LibArchive constructor. | |
std::filesystem::path | unpackByPosition (const std::filesystem::path &archaddress, const std::filesystem::path &outfolder, const ArchEntry &entry) |
Unpacks single entry content from zip archive. | |
std::string | unpackByPositionStr (const std::filesystem::path &archaddress, const ArchEntry &entry) |
Unpacks single entry content from zip archive. | |
std::filesystem::path | unpackByFileNameStream (const std::filesystem::path &archaddress, const std::filesystem::path &outfolder, const std::string &filename) |
Unpacks entry content from archive. | |
std::string | unpackByFileNameStreamStr (const std::filesystem::path &archaddress, const std::string &filename) |
Unpacks entry content from archive. | |
int | fileNames (const std::filesystem::path &filepath, std::vector< ArchEntry > &filenames) |
Lists all entries in archive file. | |
int | fileNamesStream (const std::filesystem::path &address, std::vector< ArchEntry > &filenames) |
Lists all entries in archive file. | |
ArchEntry | fileinfo (const std::filesystem::path &address, const std::string &filename) |
Returns ArchEntry for particular file or directory in archive. | |
int | libarchive_packing (const std::filesystem::path &sourcepath, const std::filesystem::path &outpath) |
Packs file or directory into 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. | |
ArchiveRemoveEntry | libarchive_remove_init (const std::filesystem::path &sourcepath, const std::filesystem::path &outpath) |
Initializes archive objects for removing entries from archive. | |
int | libarchive_remove_entry (ArchiveRemoveEntry rm_e, const std::vector< ArchEntry > &to_remove) |
Removes entry from archive. | |
void | libarchive_error (const std::shared_ptr< archive > &a, const std::string &message, const int &error_number) |
Prints libarchive error messages. | |
std::string | libarchive_read_entry_str (archive *a, archive_entry *entry) |
Reads archived file to stirng. | |
int | libarchive_write_data (archive *a, const std::string &data) |
Writes data 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::shared_ptr< archive > | libarchive_read_init (std::shared_ptr< ArchiveFileEntry > fl) |
Initializes archive reading. | |
std::shared_ptr< archive > | libarchive_read_init_fallback (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. | |
std::shared_ptr< archive > | libarchive_write_init (const std::filesystem::path &outpath) |
Initializes writing to archive. | |
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. | |
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. | |
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. | |
int | libarchive_write_data_from_file (archive *a, const std::filesystem::path &source) |
Writes raw data from file to archive. | |
The LibArchive class.
This class contains various methods for archives processing. Based on libarchive library.
LibArchive::LibArchive | ( | const std::shared_ptr< AuxFunc > & | af | ) |
LibArchive constructor.
af | smart pointer to AuxFunc object. |
std::shared_ptr< ArchiveFileEntry > LibArchive::createArchFile | ( | const std::filesystem::path & | archaddress, |
const la_int64_t & | position = la_int64_t(0) ) |
Creates ArchiveFileEntry object.
ArchiveFileEntry object is used in libarchive_read_init() and libarchive_read_init_fallback() methods.
archaddress | absolute path to archive to be read. |
position | position in archive file to start reading from. |
ArchEntry LibArchive::fileinfo | ( | const std::filesystem::path & | address, |
const std::string & | filename ) |
int LibArchive::fileNames | ( | const std::filesystem::path & | filepath, |
std::vector< ArchEntry > & | filenames ) |
Lists all entries in archive file.
filepath | absolute path to zip archive. |
filenames | vector for results. |
int LibArchive::fileNamesStream | ( | const std::filesystem::path & | address, |
std::vector< ArchEntry > & | filenames ) |
Lists all entries in archive file.
This method can be used with all supported archive types (see AuxFunc::get_supported_archive_types_unpacking()). However for zip archives it is recommended to use fileNames() method.
address | absolute path to archive. |
filenames | vector of results. |
void LibArchive::libarchive_error | ( | const std::shared_ptr< archive > & | a, |
const std::string & | message, | ||
const int & | error_number ) |
Prints libarchive error messages.
a | smart pointer to libarchive object. |
message | extra text if needed (will be shown before libarchive error text). |
error_number | error code. |
int LibArchive::libarchive_packing | ( | const std::filesystem::path & | sourcepath, |
const std::filesystem::path & | outpath ) |
Packs file or directory into archive.
sourcepath | absolute path to file or directory to be packed. |
outpath | absolute path to resulting archive. |
int LibArchive::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.
Use this method if you need source file or directory to be packed under another name.
a | smart pointer to libarchive object (see libarchive_write_init()). |
sourcepath | absolute path to file or directory to be packed. |
rename_source | if set to true, source name will be replaced for new_source_name inside the archive. |
new_source_name | new name to be used inside the archive. Should be UTF-8 string. |
std::filesystem::path LibArchive::libarchive_read_entry | ( | archive * | a, |
archive_entry * | entry, | ||
const std::filesystem::path & | outfolder ) |
Unpacks libarchive entry content.
In most cases you do not need to use this method directly. Use unpackByPosition(), unpackByPositionStr(), unpackByFileNameStream(), unpackByFileNameStreamStr() methods instead.
a | pointer to libarchive object. |
entry | pointer to libarchive entry object. |
outfolder | directory entry content to be unpacked to. If this directory does not exist, it will be created. |
std::string LibArchive::libarchive_read_entry_str | ( | archive * | a, |
archive_entry * | entry ) |
Reads archived file to stirng.
If entry is not a file, empty string will be returned. In most cases you do not need to call this method directly.
a | pointer to libarchive object. |
entry | entry to be read. |
std::shared_ptr< archive > LibArchive::libarchive_read_init | ( | std::shared_ptr< ArchiveFileEntry > | fl | ) |
Initializes archive reading.
This method can return nullptr in case of error. If this method failed, you can try to use libarchive_read_init_fallback() instead.
fl | smart pointer to ArchiveFileEntry object (see createArchFile()). |
std::shared_ptr< archive > LibArchive::libarchive_read_init_fallback | ( | std::shared_ptr< ArchiveFileEntry > | fl | ) |
Initializes archive reading.
This method can return nullptr in case of error.
fl | smart pointer to ArchiveFileEntry object (see createArchFile()). |
int LibArchive::libarchive_remove_entry | ( | ArchiveRemoveEntry | rm_e, |
const std::vector< ArchEntry > & | to_remove ) |
Removes entry from archive.
rm_e | ArchiveRemoveEntry got from libarchive_remove_init(). |
to_remove | list of entries to be removed. |
ArchiveRemoveEntry LibArchive::libarchive_remove_init | ( | const std::filesystem::path & | sourcepath, |
const std::filesystem::path & | outpath ) |
Initializes archive objects for removing entries from archive.
sourcepath | absolute path to archive entries to be removed from. |
outpath | absolute path to write new archive without removed entries. |
int LibArchive::libarchive_write_data | ( | archive * | a, |
const std::string & | data ) |
Writes data to archive.
Writes raw data to archive. In most cases you do not need to call this method directly. Use libarchive_write_directory() and libarchive_write_file() methods instead.
a | pointer to libarchive object. |
data | data to be written. |
int LibArchive::libarchive_write_data_from_file | ( | archive * | a, |
const std::filesystem::path & | source ) |
Writes raw data from file to archive.
In most cases you do not need to use this method. Use writeToArchive() instead.
a | pointer to libarchive object. |
source | absolute path to source file. |
int LibArchive::libarchive_write_directory | ( | archive * | a, |
archive_entry * | entry, | ||
const std::filesystem::path & | path_in_arch, | ||
const std::filesystem::path & | source ) |
Writes directory to archive.
In most case you do not need to use this method. Use writeToArchive() instead.
a | pointer to libarchive object. |
entry | pointer to libarchive entry object. |
path_in_arch | path of entry in archive (must be relative, example: "my_directory/my_file"). |
source | absolute path to directory to be packed. |
int LibArchive::libarchive_write_file | ( | archive * | a, |
archive_entry * | entry, | ||
const std::filesystem::path & | path_in_arch, | ||
const std::filesystem::path & | source ) |
Writes file to archive.
In most case you do not need to use this method. Use writeToArchive() instead.
a | pointer to libarchive object. |
entry | pointer to libarchive entry object. |
path_in_arch | path of entry in archive (must be relative, example: "my_directory/my_file"). |
source | absolute path to directory to be packed. |
std::shared_ptr< archive > LibArchive::libarchive_write_init | ( | const std::filesystem::path & | outpath | ) |
Initializes writing to archive.
outpath | path to resulting archive. |
std::filesystem::path LibArchive::unpackByFileNameStream | ( | const std::filesystem::path & | archaddress, |
const std::filesystem::path & | outfolder, | ||
const std::string & | filename ) |
Unpacks entry content from archive.
This method is suitable for any supported types of archives (see AuxFunc::get_supported_archive_types_unpacking()). However for zip archives it is recommended to use unpackByPosition() and unpackByPositionStr() methods (they are a little bit faster).
archaddress | absolute path to archive. |
outfolder | absolute path to directory, entry to be unpacked to. If directory does not exist, it will be created. |
filename | file or directory name in archive. |
std::string LibArchive::unpackByFileNameStreamStr | ( | const std::filesystem::path & | archaddress, |
const std::string & | filename ) |
Unpacks entry content from archive.
If entry is a file, unpacks it and returns file content. If entry is a directory, returns empty string. This method is suitable for any supported types of archives (see AuxFunc::get_supported_archive_types_unpacking()). However for zip archives it is recommended to use unpackByPosition() end unpackByPositionStr() methods (they are a little bit faster).
archaddress | absolute path to archive. |
filename | file or directory name in archive. |
std::filesystem::path LibArchive::unpackByPosition | ( | const std::filesystem::path & | archaddress, |
const std::filesystem::path & | outfolder, | ||
const ArchEntry & | entry ) |
Unpacks single entry content from zip archive.
Access to entry is carried out by its absolute position in zip file. It is recommended to use this method for fast unpacking of single file or directory from zip archive.
archaddress | absolute path to zip archive. |
outfolder | absolute path to directory archive entry content to be unpacked to. If directory does not exist, it will be created. |
entry | ArchEntry object, obtained by fileNames(), fileNamesStream() or fileinfo() methods. |
std::string LibArchive::unpackByPositionStr | ( | const std::filesystem::path & | archaddress, |
const ArchEntry & | entry ) |
Unpacks single entry content from zip archive.
If entry is a file, unpacks it and returns file content. If entry is a directory, returns empty string. Access to entry is carried out by its absolute position in zip file. It is recommended to use this method for fast unpacking of single file from zip archive.
archaddress | absolute path to zip archive. |
entry | ArchEntry object, obtained by fileNames(), fileNamesStream() or fileinfo() methods. |
int LibArchive::writeToArchive | ( | std::shared_ptr< archive > | a, |
const std::filesystem::path & | source, | ||
const std::filesystem::path & | path_in_arch ) |
Writes file or directory to archive.
a | smart pointer to libarchive object (see libarchive_write_init()). |
source | absolute path to file or directory to be packed. |
path_in_arch | path of entry in archive (must be relative, example: my_directory/my_file). |