Mila
Deep Neural Network Library
Loading...
Searching...
No Matches
Mila::Dnn::Serialization::ZipSerializer Class Referenceexport

ZIP archive serializer built on miniz. More...

Inheritance diagram for Mila::Dnn::Serialization::ZipSerializer:
Mila::Dnn::Serialization::ArchiveSerializer Mila::Dnn::Serialization::Serializer

Public Member Functions

 ZipSerializer () noexcept
 Construct an empty, closed ZipSerializer.
 ZipSerializer (const ZipSerializer &)=delete
 ZipSerializer (ZipSerializer &&other) noexcept
 ~ZipSerializer ()
 Destructor ensures underlying archive is closed.
bool addData (const std::string &path, const void *data, size_t size) override
 Add a binary blob to the archive at the given internal path.
bool addData (const std::string &path, std::span< const std::byte > data)
 Add a binary blob using std::span (C++23 style).
bool addMetadata (const std::string &key, const std::string &value) override
 Store small textual metadata under the "metadata/" prefix.
bool close () override
 Finalize and close the archive.
size_t extractData (const std::string &path, std::span< std::byte > buffer)
 Extract a file using std::span (C++23 style).
size_t extractData (const std::string &path, void *data, size_t size) override
 Extract a file from the archive into a caller-provided buffer.
const std::string & getFilename () const noexcept override
 Get the filename of the currently open archive.
size_t getFileSize (const std::string &path) const override
 Get the uncompressed size of the file stored at path.
std::string getMetadata (const std::string &key) const override
 Retrieve textual metadata previously stored with addMetadata().
bool hasFile (const std::string &path) const override
 Check whether the archive contains the given path.
bool isOpen () const noexcept override
 Check if the serializer is currently open.
bool isOpenForRead () const noexcept override
 Check if the serializer is open for reading.
bool isOpenForWrite () const noexcept override
 Check if the serializer is open for writing.
std::vector< std::string > listFiles () const override
 List all files contained in the open archive.
bool open (const std::string &filename, OpenMode mode) override
 Open an archive for read or write (create/overwrite).
ZipSerializeroperator= (const ZipSerializer &)=delete
ZipSerializeroperator= (ZipSerializer &&other) noexcept

Detailed Description

ZIP archive serializer built on miniz.

Responsibilities:

  • Write and read files (binary blobs and JSON) into a ZIP archive.
  • Provide simple metadata storage under the "metadata/" prefix.

Notes:

  • Instances are not thread-safe; callers must synchronize access.
  • Move-only type; copying is disabled to prevent resource conflicts.
  • Maintains explicit open/closed state for robustness.

Constructor & Destructor Documentation

◆ ZipSerializer()

Mila::Dnn::Serialization::ZipSerializer::ZipSerializer ( )
inlinenoexcept

Construct an empty, closed ZipSerializer.

The underlying miniz archive structure is zero-initialized.

◆ ~ZipSerializer()

Mila::Dnn::Serialization::ZipSerializer::~ZipSerializer ( )
inline

Destructor ensures underlying archive is closed.

Best-effort cleanup is performed and exceptions are suppressed.

Member Function Documentation

◆ addData() [1/2]

bool Mila::Dnn::Serialization::ZipSerializer::addData ( const std::string & path,
const void * data,
size_t size )
inlinenodiscardoverridevirtual

Add a binary blob to the archive at the given internal path.

Paths are normalized to use forward slashes and stripped of leading './' or '/'.

Parameters
pathArchive-internal path (e.g., "network/weights.bin").
dataPointer to source bytes (may be null if size == 0).
sizeNumber of bytes to write.
Returns
true on success, false on failure.

Implements Mila::Dnn::Serialization::ArchiveSerializer.

◆ addData() [2/2]

bool Mila::Dnn::Serialization::ZipSerializer::addData ( const std::string & path,
std::span< const std::byte > data )
inlinenodiscard

Add a binary blob using std::span (C++23 style).

Parameters
pathArchive-internal path.
dataSpan of bytes to write.
Returns
true on success, false on failure.

◆ addMetadata()

bool Mila::Dnn::Serialization::ZipSerializer::addMetadata ( const std::string & key,
const std::string & value )
inlinenodiscardoverridevirtual

Store small textual metadata under the "metadata/" prefix.

Parameters
keyMetadata key (will be stored as "metadata/<key>").
valueMetadata value bytes.
Returns
true on success.

Implements Mila::Dnn::Serialization::ArchiveSerializer.

