coral
A C++ library for distributed co-simulation
Public Member Functions | List of all members
coral::util::zip::Archive Class Reference

A class for reading ZIP archives. More...

#include "coral/util/zip.hpp"

Public Member Functions

 Archive () CORAL_NOEXCEPT
 Default constructor; does not associate the object with an archive file.
 
 Archive (const boost::filesystem::path &path)
 Constructor which opens a ZIP archive. More...
 
 Archive (Archive &&) CORAL_NOEXCEPT
 Move constructor.
 
Archiveoperator= (Archive &&) CORAL_NOEXCEPT
 Move assignment operator.
 
 ~Archive () CORAL_NOEXCEPT
 Destructor; calls Discard().
 
void Open (const boost::filesystem::path &path)
 Opens a ZIP archive. More...
 
void Discard () CORAL_NOEXCEPT
 Closes the archive. More...
 
bool IsOpen () const CORAL_NOEXCEPT
 Returns whether this object refers to an open ZIP archive.
 
std::uint64_t EntryCount () const
 Returns the number of entries in the archive. More...
 
EntryIndex FindEntry (const std::string &name) const
 Finds an entry by name. More...
 
std::string EntryName (EntryIndex index) const
 Returns the name of an archive entry. More...
 
bool IsDirEntry (EntryIndex index) const
 Returns whether an archive entry is a directory. More...
 
void ExtractAll (const boost::filesystem::path &targetDir) const
 Extracts the entire contents of the archive. More...
 
boost::filesystem::path ExtractFileTo (EntryIndex index, const boost::filesystem::path &targetDir) const
 Extracts a single file from the archive, placing it in a specific target directory. More...
 

Detailed Description

A class for reading ZIP archives.

Currently, only a limited set of reading operations are supported, and no writing/modification operations.

A ZIP archive is organised as a number of entries, where each entry is a file or a directory. Each entry has a unique integer index, and the indices run consecutively from 0 through EntryCount()-1. For example, a file with 2 file entries and 1 directory entry, i.e. EntryCount() == 3, could look like this:

Index  Name
-----  ----------------
    0  readme.txt
    1  images/
    2  images/photo.jpg

Constructor & Destructor Documentation

coral::util::zip::Archive::Archive ( const boost::filesystem::path &  path)

Constructor which opens a ZIP archive.

This is equivalent to default construction followed by a call to Open().

Parameters
[in]pathThe path to a ZIP archive file.
Exceptions
coral::util::zip::ExceptionIf there was an error opening the archive.

Member Function Documentation

void coral::util::zip::Archive::Discard ( )

Closes the archive.

If no archive is open, this function has no effect.

std::uint64_t coral::util::zip::Archive::EntryCount ( ) const

Returns the number of entries in the archive.

This includes both files and directories.

Precondition
IsOpen() == true
std::string coral::util::zip::Archive::EntryName ( EntryIndex  index) const

Returns the name of an archive entry.

Parameters
[in]indexAn archive entry index in the range [0,EntryCount()).
Returns
The full name of the entry with the given index.
Exceptions
coral::util::zip::ExceptionIf there was an error accessing the archive.
Precondition
IsOpen() == true
void coral::util::zip::Archive::ExtractAll ( const boost::filesystem::path &  targetDir) const

Extracts the entire contents of the archive.

This will extract all entries in the archive to the given target directory, recreating the subdirectory structure in the archive.

Parameters
[in]targetDirThe directory to which the files should be extracted.
Exceptions
coral::util::zip::ExceptionIf there was an error accessing the archive.
std::ios_base::failureOn I/O error.
Precondition
IsOpen() == true
boost::filesystem::path coral::util::zip::Archive::ExtractFileTo ( EntryIndex  index,
const boost::filesystem::path &  targetDir 
) const

Extracts a single file from the archive, placing it in a specific target directory.

This ignores the directory structure inside the archive, i.e. the file will always be created directly under the given target directory.

Parameters
[in]indexAn archive entry index in the range [0,EntryCount()).
[in]targetDirThe directory to which the file should be extracted.
Returns
The full path to the extracted file, i.e. the base name of the archive entry appended to the target directory.
Exceptions
coral::util::zip::ExceptionIf there was an error accessing the archive.
std::ios_base::failureOn I/O error.
Precondition
IsOpen() == true
EntryIndex coral::util::zip::Archive::FindEntry ( const std::string name) const

Finds an entry by name.

Parameters
[in]nameThe full name of a file or directory in the archive. The search is case sensitive, and directory names must end with a forward slash (/).
Returns
The index of the entry with the given name, or INVALID_ENTRY_INDEX if no such entry was found.
Exceptions
coral::util::zip::ExceptionIf there was an error accessing the archive.
Precondition
IsOpen() == true
bool coral::util::zip::Archive::IsDirEntry ( EntryIndex  index) const

Returns whether an archive entry is a directory.

This returns true if and only if the entry has zero size, has a CRC of zero, and a name which ends with a forward slash (/).

Parameters
[in]indexAn archive entry index in the range [0,EntryCount()).
Returns
Whether the entry with the given index is a directory.
Exceptions
coral::util::zip::ExceptionIf there was an error accessing the archive.
Precondition
IsOpen() == true
void coral::util::zip::Archive::Open ( const boost::filesystem::path &  path)

Opens a ZIP archive.

Parameters
[in]pathThe path to a ZIP archive file.
Exceptions
coral::util::zip::ExceptionIf there was an error opening the archive.
Precondition
IsOpen() == false

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