Class CCompressedInputStream
Defined in File CCompressedInputStream.h
Inheritance Relationships
Base Type
public mrpt::io::CStream(Class CStream)
Class Documentation
-
class CCompressedInputStream : public mrpt::io::CStream
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
Public Functions
-
CCompressedInputStream()
Constructor without open
-
explicit CCompressedInputStream(const std::string &fileName)
Constructor and open
- Parameters:
fileName – The file to be opened in this stream
- Throws:
std::exception – If there’s an error opening the file.
-
CCompressedInputStream(const CCompressedInputStream&) = delete
-
CCompressedInputStream &operator=(const CCompressedInputStream&) = delete
-
~CCompressedInputStream() override
Destructor
-
virtual std::string getStreamDescription() const override
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.
-
inline 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 override
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 override
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.
Note
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.
Note
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.
Note
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) override
This method is not implemented in this class
-
virtual size_t Read(void *Buffer, size_t Count) override
Introduces a pure virtual method responsible for reading from the stream.
-
virtual size_t Write(const void *Buffer, size_t Count) override
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.