Files
smartarchive/ts/classes.gziptools.ts

63 lines
1.6 KiB
TypeScript
Raw Normal View History

2023-11-06 18:14:21 +01:00
import type { SmartArchive } from './classes.smartarchive.js';
import * as plugins from './plugins.js';
2023-11-06 18:14:21 +01:00
// 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,
) {
2023-11-06 18:14:21 +01:00
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,
) {
2023-11-06 18:14:21 +01:00
// 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 {
constructor() {}
2023-11-06 18:14:21 +01:00
public getCompressionStream() {
return new CompressGunzipTransform();
}
public getDecompressionStream() {
return new DecompressGunzipTransform();
}
}