Zlib : Compression library
This module provides zlib compression and decompression functions, and also provides gzip format file access functions. This module is available in EdgerOS 1.8.5 and later.
User can use the following code to import the zlib module.
var zlib = require('zlib');
Support
The following shows zlib module APIs available for each permissions.
| User Mode | Privilege Mode | |
|---|---|---|
| zlib.compress | ● | ● |
| zlib.decompress | ● | ● |
| zlib.gz.open | ● | ● |
| gzfile.close | ● | ● |
| gzfile.read | ● | ● |
| gzfile.write | ● | ● |
| gzfile.tell | ● | ● |
| gzfile.sync | ● | ● |
| zlib.gz.createReadStream | ● | ● |
| zlib.gz.createWriteStream | ● | ● |
Compress and Decompress
zlib.compress(string[, level])
string{String} String to be compressed.level{Integer} Compression level (1 ~ 9). default: 6.- Returns: {Buffer} Compressed buffer.
Compress the string.
zlib.compress(buffer[, offset[, length[, level]]])
buffer{Buffer} Buffer to be compressed.offset{Integer} Buffer offset. default:0.length{Integer} Write length. default:buffer.length.level{Integer} Compression level (1 ~ 9). default: 6.- Returns: {Buffer} Compressed buffer.
Compress the buffer.
zlib.decompress(origLen, buffer[, offset[, length]])
origLen{Integer} Original data length before compression.buffer{Buffer} Compressed data.offset{Integer} Buffer offset. default:0.length{Integer} Write length. default:buffer.length.- Returns: {Buffer} Original data.
Data decompression.
Gzip File
zlib.gz.open(path[, mode])
path{String} File path.mode{String} Open mode. default: 'r'.- Returns: {Object} Gzip file object.
Open or create a gzip file, mode optional modes include:
mode | Description |
|---|---|
'w' | open for writing. |
'r' | open for reading. |
'a' | open for append writing. |
Example
var gzfile = zlib.gz.open('test.txt.gz', 'w');
gzfile.close()
Close the gzip file object, after this object is closed, it is not allowed to be used again.
gzfile.read(buffer[, offset[, length]])
buffer{Buffer} Read buffer.offset{Integer} Buffer offset. default:0.length{Integer} Write length. default:buffer.length.- Returns: {Integer} The number of bytes actually read.
Read data from gzip file, the data in gzip file will be decompressed and read.
Example
var gzfile = zlib.gz.open('test.txt.gz', 'r');
var buffer = new Buffer(1024);
while (true) {
var num = gzfile.read(buffer);
if (num > 0) {
var content = buffer.slice(0, num);
console.log(content.toString('hex'));
} else {
break;
}
}
gzfile.close();
gzfile.write(string)
string{Buffer} String to be written.- Returns: {Integer} The number of bytes actually write.
Write the string to a gzip file, the data will be compressed and written to the gzip file.
Example
var gzfile = zlib.gz.open('test.txt.gz', 'w');
gzfile.write('hello gzip file!');
gzfile.close();
gzfile.write(buffer[, offset[, length]])
buffer{Buffer} Write buffer.offset{Integer} Buffer offset. default:0.length{Integer} Write length. default:buffer.length.- Returns: {Integer} The number of bytes actually write.
Write the buffer content to a gzip file, the data will be compressed and written to the gzip file.
gzfile.tell()
- Returns: {Integer} Current position of the read and write pointer.
Get the current position of the read and write pointer.
gzfile.sync()
Write the current cached data to a file.
Gzip File Stream
Stream type Gzip operation can be very convenient for file compression and decompression. It is very similar to fs.createReadStream and fs.createWriteStream, please refer to Stream Filesystem
zlib.gz.createReadStream(path[, opt])
path{String} Gzip file path.opt{Object}Readableobject creation parameters. please refer to Stream- Returns: {Readable} A readable stream.
Creates a Gzip file Readable stream. The file contents read from this stream will be automatically decompressed.
Example
var r = zlib.gz.createReadStream('aaa.txt.gz');
var w = fs.createWriteStream('aaa.txt');
r.pipe(w);
w.on('finish', () => console.log('decompressed!'));
zlib.gz.createWriteStream(path[, opt])
path{String} Gzip file path.opt{Object}Writableobject creation parameters. please refer to Stream- Returns: {Writable} A writable stream.
Creates a Gzip file Writable stream. Data can be written to this stream, the data will be automatically compressed and stored in the Gzip file specified by path. opt.mode can be 'w' or 'a', default is 'w'.
Example
var r = fs.createReadStream('aaa.txt');
var w = zlib.gz.createWriteStream('aaa.txt.gz');
r.pipe(w);
w.on('finish', () => console.log('compressed!'));




陕公网安备61019002002605号