@@ -178,7 +178,7 @@ class ZipLocalFile {
178178 }
179179
180180 // determine what kind of compressed data we have and decompress
181- unzip ( ) {
181+ async unzip ( ) {
182182 if ( ! this . fileData ) {
183183 err ( 'unzip() called on a file with out compressed file data' ) ;
184184 }
@@ -196,7 +196,7 @@ class ZipLocalFile {
196196 if ( logToConsole ) {
197197 info ( `ZIP v2.0, DEFLATE: ${ this . filename } (${ this . compressedSize } bytes)` ) ;
198198 }
199- this . fileData = inflate ( this . fileData , this . uncompressedSize ) ;
199+ this . fileData = await inflate ( this . fileData , this . uncompressedSize ) ;
200200 }
201201 else {
202202 err ( `UNSUPPORTED VERSION/FORMAT: ZIP v${ this . version } , ` +
@@ -483,9 +483,18 @@ function inflateBlockData(bstream, hcLiteralTable, hcDistanceTable, buffer) {
483483 * Compression method 8. Deflate: http://tools.ietf.org/html/rfc1951
484484 * @param {Uint8Array } compressedData A Uint8Array of the compressed file data.
485485 * @param {number } numDecompressedBytes
486- * @returns {Uint8Array } The decompressed array.
486+ * @returns {Promise< Uint8Array> } The decompressed array.
487487 */
488- function inflate ( compressedData , numDecompressedBytes ) {
488+ async function inflate ( compressedData , numDecompressedBytes ) {
489+ // Try to use native implementation of DEFLATE if it exists.
490+ try {
491+ const blob = new Blob ( [ compressedData . buffer ] ) ;
492+ const decompressedStream = blob . stream ( ) . pipeThrough ( new DecompressionStream ( 'deflate-raw' ) ) ;
493+ return new Uint8Array ( await new Response ( decompressedStream ) . arrayBuffer ( ) ) ;
494+ } catch ( err ) {
495+ // Fall through to non-native implementation of DEFLATE.
496+ }
497+
489498 // Bit stream representing the compressed data.
490499 /** @type {BitStream } */
491500 const bstream = new BitStream ( compressedData . buffer ,
@@ -596,7 +605,7 @@ function inflate(compressedData, numDecompressedBytes) {
596605 return buffer . data ;
597606}
598607
599- function archiveUnzip ( ) {
608+ async function archiveUnzip ( ) {
600609 let bstream = bytestream . tee ( ) ;
601610
602611 // loop until we don't see any more local files or we find a data descriptor.
@@ -619,7 +628,7 @@ function archiveUnzip() {
619628 currentBytesUnarchivedInFile = 0 ;
620629
621630 // Actually do the unzipping.
622- oneLocalFile . unzip ( ) ;
631+ await oneLocalFile . unzip ( ) ;
623632
624633 if ( oneLocalFile . fileData != null ) {
625634 hostPort . postMessage ( { type : 'extract' , unarchivedFile : oneLocalFile } , [ oneLocalFile . fileData . buffer ] ) ;
@@ -724,7 +733,7 @@ function archiveUnzip() {
724733
725734// event.data.file has the first ArrayBuffer.
726735// event.data.bytes has all subsequent ArrayBuffers.
727- const onmessage = function ( event ) {
736+ const onmessage = async function ( event ) {
728737 const bytes = event . data . file || event . data . bytes ;
729738 logToConsole = ! ! event . data . logToConsole ;
730739
@@ -755,7 +764,7 @@ const onmessage = function (event) {
755764 if ( unarchiveState === UnarchiveState . UNARCHIVING ||
756765 unarchiveState === UnarchiveState . WAITING ) {
757766 try {
758- archiveUnzip ( ) ;
767+ await archiveUnzip ( ) ;
759768 } catch ( e ) {
760769 if ( typeof e === 'string' && e . startsWith ( 'Error! Overflowed' ) ) {
761770 // Overrun the buffer.
0 commit comments