File tree Expand file tree Collapse file tree 2 files changed +26
-11
lines changed
Expand file tree Collapse file tree 2 files changed +26
-11
lines changed Original file line number Diff line number Diff line change 1+ import { expect , test } from "bun:test" ;
2+ import { $ } from "bun" ;
3+ import extract from "./extract.ts" ;
4+
5+ test ( extract . name , async ( ) => {
6+ await $ `echo 'Hello, world!' | 7z a test.zip -si` ;
7+
8+ try {
9+ expect (
10+ new TextDecoder ( ) . decode (
11+ await extract ( Buffer . from ( await Bun . file ( "test.zip" ) . arrayBuffer ( ) ) )
12+ )
13+ ) . toBe ( "Hello, world!\n" ) ;
14+ } finally {
15+ await $ `rm -f test.zip` ;
16+ }
17+ } ) ;
Original file line number Diff line number Diff line change 1- /* https://github.com/TomasHubelbauer/node-extract-zip */
2-
3- import util from "util" ;
4- import zlib from "zlib" ;
5-
6- /**
7- * Extracts a single file from a single-file DEFLATE ZIP archive.
8- */
91export default async function extract ( buffer : Buffer ) {
102 // https://en.wikipedia.org/wiki/ZIP_(file_format)#End_of_central_directory_record_(EOCD)
113 const eocdIndex = buffer . indexOf (
@@ -62,7 +54,13 @@ export default async function extract(buffer: Buffer) {
6254 buffer . readUint16LE ( lfhIndex + 26 ) +
6355 buffer . readUint16LE ( lfhIndex + 28 ) ;
6456
65- return util . promisify ( zlib . inflateRaw ) (
66- buffer . subarray ( offset , offset + size )
67- ) ;
57+ const compressed = buffer . subarray ( offset , offset + size ) ;
58+
59+ const decompressed = await new Response (
60+ new Blob ( [ new Uint8Array ( compressed ) ] )
61+ . stream ( )
62+ . pipeThrough ( new DecompressionStream ( "deflate-raw" ) )
63+ ) . arrayBuffer ( ) ;
64+
65+ return Buffer . from ( decompressed ) ;
6866}
You can’t perform that action at this time.
0 commit comments