smartarchive/ts/classes.bzip2tools.ts

45 lines
1.3 KiB
TypeScript

import type { SmartArchive } from './classes.smartarchive.js';
import * as plugins from './plugins.js';
export class DecompressBzip2Transform extends plugins.stream.Transform {
private bzip2Decompressor: plugins.stream.Transform;
constructor() {
super();
// Initialize the bzip2 decompressor once here
this.bzip2Decompressor = plugins.unbzip2Stream();
this.bzip2Decompressor.on('data', (data: Buffer) => {
// When data is decompressed, push it to the stream
this.push(data);
});
this.bzip2Decompressor.on('error', (err) => {
// If an error occurs, emit it on this stream
this.emit('error', err);
});
}
_transform(chunk: Buffer, encoding: BufferEncoding, callback: plugins.stream.TransformCallback) {
// Pass the chunk directly to the decompressor
// The decompressor will handle the state across chunks
this.bzip2Decompressor.write(chunk);
callback();
}
_flush(callback: plugins.stream.TransformCallback) {
// When the stream is ending, end the decompressor stream as well
this.bzip2Decompressor.end();
callback();
}
}
export class Bzip2Tools {
smartArchiveRef: SmartArchive;
constructor(smartArchiveRefArg: SmartArchive) {
this.smartArchiveRef = smartArchiveRefArg;
}
getDecompressionStream() {
return new DecompressBzip2Transform();
}
}