◆ close()

bool Mila::Dnn::Serialization::ZipSerializer::close ( )
inlinenodiscardoverridevirtual

Finalize and close the archive.

For write mode this ensures the archive is finalized and internal miniz state is released. For read mode releases reader resources.

Returns
true on success, false on internal miniz failure.

Implements Mila::Dnn::Serialization::Serializer.

◆ extractData() [1/2]

size_t Mila::Dnn::Serialization::ZipSerializer::extractData ( const std::string & path,
std::span< std::byte > buffer )
inlinenodiscard

Extract a file using std::span (C++23 style).

Parameters
pathArchive-internal path to extract.
bufferSpan to receive file contents.
Returns
Number of bytes written into the buffer, or 0 on error.

◆ extractData() [2/2]

size_t Mila::Dnn::Serialization::ZipSerializer::extractData ( const std::string & path,
void * data,
size_t size )
inlinenodiscardoverridevirtual

Extract a file from the archive into a caller-provided buffer.

Parameters
pathArchive-internal path to extract.
dataPre-allocated buffer to receive file contents.
sizeSize of the provided buffer in bytes.
Returns
Number of bytes written into the buffer, or 0 on error.

Implements Mila::Dnn::Serialization::ArchiveSerializer.

◆ getFilename()

const std::string & Mila::Dnn::Serialization::ZipSerializer::getFilename ( ) const
inlinenodiscardoverridevirtualnoexcept

Get the filename of the currently open archive.

Returns
Filename, or empty string if no archive is open.

Implements Mila::Dnn::Serialization::Serializer.

◆ getFileSize()

size_t Mila::Dnn::Serialization::ZipSerializer::getFileSize ( const std::string & path) const
inlinenodiscardoverridevirtual

Get the uncompressed size of the file stored at path.

Parameters
pathArchive-internal path.
Returns
File size in bytes, or 0 if the file is missing or an error occurred.

Implements Mila::Dnn::Serialization::ArchiveSerializer.

◆ getMetadata()

std::string Mila::Dnn::Serialization::ZipSerializer::getMetadata ( const std::string & key) const
inlinenodiscardoverridevirtual

Retrieve textual metadata previously stored with addMetadata().

Parameters
keyMetadata key.
Returns
Metadata value string, or empty string if missing or on error.

Implements Mila::Dnn::Serialization::ArchiveSerializer.

◆ hasFile()

bool Mila::Dnn::Serialization::ZipSerializer::hasFile ( const std::string & path) const
inlinenodiscardoverridevirtual

Check whether the archive contains the given path.

Parameters
pathArchive-internal path to query.
Returns
true if the file exists and archive is open for read.

Implements Mila::Dnn::Serialization::ArchiveSerializer.

◆ isOpen()

bool Mila::Dnn::Serialization::ZipSerializer::isOpen ( ) const
inlinenodiscardoverridevirtualnoexcept

Check if the serializer is currently open.

Returns
true if open for reading or writing, false if closed.

Implements Mila::Dnn::Serialization::Serializer.

◆ isOpenForRead()

bool Mila::Dnn::Serialization::ZipSerializer::isOpenForRead ( ) const
inlinenodiscardoverridevirtualnoexcept

Check if the serializer is open for reading.

Returns
true if open for reading.

Implements Mila::Dnn::Serialization::Serializer.

◆ isOpenForWrite()

bool Mila::Dnn::Serialization::ZipSerializer::isOpenForWrite ( ) const
inlinenodiscardoverridevirtualnoexcept

Check if the serializer is open for writing.

Returns
true if open for writing.

Implements Mila::Dnn::Serialization::Serializer.

◆ listFiles()

std::vector< std::string > Mila::Dnn::Serialization::ZipSerializer::listFiles ( ) const
inlinenodiscardoverridevirtual

List all files contained in the open archive.

Returns
Vector of archive-internal file paths. Empty if archive is closed or opened for writing.

Implements Mila::Dnn::Serialization::ArchiveSerializer.

◆ open()

bool Mila::Dnn::Serialization::ZipSerializer::open ( const std::string & filename,
OpenMode mode )
inlinenodiscardoverridevirtual

Open an archive for read or write (create/overwrite).

Combines previous openForRead/openForWrite into one API using ModelSerializer::OpenMode to select the behavior.

Parameters
filenameFilesystem path to open.
modeOpenMode::Read or OpenMode::Write.
Returns
true on success, false on failure.

Implements Mila::Dnn::Serialization::Serializer.


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