Lightweight 0.20251202.0
Loading...
Searching...
No Matches
Lightweight::Zip::ZipArchive Class Referencefinal

#include <ZipArchive.hpp>

Public Member Functions

 ZipArchive (ZipArchive const &)=delete
 
ZipArchiveoperator= (ZipArchive const &)=delete
 
 ZipArchive (ZipArchive &&other) noexcept
 Move constructor. Transfers ownership of the archive handle.
 
ZipArchiveoperator= (ZipArchive &&other) noexcept
 Move assignment operator. Transfers ownership of the archive handle.
 
 ~ZipArchive () noexcept
 Destructor. Discards the archive if not explicitly closed.
 
bool IsOpen () const noexcept
 
zip_int64_t EntryCount () const noexcept
 
zip_t * NativeHandle () const noexcept
 
std::optional< zip_int64_t > LocateEntry (std::string_view name) const
 
std::expected< EntryInfo, ZipErrorGetEntryInfo (zip_int64_t index) const
 
std::expected< ZipEntry, ZipErrorOpenEntry (zip_int64_t index) const
 
std::expected< std::vector< uint8_t >, ZipErrorReadEntry (zip_int64_t index) const
 
std::expected< std::string, ZipErrorReadEntryAsString (zip_int64_t index) const
 
std::expected< zip_int64_t, ZipErrorAddBuffer (std::string_view name, std::span< uint8_t const > data, CompressionMethod method=CompressionMethod::Deflate, uint32_t level=6)
 
std::expected< zip_int64_t, ZipErrorAddString (std::string_view name, std::string_view content, CompressionMethod method=CompressionMethod::Deflate, uint32_t level=6)
 
void ForEachEntry (std::function< bool(zip_int64_t, std::string_view, zip_uint64_t)> const &callback) const
 
std::vector< EntryInfoGetAllEntries () const
 
std::expected< void, ZipErrorClose ()
 
void Discard () noexcept
 

Static Public Member Functions

static std::expected< ZipArchive, ZipErrorOpen (std::filesystem::path const &path)
 
static std::expected< ZipArchive, ZipErrorCreate (std::filesystem::path const &path)
 
static std::expected< ZipArchive, ZipErrorCreateOrTruncate (std::filesystem::path const &path)
 

Detailed Description

RAII wrapper for a ZIP archive (zip_t*).

This class provides safe, scoped access to a ZIP archive file. The archive is automatically closed when the ZipArchive object is destroyed.

ZipArchive objects are non-copyable but movable.

// Writing
auto archiveResult = ZipArchive::CreateOrTruncate("backup.zip");
if (!archiveResult) {
std::cerr << "Failed: " << archiveResult.error().message << "\n";
return;
}
auto& archive = *archiveResult;
archive.AddString("metadata.json", jsonStr);
archive.AddBuffer("data.bin", binaryData);
archive.Close();
// Reading with functional chaining
ZipArchive::Open("backup.zip")
.and_then([](ZipArchive& ar) { return ar.ReadEntryAsString(0); })
.transform([](std::string const& content) { /* process */ })
.or_else([](ZipError const& e) { std::cerr << e.message; });
static std::expected< ZipArchive, ZipError > CreateOrTruncate(std::filesystem::path const &path)
std::expected< std::string, ZipError > ReadEntryAsString(zip_int64_t index) const
static std::expected< ZipArchive, ZipError > Open(std::filesystem::path const &path)

Definition at line 85 of file ZipArchive.hpp.

Member Function Documentation

◆ Open()

static std::expected< ZipArchive, ZipError > Lightweight::Zip::ZipArchive::Open ( std::filesystem::path const &  path)
static

Opens an existing ZIP archive for reading.

Parameters
pathThe path to the archive file.
Returns
The opened archive, or a ZipError on failure.

◆ Create()

static std::expected< ZipArchive, ZipError > Lightweight::Zip::ZipArchive::Create ( std::filesystem::path const &  path)
static

Creates a new ZIP archive.

Parameters
pathThe path for the new archive file.
Returns
The created archive, or a ZipError if the file already exists or creation fails.

◆ CreateOrTruncate()

static std::expected< ZipArchive, ZipError > Lightweight::Zip::ZipArchive::CreateOrTruncate ( std::filesystem::path const &  path)
static

