infinity.zlib¶
Provides gzip compression and decompression functionality for strings, files and streams. The stream functions require the module infinity.stream for creating the infinity.stream objects.
Module: infinity.zlib
Example:
infinity.loadModule('infinity.zlib');
let compressedData = infinity.zlib.compress('123456_123456_123456_123456_123456_123456_123456');
console.debug(compressedData);
//H4sIAAAAAAAECzM0MjYxNYs3JJYCAKTSqLQwAAAA
let extractedData = infinity.zlib.extract(compressedData);
console.debug(extractedData);
//123456_123456_123456_123456_123456_123456_123456
compress()¶
Compresses the given string with gzip and returns the compressed data as a Base64-encoded string. The string is converted to bytes with UTF-8 before compression (before version 3.11 with the system default encoding, i.e. the ANSI code page under Windows, where characters outside of it were lost).
Signature:
compress( value: string ): string
Parameters:
- value:
stringThe string to compress.
Return type: string
Example:
infinity.loadModule('infinity.zlib');
let compressedData = infinity.zlib.compress('Hello World');
compressFile()¶
Reads out the contents of a file, compresses them with gzip and writes the results to a file at the given location. An existing output file is overwritten. Throws an error if a file cannot be opened.
Signature:
compressFile( inFileName: string, outFileName: string ): void
Parameters:
-
inFileName:
stringFilename, relative path (location relative to the folder with the used INFINITY.JS executable file) or absolute path to the file which has to be compressed.
-
outFileName:
stringFilename, relative path (location relative to the folder with the used INFINITY.JS executable file) or absolute path to the file which should contain the encoded output.
Example:
infinity.loadModule('infinity.zlib');
infinity.zlib.compressFile('dataToCompress.txt', 'compressedData.gz');
compressStream()¶
Reads the complete contents of a stream (from its beginning), compresses them with gzip and writes the results to another given stream, starting at the beginning of the output stream (existing data is overwritten, but the output stream is not truncated).
Signature:
compressStream( inStream: infinity.stream, outStream: infinity.stream ): void
Parameters:
-
inStream:
infinity.streamThe stream object to compress from. See infinity.stream.
-
outStream:
infinity.streamThe stream object to write to. See infinity.stream.
Example:
infinity.loadModule('infinity.zlib');
infinity.loadModule('infinity.stream');
let inStream = new infinity.stream();
let outStream = new infinity.stream();
try {
inStream.writeRawString('Hello World');
infinity.zlib.compressStream(inStream, outStream);
console.debug(outStream.size + ' bytes');
} finally {
inStream.free();
outStream.free();
}
extract()¶
Decompresses the given Base64-encoded gzip data (as returned by compress()) and returns the result as a string. The encoding is detected: a byte order mark (BOM), otherwise UTF-8 if the data is valid UTF-8, else Windows-1252 (so data compressed by older versions under Windows can still be read). Throws an error if the data cannot be decompressed.
Signature:
extract( value: string ): string
Parameters:
- value:
stringThe Base64-encoded gzip-compressed data to decompress.
Return type: string
Example:
infinity.loadModule('infinity.zlib');
let extractedData = infinity.zlib.extract(compressedData);
extractFile()¶
Reads out the contents of a file, attempts to decompress them with gzip, and writes the results to a file at the given location. An existing output file is overwritten. Throws an error if a file cannot be opened or the data cannot be decompressed.
Signature:
extractFile( inFileName: string, outFileName: string ): void
Parameters:
-
inFileName:
stringFilename, relative path (location relative to the folder with the used INFINITY.JS executable file) or absolute path to compressed file.
-
outFileName:
stringFilename, relative path (location relative to the folder with the used INFINITY.JS executable file) or absolute path to the file which should contain the decompressed output.
Example:
infinity.loadModule('infinity.zlib');
infinity.zlib.extractFile('compressedData.gz', 'extractedData.txt');
extractStream()¶
Reads the complete contents of a stream (from its beginning), attempts to decompress them with gzip, and writes the results to another given stream, starting at the beginning of the output stream (existing data is overwritten, but the output stream is not truncated). Throws an error if the data cannot be decompressed.
Signature:
extractStream( inStream: infinity.stream, outStream: infinity.stream ): void
Parameters:
-
inStream:
infinity.streamThe stream object containing the compressed data. See infinity.stream.
-
outStream:
infinity.streamThe stream object to write to. See infinity.stream.
Example:
infinity.loadModule('infinity.zlib');
infinity.loadModule('infinity.stream');
infinity.zlib.extractStream(inStream, outStream);
outStream.position = 0;
let text = outStream.readRawString(infinity.encoding.utf8);