Title: | Compression and Decompression |
---|---|
Description: | The 'zlib' package for R aims to offer an R-based equivalent of 'Python's' built-in 'zlib' module for data compression and decompression. This package provides a suite of functions for working with 'zlib' compression, including utilities for compressing and decompressing data streams, manipulating compressed files, and working with 'gzip', 'zlib', and 'deflate' formats. |
Authors: | Semjon Geist [aut, cre] |
Maintainer: | Semjon Geist <[email protected]> |
License: | MIT + file LICENSE |
Version: | 1.0.3 |
Built: | 2024-11-09 06:13:12 UTC |
Source: | CRAN |
Compresses the provided raw data in a single step.
compress( data, level = -1, method = zlib$DEFLATED, wbits = zlib$MAX_WBITS, memLevel = zlib$DEF_MEM_LEVEL, strategy = zlib$Z_DEFAULT_STRATEGY, zdict = NULL )
compress( data, level = -1, method = zlib$DEFLATED, wbits = zlib$MAX_WBITS, memLevel = zlib$DEF_MEM_LEVEL, strategy = zlib$Z_DEFAULT_STRATEGY, zdict = NULL )
data |
Raw data to be compressed. |
level |
Compression level, default is -1. |
method |
Compression method, default is |
wbits |
Window bits, default is |
memLevel |
Memory level, default is |
strategy |
Compression strategy, default is |
zdict |
Optional predefined compression dictionary as a raw vector. |
The compress
function simplifies the compression process by encapsulating
the creation of a compression object, compressing the data, and flushing the buffer
all within a single call. This is particularly useful for scenarios where the user
wants to quickly compress data without dealing with the intricacies of compression
objects and buffer management. The function leverages the compressobj
function
to handle the underlying compression mechanics.
A raw vector containing the compressed data.
compressed_data <- compress(charToRaw("some data"))
compressed_data <- compress(charToRaw("some data"))
Compresses a given chunk of raw binary data using a pre-existing compressor object.
compress_chunk(compressorPtr, input_chunk)
compress_chunk(compressorPtr, input_chunk)
compressorPtr |
An external pointer to an existing compressor object.
This object is usually initialized by calling a different function like |
input_chunk |
A raw vector containing the uncompressed data that needs to be compressed. |
This function is primarily designed for use with a compressor object created by create_compressor()
.
It takes a chunk of raw data and compresses it, returning a raw vector of the compressed data.
A raw vector containing the compressed data.
# Create a new compressor object for zlib -> wbts = 15 zlib_compressor <- create_compressor(wbits=31) compressed_data <- compress_chunk(zlib_compressor, charToRaw("Hello, World")) compressed_data <- c(compressed_data, flush_compressor_buffer(zlib_compressor)) decompressed_data <- memDecompress(compressed_data, type = "gzip") cat(rawToChar(decompressed_data))
# Create a new compressor object for zlib -> wbts = 15 zlib_compressor <- create_compressor(wbits=31) compressed_data <- compress_chunk(zlib_compressor, charToRaw("Hello, World")) compressed_data <- c(compressed_data, flush_compressor_buffer(zlib_compressor)) decompressed_data <- memDecompress(compressed_data, type = "gzip") cat(rawToChar(decompressed_data))
compressobj
initializes a new compression object with specified parameters
and methods. The function makes use of publicEval
to manage scope and encapsulation.
compressobj( level = -1, method = zlib$DEFLATED, wbits = zlib$MAX_WBITS, memLevel = zlib$DEF_MEM_LEVEL, strategy = zlib$Z_DEFAULT_STRATEGY, zdict = NULL )
compressobj( level = -1, method = zlib$DEFLATED, wbits = zlib$MAX_WBITS, memLevel = zlib$DEF_MEM_LEVEL, strategy = zlib$Z_DEFAULT_STRATEGY, zdict = NULL )
level |
Compression level, default is -1. |
method |
Compression method, default is |
wbits |
Window bits, default is |
memLevel |
Memory level, default is |
strategy |
Compression strategy, default is |
zdict |
Optional predefined compression dictionary as a raw vector. |
Returns an environment containing the public methods compress
and flush
.
compress(data)
: Compresses a chunk of data.
flush()
: Flushes the compression buffer.
compressor <- compressobj(level = 6) compressed_data <- compressor$compress(charToRaw("some data")) compressed_data <- c(compressed_data, compressor$flush())
compressor <- compressobj(level = 6) compressed_data <- compressor$compress(charToRaw("some data")) compressed_data <- c(compressed_data, compressor$flush())
Initialize a new compressor object for zlib-based compression with specified settings.
create_compressor( level = -1L, method = 8L, wbits = 15L, memLevel = 8L, strategy = 0L, zdict = NULL )
create_compressor( level = -1L, method = 8L, wbits = 15L, memLevel = 8L, strategy = 0L, zdict = NULL )
level |
Compression level, integer between 0 and 9, or -1 for default. |
method |
Compression method. |
wbits |
Window size bits. |
memLevel |
Memory level for internal compression state. |
strategy |
Compression strategy. |
zdict |
Optional predefined compression dictionary as a raw vector. |
A SEXP pointer to the new compressor object.
compressor <- create_compressor(level = 6, memLevel = 8)
compressor <- create_compressor(level = 6, memLevel = 8)
Initialize a new decompressor object for zlib-based decompression.
create_decompressor(wbits = 0L)
create_decompressor(wbits = 0L)
wbits |
The window size bits parameter. Default is 0. |
A SEXP pointer to the new decompressor object.
decompressor <- create_decompressor()
decompressor <- create_decompressor()
Decompresses the provided compressed raw data in a single step.
decompress(data, wbits = 0)
decompress(data, wbits = 0)
data |
Compressed raw data to be decompressed. |
wbits |
The window size bits parameter. Default is 0. |
The decompress
function offers a streamlined approach to decompressing
raw data. By abstracting the creation of a decompression object, decompressing
the data, and flushing the buffer into one function call, it provides a hassle-free
way to retrieve original data from its compressed form. This function is designed
to work seamlessly with data compressed using the compress
function or
any other zlib-based compression method.
A raw vector containing the decompressed data.
original_data <- charToRaw("some data") compressed_data <- compress(original_data) decompressed_data <- decompress(compressed_data)
original_data <- charToRaw("some data") compressed_data <- compress(original_data) decompressed_data <- decompress(compressed_data)
Perform chunk-wise decompression on a given raw vector using a decompressor object.
decompress_chunk(decompressorPtr, input_chunk)
decompress_chunk(decompressorPtr, input_chunk)
decompressorPtr |
An external pointer to an initialized decompressor object. |
input_chunk |
A raw vector containing the compressed data chunk. |
A raw vector containing the decompressed data.
rawToChar(decompress_chunk(create_decompressor(), memCompress(charToRaw("Hello, World"))))
rawToChar(decompress_chunk(create_decompressor(), memCompress(charToRaw("Hello, World"))))
Initializes a new decompressor object for zlib-based decompression.
decompressobj(wbits = 0)
decompressobj(wbits = 0)
wbits |
The window size bits parameter. Default is 0. |
The returned decompressor object has methods for performing chunk-wise decompression on compressed data using the zlib library.
A decompressor object with methods for decompression.
decompress(data)
: Compresses a chunk of data.
flush()
: Flushes the compression buffer.
compressor <- zlib$compressobj(zlib$Z_DEFAULT_COMPRESSION, zlib$DEFLATED, zlib$MAX_WBITS + 16) compressed_data <- compressor$compress(charToRaw("some data")) compressed_data <- c(compressed_data, compressor$flush()) decompressor <- decompressobj(zlib$MAX_WBITS + 16) decompressed_data <- c(decompressor$decompress(compressed_data), decompressor$flush())
compressor <- zlib$compressobj(zlib$Z_DEFAULT_COMPRESSION, zlib$DEFLATED, zlib$MAX_WBITS + 16) compressed_data <- compressor$compress(charToRaw("some data")) compressed_data <- c(compressed_data, compressor$flush()) decompressor <- decompressobj(zlib$MAX_WBITS + 16) decompressed_data <- c(decompressor$decompress(compressed_data), decompressor$flush())
This function flushes the internal buffer according to the specified mode.
flush_compressor_buffer(compressorPtr, mode = 4L)
flush_compressor_buffer(compressorPtr, mode = 4L)
compressorPtr |
A SEXP pointer to an existing compressor object. |
mode |
A compression flush mode. Default is Z_FINISH. Available modes are Z_NO_FLUSH, Z_PARTIAL_FLUSH, Z_SYNC_FLUSH, Z_FULL_FLUSH, Z_BLOCK, and Z_FINISH. |
A raw vector containing the flushed output.
compressor <- create_compressor() # ... (some compression actions) flushed_data <- flush_compressor_buffer(compressor)
compressor <- create_compressor() # ... (some compression actions) flushed_data <- flush_compressor_buffer(compressor)
This function processes all pending input and returns the remaining uncompressed output. The function uses the provided initial buffer size and dynamically expands it as necessary to ensure all remaining data is decompressed. After calling this function, the decompress_chunk() method cannot be called again on the same object.
flush_decompressor_buffer(decompressorPtr, length = 256L)
flush_decompressor_buffer(decompressorPtr, length = 256L)
decompressorPtr |
A SEXP pointer to an existing decompressor object. |
length |
An optional parameter that sets the initial size of the output buffer. Default is 256. |
A raw vector containing the remaining uncompressed output.
decompressor <- create_decompressor() # ... (some decompression actions) flushed_data <- flush_decompressor_buffer(decompressor)
decompressor <- create_decompressor() # ... (some decompression actions) flushed_data <- flush_decompressor_buffer(decompressor)
This function takes a file path as input and checks if it's a valid gzip-compressed file.
It reads the file in chunks and tries to decompress it using the zlib library.
If any step fails, the function returns FALSE
. Otherwise, it returns TRUE
.
validate_gzip_file(file_path)
validate_gzip_file(file_path)
file_path |
A string representing the path of the file to validate. |
A boolean value indicating whether the file is a valid gzip file.
TRUE
if the file is valid, FALSE
otherwise.
validate_gzip_file("path/to/your/file.gz")
validate_gzip_file("path/to/your/file.gz")