smartarchive/ts/classes.gziptools.ts

57 lines
1.5 KiB
TypeScript
Raw Normal View History

2023-11-06 17:14:21 +00:00
import type { SmartArchive } from './classes.smartarchive.js';
import * as plugins from './plugins.js'
// This class wraps fflate's gunzip in a Node.js Transform stream
export class CompressGunzipTransform extends plugins.stream.Transform {
constructor() {
super();
}
_transform(chunk: Buffer, encoding: BufferEncoding, callback: plugins.stream.TransformCallback) {
plugins.fflate.gunzip(chunk, (err, decompressed) => {
if (err) {
callback(err);
} else {
this.push(decompressed);
callback();
}
});
}
}
// DecompressGunzipTransform class that extends the Node.js Transform stream to
// create a stream that decompresses GZip-compressed data using fflate's gunzip function
export class DecompressGunzipTransform extends plugins.stream.Transform {
constructor() {
super();
}
_transform(chunk: Buffer, encoding: BufferEncoding, callback: plugins.stream.TransformCallback) {
// Use fflate's gunzip function to decompress the chunk
plugins.fflate.gunzip(chunk, (err, decompressed) => {
if (err) {
// If an error occurs during decompression, pass the error to the callback
callback(err);
} else {
// If decompression is successful, push the decompressed data into the stream
this.push(decompressed);
callback();
}
});
}
}
export class GzipTools {
2024-06-08 10:46:43 +00:00
constructor() {
2023-11-06 17:14:21 +00:00
}
public getCompressionStream() {
return new CompressGunzipTransform();
}
public getDecompressionStream() {
return new DecompressGunzipTransform();
}
}