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