81 lines
1.9 KiB
TypeScript
81 lines
1.9 KiB
TypeScript
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 {
|
|
private gunzip: any; // fflate.Gunzip instance
|
|
|
|
constructor() {
|
|
super();
|
|
|
|
// Create a streaming Gunzip decompressor
|
|
this.gunzip = new plugins.fflate.Gunzip((chunk, final) => {
|
|
// Push decompressed chunks to the output stream
|
|
this.push(Buffer.from(chunk));
|
|
if (final) {
|
|
// Signal end of stream when decompression is complete
|
|
this.push(null);
|
|
}
|
|
});
|
|
}
|
|
|
|
_transform(
|
|
chunk: Buffer,
|
|
encoding: BufferEncoding,
|
|
callback: plugins.stream.TransformCallback,
|
|
) {
|
|
try {
|
|
// Feed chunks to the gunzip stream
|
|
this.gunzip.push(chunk, false);
|
|
callback();
|
|
} catch (err) {
|
|
callback(err as Error);
|
|
}
|
|
}
|
|
|
|
_flush(callback: plugins.stream.TransformCallback) {
|
|
try {
|
|
// Signal end of input to gunzip
|
|
this.gunzip.push(new Uint8Array(0), true);
|
|
callback();
|
|
} catch (err) {
|
|
callback(err as Error);
|
|
}
|
|
}
|
|
}
|
|
|
|
export class GzipTools {
|
|
constructor() {}
|
|
|
|
public getCompressionStream() {
|
|
return new CompressGunzipTransform();
|
|
}
|
|
|
|
public getDecompressionStream() {
|
|
return new DecompressGunzipTransform();
|
|
}
|
|
}
|