class mrpt::io::CCompressedOutputStream

Overview

Saves data to a file with optional transparent compression.

Supports:

  • No compression (raw binary output)

  • Gzip compression

  • Zstandard compression

The compression type and level are specified via CompressionOptions.

See also:

CFileOutputStream, CCompressedInputStream

#include <mrpt/io/CCompressedOutputStream.h>

class CCompressedOutputStream: public mrpt::io::CStream
{
public:
    // structs

    struct Impl;

    // construction

    CCompressedOutputStream(const std::string& fileName, const OpenMode mode = OpenMode::TRUNCATE, const CompressionOptions& options = CompressionOptions());
    CCompressedOutputStream();
    CCompressedOutputStream(const CCompressedOutputStream&);

    // methods

    CCompressedOutputStream& operator = (const CCompressedOutputStream&);
    virtual std::string getStreamDescription() const;

    bool open(
        const std::string& fileName,
        const CompressionOptions& options = CompressionOptions(),
        mrpt::optional_ref<std::string> error_msg = std::nullopt,
        const OpenMode mode = OpenMode::TRUNCATE
        );

    void close();
    bool fileOpenCorrectly() const;
    bool is_open();
    virtual uint64_t getPosition() const;
    std::string filePathAtUse() const;
    CompressionType getCompressionType() const;
    virtual uint64_t Seek(int64_t Offset, CStream::TSeekOrigin Origin);
    virtual uint64_t getTotalBytesCount() const;
    virtual size_t Read(void* Buffer, size_t Count);
    virtual size_t Write(const void* Buffer, size_t Count);
};

Inherited Members

public:
    // methods

    virtual size_t Read(void* Buffer, size_t Count) = 0;
    virtual size_t Write(const void* Buffer, size_t Count) = 0;
    virtual uint64_t getTotalBytesCount() const = 0;
    virtual uint64_t getPosition() const = 0;

Construction

CCompressedOutputStream(
    const std::string& fileName,
    const OpenMode mode = OpenMode::TRUNCATE,
    const CompressionOptions& options = CompressionOptions()
    )

Constructor: opens an output file with the given compression options.

Parameters:

fileName

The file to be opened in this stream

mode

Specify whether to truncate/create the file, or to append at the end if it exists. Note: append mode may not work correctly with all compression formats.

options

Compression type and level

std::exception

if the file cannot be opened.

CCompressedOutputStream()

Constructor, without opening the file.

See also:

open

Methods

virtual std::string getStreamDescription() const

Returns a human-friendly description of the stream, e.g.

a filename.

bool open(
    const std::string& fileName,
    const CompressionOptions& options = CompressionOptions(),
    mrpt::optional_ref<std::string> error_msg = std::nullopt,
    const OpenMode mode = OpenMode::TRUNCATE
    )

Open a file for writing with the specified compression options.

Parameters:

fileName

The file to be opened in this stream

options

Compression type and level

error_msg

Optional output parameter for error message

mode

Specify whether to truncate or append

Returns:

true on success, false on any error.

void close()

Close the file.

bool fileOpenCorrectly() const

Returns true if the file was opened without errors.

bool is_open()

Returns true if the file was opened without errors.

virtual uint64_t getPosition() const

Method for getting the current cursor position in the compressed stream, where 0 is the first byte and TotalBytesCount-1 the last one.

For compressed files, this represents the position in the compressed output. For uncompressed files, it equals the number of bytes written.

std::string filePathAtUse() const

Returns the path of the filename passed to open(), or empty if none.

CompressionType getCompressionType() const

Returns the compression type being used.

virtual uint64_t Seek(int64_t Offset, CStream::TSeekOrigin Origin)

This method is not implemented in this class.

virtual uint64_t getTotalBytesCount() const

This method is not implemented in this class.

virtual size_t Read(void* Buffer, size_t Count)

Introduces a pure virtual method responsible for reading from the stream.

virtual size_t Write(const void* Buffer, size_t Count)

Introduces a pure virtual method responsible for writing to the stream.

Write attempts to write up to Count bytes to Buffer, and returns the number of bytes actually written.