Creates a new ZIP archive, truncating any existing file.

Parameters
pathThe path for the archive file.
Returns
The created archive, or a ZipError on failure.

◆ IsOpen()

bool Lightweight::Zip::ZipArchive::IsOpen ( ) const
noexcept

Checks if the archive is currently open.

Returns
true if the archive is open, false otherwise.

◆ EntryCount()

zip_int64_t Lightweight::Zip::ZipArchive::EntryCount ( ) const
noexcept

Returns the number of entries in the archive.

Returns
The entry count, or -1 if the archive is not open.

◆ NativeHandle()

zip_t * Lightweight::Zip::ZipArchive::NativeHandle ( ) const
noexcept

Returns the native libzip handle.

Returns
The zip_t* handle, or nullptr if not open.

◆ LocateEntry()

std::optional< zip_int64_t > Lightweight::Zip::ZipArchive::LocateEntry ( std::string_view  name) const

Locates an entry by name.

Parameters
nameThe name of the entry to find.
Returns
The entry index, or std::nullopt if not found.

◆ GetEntryInfo()

std::expected< EntryInfo, ZipError > Lightweight::Zip::ZipArchive::GetEntryInfo ( zip_int64_t  index) const

Gets information about an entry by index.

Parameters
indexThe entry index.
Returns
Entry information, or a ZipError if the index is invalid.

◆ OpenEntry()

std::expected< ZipEntry, ZipError > Lightweight::Zip::ZipArchive::OpenEntry ( zip_int64_t  index) const

Opens an entry for streaming read access.

Parameters
indexThe entry index.
Returns
A ZipEntry for reading, or a ZipError on failure.

◆ ReadEntry()

std::expected< std::vector< uint8_t >, ZipError > Lightweight::Zip::ZipArchive::ReadEntry ( zip_int64_t  index) const

Reads an entire entry as binary data.

Parameters
indexThe entry index.
Returns
The entry contents as a byte vector, or a ZipError on failure.

◆ ReadEntryAsString()

std::expected< std::string, ZipError > Lightweight::Zip::ZipArchive::ReadEntryAsString ( zip_int64_t  index) const

Reads an entire entry as a string.

Parameters
indexThe entry index.
Returns
The entry contents as a string, or a ZipError on failure.

◆ AddBuffer()

std::expected< zip_int64_t, ZipError > Lightweight::Zip::ZipArchive::AddBuffer ( std::string_view  name,
std::span< uint8_t const >  data,
CompressionMethod  method = CompressionMethod::Deflate,
uint32_t  level = 6 
)

Adds binary data as a new entry.

Parameters
nameThe name for the new entry.
dataThe binary data to add.
methodThe compression method to use.
levelThe compression level (0-9, where 0 is no compression).
Returns
The index of the new entry, or a ZipError on failure.

◆ AddString()

std::expected< zip_int64_t, ZipError > Lightweight::Zip::ZipArchive::AddString ( std::string_view  name,
std::string_view  content,
CompressionMethod  method = CompressionMethod::Deflate,
uint32_t  level = 6 
)

Adds string content as a new entry.

Parameters
nameThe name for the new entry.
contentThe string content to add.
methodThe compression method to use.
levelThe compression level (0-9, where 0 is no compression).
Returns
The index of the new entry, or a ZipError on failure.

◆ ForEachEntry()

void Lightweight::Zip::ZipArchive::ForEachEntry ( std::function< bool(zip_int64_t, std::string_view, zip_uint64_t)> const &  callback) const

Iterates over all entries in the archive.

Parameters
callbackFunction called for each entry. Return false to stop iteration. Parameters: (index, name, uncompressed_size)

◆ GetAllEntries()

std::vector< EntryInfo > Lightweight::Zip::ZipArchive::GetAllEntries ( ) const

Gets information about all entries in the archive.

Returns
A vector of EntryInfo for all entries.

◆ Close()

std::expected< void, ZipError > Lightweight::Zip::ZipArchive::Close ( )

Closes the archive, writing all changes to disk.

Returns
void on success, or a ZipError if closing fails.

◆ Discard()

void Lightweight::Zip::ZipArchive::Discard ( )
noexcept

Discards the archive without writing changes.

Use this to abandon modifications without saving them.


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