class mrpt::io::CCompressedInputStream
Overview
Transparently reads from a compressed file, automatically detecting the compression format from the file magic signature.
Supports:
Uncompressed files
Gzip compressed files (.gz)
Zstandard compressed files (.zst)
The compression type is detected automatically when opening the file.
See also:
CFileInputStream, CCompressedOutputStream
#include <mrpt/io/CCompressedInputStream.h> class CCompressedInputStream: public mrpt::io::CStream { public: // structs struct Impl; // construction CCompressedInputStream(); CCompressedInputStream(const std::string& fileName); CCompressedInputStream(const CCompressedInputStream&); // methods CCompressedInputStream& operator = (const CCompressedInputStream&); virtual std::string getStreamDescription() const; bool open(const std::string& fileName, mrpt::optional_ref<std::string> error_msg = std::nullopt); void close(); bool fileOpenCorrectly() const; bool is_open(); bool checkEOF(); std::string filePathAtUse() const; CompressionType getCompressionType() const; virtual uint64_t getTotalBytesCount() const; virtual uint64_t getPosition() const; uint64_t getUncompressedSize() const; double getCompressionRatio() const; uint64_t getUncompressedPosition() const; virtual uint64_t Seek(int64_t Offset, CStream::TSeekOrigin Origin); 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
CCompressedInputStream()
Constructor without open.
CCompressedInputStream(const std::string& fileName)
Constructor and open.
Parameters:
fileName |
The file to be opened in this stream |
std::exception |
If there’s an error opening the file. |
Methods
virtual std::string getStreamDescription() const
Returns a human-friendly description of the stream, e.g.
a filename.
bool open(const std::string& fileName, mrpt::optional_ref<std::string> error_msg = std::nullopt)
Opens the file for reading.
Automatically detects compression format.
Parameters:
fileName |
The file to be opened in this stream |
error_msg |
Optional output parameter for error message |
Returns:
false if there’s an error opening the file, true otherwise
void close()
Closes 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.
bool checkEOF()
Will be true if EOF has been already reached.
std::string filePathAtUse() const
Returns the path of the filename passed to open(), or empty if none.
CompressionType getCompressionType() const
Returns the detected compression type for the opened file.
virtual uint64_t getTotalBytesCount() const
Method for getting the total number of compressed bytes in the file (the physical size of the file on disk).
virtual uint64_t getPosition() const
Method for getting the current cursor position in the compressed data, where 0 is the first byte and TotalBytesCount-1 the last one.
For uncompressed files, this equals the position in uncompressed data. For compressed files, this is an approximation based on the compressed stream position.
uint64_t getUncompressedSize() const
Attempts to estimate the total uncompressed size of the file.
For Gzip files, this reads the size from the footer if available. For Zstd files with frame content size, returns that value. For uncompressed files, returns the actual file size. If the exact size is not available, returns an estimate based on the compression ratio measured so far (compressed bytes read vs uncompressed bytes served). This estimate improves as more data is read. Returns 0 only if no data has been read yet and no size hint is available.
Returns:
Estimated uncompressed size, or 0 if it cannot be determined.
double getCompressionRatio() const
Returns the compression ratio measured so far.
This is the ratio: uncompressed_bytes / compressed_bytes. A ratio of 3.0 means the data compressed to 1/3 of its original size.
Returns:
Ratio of uncompressed to compressed bytes, or 0.0 if no data read.
uint64_t getUncompressedPosition() const
Estimates the current position in the uncompressed data stream.
This is an estimate and may not be accurate for all compression formats.
Returns:
Estimated position in uncompressed data, or 0 if it cannot be determined.
virtual uint64_t Seek(int64_t Offset, CStream::TSeekOrigin Origin)
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.