crawlserv++  [under development]
Application for crawling and analyzing textual content of websites.
crawlservpp::Wrapper::ZipArchive Class Reference

RAII wrapper for ZIP archives used by libzip. More...

#include <ZipArchive.hpp>

Construction and Destruction

 ZipArchive ()
 Constructor creating an archive from an empty source. More...
 
virtual ~ZipArchive ()
 Destructor clearing the archive if necessary. More...
 

Getters

struct zip * get () noexcept
 Gets a pointer to the underlying archive. More...
 
const struct zip * getc () const noexcept
 Gets a const pointer to the underlying archive. More...
 
bool valid () const noexcept
 Checks whether the underlying archive is valid. More...
 
std::string getError ()
 Get the last occurred error as string. More...
 

Archive Functionality

bool addEmptyDirectory (const std::string &name)
 Adds an empty directory to the archive. More...
 
bool addFile (const std::string &fileName, const std::string &content, bool overwrite)
 Adds a file to the archive. More...
 

Cleanup

void close () noexcept
 Closes the underlying archive if necessary. More...
 
void close (std::string &dumpTo)
 Closes the underlying archive and dumps its contents. More...
 

Copy and Move

The class is not copyable, only moveable.

 ZipArchive (ZipArchive &)=delete
 Deleted copy constructor. More...
 
ZipArchiveoperator= (ZipArchive &)=delete
 Deleted copy assignment operator. More...
 
 ZipArchive (ZipArchive &&other) noexcept
 Move constructor. More...
 
ZipArchiveoperator= (ZipArchive &&other) noexcept
 Move assignment operator. More...
 

Detailed Description

RAII wrapper for ZIP archives used by libzip.

Creates the archive on construction and closes it on destruction, avoiding memory leaks.

This class is used exclusively by functions in the Data::Compression::Zip namespace.

For more information about the libzip library used, visit its website.

Note
The class does not own the underlying pointer, but takes care of its deletion via API call.

Constructor & Destructor Documentation

◆ ZipArchive() [1/3]

crawlservpp::Wrapper::ZipArchive::ZipArchive ( )
inline

◆ ~ZipArchive()

crawlservpp::Wrapper::ZipArchive::~ZipArchive ( )
inlinevirtual

Destructor clearing the archive if necessary.

References close().

◆ ZipArchive() [2/3]

crawlservpp::Wrapper::ZipArchive::ZipArchive ( ZipArchive )
delete

Deleted copy constructor.

◆ ZipArchive() [3/3]

crawlservpp::Wrapper::ZipArchive::ZipArchive ( ZipArchive &&  other)
inlinenoexcept

Move constructor.

Moves the archive from the specified location into this instance of the class.

Note
The other archive will be invalidated by this move.
Parameters
otherThe archive to move from.
See also
valid

Member Function Documentation

◆ addEmptyDirectory()

bool crawlservpp::Wrapper::ZipArchive::addEmptyDirectory ( const std::string &  name)
inline

Adds an empty directory to the archive.

Parameters
nameThe name of the directory to add to the archive.
Returns
True on success. False, if the archive is invalid or the adding failed. If it failed, use getError() to find out why.
Note
It is not necessary to add directories that contain files before adding these files.
See also
addFile

◆ addFile()

bool crawlservpp::Wrapper::ZipArchive::addFile ( const std::string &  name,
const std::string &  content,
bool  overwrite 
)
inline

Adds a file to the archive.

Parameters
nameThe name of the file to add to the archive.
contentThe content of the file to add to the archive.
overwriteSpecifies whether to overwrite an already existing file with the same name.
Returns
True on success. False, if the archive is invalid or the adding failed. If it failed, use getError() to find out why.
Note
Automatically creates the directories contained in the file names.

Referenced by crawlservpp::Data::Compression::Zip::compress().

◆ close() [1/2]

void crawlservpp::Wrapper::ZipArchive::close ( )
inlinenoexcept

Closes the underlying archive if necessary.

The archive will be invalid and valid() will return false afterwards.

Note
Does nothing if the underlying archive is not valid.
See also
valid

Referenced by close(), crawlservpp::Data::Compression::Zip::compress(), operator=(), and ~ZipArchive().

◆ close() [2/2]

void crawlservpp::Wrapper::ZipArchive::close ( std::string &  dumpTo)
inline

Closes the underlying archive and dumps its contents.

Parameters
dumpToReference to a string into which the contents of the archive will be written.
Note
Does nothing if the underlying archive is not valid.
See also
valid

References close(), crawlservpp::Wrapper::ZipSource::get(), and crawlservpp::Wrapper::ZipSource::valid().

◆ get()

struct zip * crawlservpp::Wrapper::ZipArchive::get ( )
noexcept

Gets a pointer to the underlying archive.

Returns
A pointer to the underlying archive or nullptr if none is set.

◆ getc()

const struct zip * crawlservpp::Wrapper::ZipArchive::getc ( ) const
noexcept

Gets a const pointer to the underlying archive.

Returns
A constant pointer to the underlying archive or nullptr if none is set.

◆ getError()

std::string crawlservpp::Wrapper::ZipArchive::getError ( )
inline

Get the last occurred error as string.

Returns
A string containing a description of the last error that occurred while calling the libzip API.

Referenced by crawlservpp::Data::Compression::Zip::compress().

◆ operator=() [1/2]

ZipArchive& crawlservpp::Wrapper::ZipArchive::operator= ( ZipArchive )
delete

Deleted copy assignment operator.

◆ operator=() [2/2]

ZipArchive & crawlservpp::Wrapper::ZipArchive::operator= ( ZipArchive &&  other)
inlinenoexcept

Move assignment operator.

Moves the archive from the specified location into this instance of the class.

Note
The other archive will be invalidated by this move.
Nothing will be done if used on itself.
Parameters
otherThe archive to move from.
Returns
A reference to the instance containing the archive after moving (i.e. *this).
See also
valid

References close().

◆ valid()

bool crawlservpp::Wrapper::ZipArchive::valid ( ) const
inlinenoexcept

Checks whether the underlying archive is valid.

Returns
True, if the archive is valid, i.e. a pointer has been set. False otherwise.
See also
clear

Referenced by crawlservpp::Data::Compression::Zip::compress().


The documentation for this class was generated from the following file: