1515import { BitStream } from '../io/bitstream.js' ;
1616import { ByteBuffer } from '../io/bytebuffer.js' ;
1717import { ByteStream } from '../io/bytestream.js' ;
18+ import { ARCHIVE_EXTRA_DATA_SIG , CENTRAL_FILE_HEADER_SIG , CRC32_MAGIC_NUMBER ,
19+ DATA_DESCRIPTOR_SIG , DIGITAL_SIGNATURE_SIG , END_OF_CENTRAL_DIR_SIG ,
20+ LOCAL_FILE_HEADER_SIG } from './common.js' ;
1821
1922const UnarchiveState = {
2023 NOT_STARTED : 0 ,
@@ -28,6 +31,7 @@ let hostPort;
2831
2932// State - consider putting these into a class.
3033let unarchiveState = UnarchiveState . NOT_STARTED ;
34+ /** @type {ByteStream } */
3135let bytestream = null ;
3236let allLocalFiles = null ;
3337let logToConsole = false ;
@@ -60,24 +64,14 @@ const postProgress = function () {
6064 } ) ;
6165} ;
6266
63- const zLocalFileHeaderSignature = 0x04034b50 ;
64- const zArchiveExtraDataSignature = 0x08064b50 ;
65- const zCentralFileHeaderSignature = 0x02014b50 ;
66- const zDigitalSignatureSignature = 0x05054b50 ;
67- const zEndOfCentralDirSignature = 0x06054b50 ;
68- const zEndOfCentralDirLocatorSignature = 0x07064b50 ;
69- const zDataDescriptorSignature = 0x08074b50 ;
70-
7167// mask for getting the Nth bit (zero-based)
7268const BIT = [ 0x01 , 0x02 , 0x04 , 0x08 ,
7369 0x10 , 0x20 , 0x40 , 0x80 ,
7470 0x100 , 0x200 , 0x400 , 0x800 ,
7571 0x1000 , 0x2000 , 0x4000 , 0x8000 ] ;
7672
7773class ZipLocalFile {
78- /**
79- * @param {ByteStream } bstream
80- */
74+ /** @param {ByteStream } bstream */
8175 constructor ( bstream ) {
8276 if ( typeof bstream != typeof { } || ! bstream . readNumber || typeof bstream . readNumber != typeof function ( ) { } ) {
8377 return null ;
@@ -125,9 +119,9 @@ class ZipLocalFile {
125119 let foundDataDescriptor = false ;
126120 let numBytesSeeked = 0 ;
127121 while ( ! foundDataDescriptor ) {
128- while ( bstream . peekNumber ( 4 ) !== zLocalFileHeaderSignature &&
129- bstream . peekNumber ( 4 ) !== zArchiveExtraDataSignature &&
130- bstream . peekNumber ( 4 ) !== zCentralFileHeaderSignature ) {
122+ while ( bstream . peekNumber ( 4 ) !== LOCAL_FILE_HEADER_SIG &&
123+ bstream . peekNumber ( 4 ) !== ARCHIVE_EXTRA_DATA_SIG &&
124+ bstream . peekNumber ( 4 ) !== CENTRAL_FILE_HEADER_SIG ) {
131125 numBytesSeeked ++ ;
132126 bstream . readBytes ( 1 ) ;
133127 }
@@ -143,7 +137,7 @@ class ZipLocalFile {
143137
144138 // From the PKZIP App Note: "The signature value 0x08074b50 is also used by some ZIP
145139 // implementations as a marker for the Data Descriptor record".
146- if ( maybeDescriptorSig === zDataDescriptorSignature ) {
140+ if ( maybeDescriptorSig === DATA_DESCRIPTOR_SIG ) {
147141 if ( maybeCompressedSize === ( numBytesSeeked - 16 ) ) {
148142 foundDataDescriptor = true ;
149143 descriptorSize = 16 ;
@@ -606,7 +600,7 @@ function archiveUnzip() {
606600 let bstream = bytestream . tee ( ) ;
607601
608602 // loop until we don't see any more local files or we find a data descriptor.
609- while ( bstream . peekNumber ( 4 ) == zLocalFileHeaderSignature ) {
603+ while ( bstream . peekNumber ( 4 ) == LOCAL_FILE_HEADER_SIG ) {
610604 // Note that this could throw an error if the bstream overflows, which is caught in the
611605 // message handler.
612606 const oneLocalFile = new ZipLocalFile ( bstream ) ;
@@ -636,7 +630,7 @@ function archiveUnzip() {
636630 totalFilesInArchive = allLocalFiles . length ;
637631
638632 // archive extra data record
639- if ( bstream . peekNumber ( 4 ) == zArchiveExtraDataSignature ) {
633+ if ( bstream . peekNumber ( 4 ) == ARCHIVE_EXTRA_DATA_SIG ) {
640634 if ( logToConsole ) {
641635 info ( ' Found an Archive Extra Data Signature' ) ;
642636 }
@@ -649,13 +643,13 @@ function archiveUnzip() {
649643
650644 // central directory structure
651645 // TODO: handle the rest of the structures (Zip64 stuff)
652- if ( bstream . peekNumber ( 4 ) == zCentralFileHeaderSignature ) {
646+ if ( bstream . peekNumber ( 4 ) == CENTRAL_FILE_HEADER_SIG ) {
653647 if ( logToConsole ) {
654648 info ( ' Found a Central File Header' ) ;
655649 }
656650
657651 // read all file headers
658- while ( bstream . peekNumber ( 4 ) == zCentralFileHeaderSignature ) {
652+ while ( bstream . peekNumber ( 4 ) == CENTRAL_FILE_HEADER_SIG ) {
659653 bstream . readNumber ( 4 ) ; // signature
660654 const cdfh = {
661655 versionMadeBy : bstream . readNumber ( 2 ) ,
@@ -688,7 +682,7 @@ function archiveUnzip() {
688682 }
689683
690684 // digital signature
691- if ( bstream . peekNumber ( 4 ) == zDigitalSignatureSignature ) {
685+ if ( bstream . peekNumber ( 4 ) == DIGITAL_SIGNATURE_SIG ) {
692686 if ( logToConsole ) {
693687 info ( ' Found a Digital Signature' ) ;
694688 }
@@ -699,7 +693,7 @@ function archiveUnzip() {
699693 }
700694
701695 let metadata = { } ;
702- if ( bstream . peekNumber ( 4 ) == zEndOfCentralDirSignature ) {
696+ if ( bstream . peekNumber ( 4 ) == END_OF_CENTRAL_DIR_SIG ) {
703697 bstream . readNumber ( 4 ) ; // signature
704698 const eocds = {
705699 numberOfThisDisk : bstream . readNumber ( 2 ) ,
0 commit comments