MLBookProc 1.1
 
Loading...
Searching...
No Matches
LibArchive Class Reference

The LibArchive class. More...

#include <LibArchive.h>

Inheritance diagram for LibArchive:

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< ArchiveFileEntrycreateArchFile (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.
 

Detailed Description

The LibArchive class.

This class contains various methods for archives processing. Based on libarchive library.

Constructor & Destructor Documentation

◆ LibArchive()

LibArchive::LibArchive ( const std::shared_ptr< AuxFunc > & af)

LibArchive constructor.

Parameters
afsmart pointer to AuxFunc object.

Member Function Documentation

◆ createArchFile()

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.

Parameters
archaddressabsolute path to archive to be read.
positionposition in archive file to start reading from.
Returns
Smart pointer to ArchiveFileEntry object.

◆ fileinfo()

ArchEntry LibArchive::fileinfo ( const std::filesystem::path & address,
const std::string & filename )

Returns ArchEntry for particular file or directory in archive.

In case of any error ArchEntry filename will be empty.

Parameters
addressabsolute path to archive.
filenamepath in archive.
Returns
ArchEntry object.

◆ fileNames()

int LibArchive::fileNames ( const std::filesystem::path & filepath,
std::vector< ArchEntry > & filenames )

Lists all entries in archive file.

Warning
This method is suitable for zip archives only. For other archive types behavior is undefined.
Parameters
filepathabsolute path to zip archive.
filenamesvector for results.
Returns
1 in case of success, -1 in case of error, 0 in case archive file has not been opened.

◆ fileNamesStream()

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.

Parameters
addressabsolute path to archive.
filenamesvector of results.
Returns
in case of succes returns ARCHIVE_OK, libarchive error codes will be returned otherwise (see archive.h file for details).

◆ libarchive_error()

void LibArchive::libarchive_error ( const std::shared_ptr< archive > & a,
const std::string & message,
const int & error_number )

Prints libarchive error messages.

Parameters
asmart pointer to libarchive object.
messageextra text if needed (will be shown before libarchive error text).
error_numbererror code.

◆ libarchive_packing() [1/2]

int LibArchive::libarchive_packing ( const std::filesystem::path & sourcepath,
const std::filesystem::path & outpath )

Packs file or directory into archive.

Parameters
sourcepathabsolute path to file or directory to be packed.
outpathabsolute path to resulting archive.
Returns
-100 in case if sourcepath does not exist, -200 in case of error on libarchive object creation, ARCHIVE_OK in case of success, libarchive error code otherwise (see libarchive archive.h file for details).

◆ libarchive_packing() [2/2]

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.

Parameters
asmart pointer to libarchive object (see libarchive_write_init()).
sourcepathabsolute path to file or directory to be packed.
rename_sourceif set to true, source name will be replaced for new_source_name inside the archive.
new_source_namenew name to be used inside the archive. Should be UTF-8 string.
Returns
-100 in case if sourcepath does not exist, -200 in case of error on libarchive object creation, ARCHIVE_OK in case of success, libarchive error code otherwise (see libarchive archive.h file for details).

◆ libarchive_read_entry()

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.

Parameters
apointer to libarchive object.
entrypointer to libarchive entry object.
outfolderdirectory entry content to be unpacked to. If this directory does not exist, it will be created.
Returns
Absolute path to unpacked file or directory.

◆ libarchive_read_entry_str()

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.

Parameters
apointer to libarchive object.
entryentry to be read.
Returns
File content.

◆ libarchive_read_init()

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.

Parameters
flsmart pointer to ArchiveFileEntry object (see createArchFile()).
Returns
Smart pointer to libarchive object (can be nullptr in case of any error).

◆ libarchive_read_init_fallback()

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.

Parameters
flsmart pointer to ArchiveFileEntry object (see createArchFile()).
Returns
Smart pointer to libarchive object (can be nullptr in case of any error).

◆ libarchive_remove_entry()

int LibArchive::libarchive_remove_entry ( ArchiveRemoveEntry rm_e,
const std::vector< ArchEntry > & to_remove )

Removes entry from archive.

Parameters
rm_eArchiveRemoveEntry got from libarchive_remove_init().
to_removelist of entries to be removed.
Returns
ARCHIVE_OK in case of success, libarchive error code otherwise (see libarchive archive.h for details).

◆ libarchive_remove_init()

ArchiveRemoveEntry LibArchive::libarchive_remove_init ( const std::filesystem::path & sourcepath,
const std::filesystem::path & outpath )

Initializes archive objects for removing entries from archive.

Parameters
sourcepathabsolute path to archive entries to be removed from.
outpathabsolute path to write new archive without removed entries.
Returns
ArchiveRemoveEntry object.

◆ libarchive_write_data()

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.

Parameters
apointer to libarchive object.
datadata to be written.
Returns
libarchive error code (see libarchive archive.h for details).

◆ libarchive_write_data_from_file()

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.

Parameters
apointer to libarchive object.
sourceabsolute path to source file.
Returns
libarchive error code (see libarchive archive.h for details).

◆ libarchive_write_directory()

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.

Note
This method resolves symbolic links and processes them as underlying objects.
Parameters
apointer to libarchive object.
entrypointer to libarchive entry object.
path_in_archpath of entry in archive (must be relative, example: "my_directory/my_file").
sourceabsolute path to directory to be packed.
Returns
ARCHIVE_OK in case of success, libarchive error code otherwise (see libarchive archive.h for details).

◆ libarchive_write_file()

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.

Parameters
apointer to libarchive object.
entrypointer to libarchive entry object.
path_in_archpath of entry in archive (must be relative, example: "my_directory/my_file").
sourceabsolute path to directory to be packed.
Returns
ARCHIVE_OK in case of success, libarchive error code otherwise (see libarchive archive.h for details).

◆ libarchive_write_init()

std::shared_ptr< archive > LibArchive::libarchive_write_init ( const std::filesystem::path & outpath)

Initializes writing to archive.

Warning
If resulting archive path already exists, it will be overwritten.
Parameters
outpathpath to resulting archive.
Returns
Smart pointer to libarchive object (can be nullptr in case of any error).

◆ unpackByFileNameStream()

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).

Parameters
archaddressabsolute path to archive.
outfolderabsolute path to directory, entry to be unpacked to. If directory does not exist, it will be created.
filenamefile or directory name in archive.
Returns
Absolute path to unpacked file or directory.

◆ unpackByFileNameStreamStr()

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).

Parameters
archaddressabsolute path to archive.
filenamefile or directory name in archive.
Returns
Unpacked file content or empty string.

◆ unpackByPosition()

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.

Warning
This method should be used for zip archives only!
Parameters
archaddressabsolute path to zip archive.
outfolderabsolute path to directory archive entry content to be unpacked to. If directory does not exist, it will be created.
entryArchEntry object, obtained by fileNames(), fileNamesStream() or fileinfo() methods.
Returns
Absolute path to unpacked file or directory.

◆ unpackByPositionStr()

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.

Warning
This method should be used for zip archives only!
Parameters
archaddressabsolute path to zip archive.
entryArchEntry object, obtained by fileNames(), fileNamesStream() or fileinfo() methods.
Returns
Unpacked file content or empty string.

◆ writeToArchive()

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.

Note
This method resolves symbolic links and processes them as underlying objects.
Parameters
asmart pointer to libarchive object (see libarchive_write_init()).
sourceabsolute path to file or directory to be packed.
path_in_archpath of entry in archive (must be relative, example: my_directory/my_file).
Returns
ARCHIVE_OK in case of success, libarchive error code otherwise (see libarchive archive.h for details).