Use DecompressionStream for decompressing .spz files (#181)

This commit is contained in:
Noeri Huisman
2025-09-25 17:25:57 +02:00
committed by GitHub
parent 2defaf3f64
commit c6bb49d74d
3 changed files with 48 additions and 41 deletions
+12 -18
View File
@@ -1344,10 +1344,9 @@ export class GunzipReader {
fileBytes: Uint8Array;
chunkBytes: number;
offset: number;
chunks: Uint8Array[];
totalBytes: number;
gunzip: Gunzip;
reader: ReadableStreamDefaultReader;
constructor({
fileBytes,
@@ -1355,28 +1354,23 @@ export class GunzipReader {
}: { fileBytes: Uint8Array; chunkBytes?: number }) {
this.fileBytes = fileBytes;
this.chunkBytes = chunkBytes;
this.offset = 0;
this.chunks = [];
this.totalBytes = 0;
this.gunzip = new Gunzip((chunk, _final) => {
this.chunks.push(chunk);
this.totalBytes += chunk.length;
});
const ds = new DecompressionStream("gzip");
const decompressionStream = new Blob([fileBytes]).stream().pipeThrough(ds);
this.reader = decompressionStream.getReader();
}
read(numBytes: number): Uint8Array {
while (this.totalBytes < numBytes && this.offset < this.fileBytes.length) {
const end = Math.min(
this.offset + this.chunkBytes,
this.fileBytes.length,
);
this.gunzip.push(this.fileBytes.subarray(this.offset, end), false);
this.offset = end;
}
async read(numBytes: number): Promise<Uint8Array> {
while (this.totalBytes < numBytes) {
const { value: chunk, done: readerDone } = await this.reader.read();
if (readerDone) {
break;
}
if (this.totalBytes < numBytes && this.offset >= this.fileBytes.length) {
this.gunzip.push(new Uint8Array(0), true);
this.chunks.push(chunk);
this.totalBytes += chunk.length;
}
if (this.totalBytes < numBytes) {