@ -72,19 +72,6 @@ var ZipLocalFile = function(bstream) {
this . filename = bstream . readString ( this . fileNameLength ) ;
this . filename = bstream . readString ( this . fileNameLength ) ;
}
}
info ( "Zip Local File Header:" ) ;
info ( " version=" + this . version ) ;
info ( " general purpose=" + this . generalPurpose ) ;
info ( " compression method=" + this . compressionMethod ) ;
info ( " last mod file time=" + this . lastModFileTime ) ;
info ( " last mod file date=" + this . lastModFileDate ) ;
info ( " crc32=" + this . crc32 ) ;
info ( " compressed size=" + this . compressedSize ) ;
info ( " uncompressed size=" + this . uncompressedSize ) ;
info ( " file name length=" + this . fileNameLength ) ;
info ( " extra field length=" + this . extraFieldLength ) ;
info ( " filename = '" + this . filename + "'" ) ;
this . extraField = null ;
this . extraField = null ;
if ( this . extraFieldLength > 0 ) {
if ( this . extraFieldLength > 0 ) {
this . extraField = bstream . readString ( this . extraFieldLength ) ;
this . extraField = bstream . readString ( this . extraFieldLength ) ;
@ -107,6 +94,21 @@ var ZipLocalFile = function(bstream) {
this . compressedSize = bstream . readNumber ( 4 ) ;
this . compressedSize = bstream . readNumber ( 4 ) ;
this . uncompressedSize = bstream . readNumber ( 4 ) ;
this . uncompressedSize = bstream . readNumber ( 4 ) ;
}
}
// Now that we have all the bytes for this file, we can print out some information.
info ( "Zip Local File Header:" ) ;
info ( " version=" + this . version ) ;
info ( " general purpose=" + this . generalPurpose ) ;
info ( " compression method=" + this . compressionMethod ) ;
info ( " last mod file time=" + this . lastModFileTime ) ;
info ( " last mod file date=" + this . lastModFileDate ) ;
info ( " crc32=" + this . crc32 ) ;
info ( " compressed size=" + this . compressedSize ) ;
info ( " uncompressed size=" + this . uncompressedSize ) ;
info ( " file name length=" + this . fileNameLength ) ;
info ( " extra field length=" + this . extraFieldLength ) ;
info ( " filename = '" + this . filename + "'" ) ;
} ;
} ;
// determine what kind of compressed data we have and decompress
// determine what kind of compressed data we have and decompress
@ -132,6 +134,7 @@ ZipLocalFile.prototype.unzip = function() {
// Takes an ArrayBuffer of a zip file in
// Takes an ArrayBuffer of a zip file in
// returns null on error
// returns null on error
// returns an array of DecompressedFile objects on success
// returns an array of DecompressedFile objects on success
// ToDo This function differs
var unzip = function ( arrayBuffer ) {
var unzip = function ( arrayBuffer ) {
postMessage ( new bitjs . archive . UnarchiveStartEvent ( ) ) ;
postMessage ( new bitjs . archive . UnarchiveStartEvent ( ) ) ;
@ -159,11 +162,12 @@ var unzip = function(arrayBuffer) {
totalFilesInArchive = localFiles . length ;
totalFilesInArchive = localFiles . length ;
// got all local files, now sort them
// got all local files, now sort them
localFiles . sort ( function ( a , b ) {
localFiles . sort ( naturalCompare ) ;
/ * l o c a l F i l e s . s o r t ( f u n c t i o n ( a , b ) {
var aname = a . filename . toLowerCase ( ) ;
var aname = a . filename . toLowerCase ( ) ;
var bname = b . filename . toLowerCase ( ) ;
var bname = b . filename . toLowerCase ( ) ;
return aname > bname ? 1 : - 1 ;
return aname > bname ? 1 : - 1 ;
} ) ;
} ) ; * /
// archive extra data record
// archive extra data record
if ( bstream . peekNumber ( 4 ) === zArchiveExtraDataSignature ) {
if ( bstream . peekNumber ( 4 ) === zArchiveExtraDataSignature ) {
@ -253,9 +257,9 @@ function getHuffmanCodes(bitLengths) {
}
}
// Reference: http://tools.ietf.org/html/rfc1951#page-8
// Reference: http://tools.ietf.org/html/rfc1951#page-8
var numLengths = bitLengths . length ,
var numLengths = bitLengths . length ;
blCount = [ ] ,
var blCount = [ ] ;
MAX _BITS = 1 ;
var MAX _BITS = 1 ;
// Step 1: count up how many codes of each length we have
// Step 1: count up how many codes of each length we have
for ( var i = 0 ; i < numLengths ; ++ i ) {
for ( var i = 0 ; i < numLengths ; ++ i ) {
@ -274,8 +278,8 @@ function getHuffmanCodes(bitLengths) {
}
}
// Step 2: Find the numerical value of the smallest code for each code length
// Step 2: Find the numerical value of the smallest code for each code length
var nextCode = [ ] ,
var nextCode = [ ] ;
code = 0 ;
var code = 0 ;
for ( var bits = 1 ; bits <= MAX _BITS ; ++ bits ) {
for ( var bits = 1 ; bits <= MAX _BITS ; ++ bits ) {
var length2 = bits - 1 ;
var length2 = bits - 1 ;
// ensure undefined lengths are zero
// ensure undefined lengths are zero
@ -285,8 +289,8 @@ function getHuffmanCodes(bitLengths) {
}
}
// Step 3: Assign numerical values to all codes
// Step 3: Assign numerical values to all codes
var table = { } ,
var table = { } ;
tableLength = 0 ;
var tableLength = 0 ;
for ( var n = 0 ; n < numLengths ; ++ n ) {
for ( var n = 0 ; n < numLengths ; ++ n ) {
var len = bitLengths [ n ] ;
var len = bitLengths [ n ] ;
if ( len !== 0 ) {
if ( len !== 0 ) {
@ -353,7 +357,8 @@ function getFixedDistanceTable() {
// extract one bit at a time until we find a matching Huffman Code
// extract one bit at a time until we find a matching Huffman Code
// then return that symbol
// then return that symbol
function decodeSymbol ( bstream , hcTable ) {
function decodeSymbol ( bstream , hcTable ) {
var code = 0 , len = 0 ;
var code = 0 ;
var len = 0 ;
// loop until we match
// loop until we match
for ( ; ; ) {
for ( ; ; ) {
@ -364,7 +369,6 @@ function decodeSymbol(bstream, hcTable) {
// check against Huffman Code table and break if found
// check against Huffman Code table and break if found
if ( hcTable . hasOwnProperty ( code ) && hcTable [ code ] . length === len ) {
if ( hcTable . hasOwnProperty ( code ) && hcTable [ code ] . length === len ) {
break ;
break ;
}
}
if ( len > hcTable . maxLength ) {
if ( len > hcTable . maxLength ) {
@ -500,10 +504,10 @@ function inflateBlockData(bstream, hcLiteralTable, hcDistanceTable, buffer) {
if ( symbol === 256 ) {
if ( symbol === 256 ) {
break ;
break ;
} else {
} else {
var lengthLookup = LengthLookupTable [ symbol - 257 ] ,
var lengthLookup = LengthLookupTable [ symbol - 257 ] ;
length = lengthLookup [ 1 ] + bstream . readBits ( lengthLookup [ 0 ] ) ,
var length = lengthLookup [ 1 ] + bstream . readBits ( lengthLookup [ 0 ] ) ;
distLookup = DistLookupTable [ decodeSymbol ( bstream , hcDistanceTable ) ] ,
var distLookup = DistLookupTable [ decodeSymbol ( bstream , hcDistanceTable ) ] ;
distance = distLookup [ 1 ] + bstream . readBits ( distLookup [ 0 ] ) ;
var distance = distLookup [ 1 ] + bstream . readBits ( distLookup [ 0 ] ) ;
// now apply length and distance appropriately and copy to output
// now apply length and distance appropriately and copy to output
@ -634,8 +638,8 @@ function inflate(compressedData, numDecompressedBytes) {
var distanceCodeLengths = literalCodeLengths . splice ( numLiteralLengthCodes , numDistanceCodes ) ;
var distanceCodeLengths = literalCodeLengths . splice ( numLiteralLengthCodes , numDistanceCodes ) ;
// now generate the true Huffman Code tables using these code lengths
// now generate the true Huffman Code tables using these code lengths
var hcLiteralTable = getHuffmanCodes ( literalCodeLengths ) ,
var hcLiteralTable = getHuffmanCodes ( literalCodeLengths ) ;
hcDistanceTable = getHuffmanCodes ( distanceCodeLengths ) ;
var hcDistanceTable = getHuffmanCodes ( distanceCodeLengths ) ;
blockSize = inflateBlockData ( bstream , hcLiteralTable , hcDistanceTable , buffer ) ;
blockSize = inflateBlockData ( bstream , hcLiteralTable , hcDistanceTable , buffer ) ;
} else {
} else {
// error
// error
@ -659,3 +663,51 @@ function inflate(compressedData, numDecompressedBytes) {
onmessage = function ( event ) {
onmessage = function ( event ) {
unzip ( event . data . file , true ) ;
unzip ( event . data . file , true ) ;
} ;
} ;
/ *
function naturalCompare ( a , b ) {
var ax = [ ] , bx = [ ] ;
a . filename . toLowerCase ( ) . replace ( /(\d+)|(\D+)/g , function ( _ , $1 , $2 ) { ax . push ( [ $1 || Infinity , $2 || "" ] ) } ) ;
b . filename . toLowerCase ( ) . replace ( /(\d+)|(\D+)/g , function ( _ , $1 , $2 ) { bx . push ( [ $1 || Infinity , $2 || "" ] ) } ) ;
while ( ax . length && bx . length ) {
var an = ax . shift ( ) ;
var bn = bx . shift ( ) ;
var nn = ( an [ 0 ] - bn [ 0 ] ) || an [ 1 ] . localeCompare ( bn [ 1 ] ) ;
if ( nn ) return nn ;
}
return ax . length - bx . length ;
} * /
/*var re = / ( [ a - z ] + ) ( \ d + ) ( . + ) / i ;
function naturalCompare ( a , b ) {
var ma = a . match ( re ) ,
mb = b . match ( re ) ,
a _str = ma [ 1 ] ,
b _str = mb [ 1 ] ,
a _num = parseInt ( ma [ 2 ] , 10 ) ,
b _num = parseInt ( mb [ 2 ] , 10 ) ,
a _rem = ma [ 3 ] ,
b _rem = mb [ 3 ] ;
return a _str > b _str ? 1 : a _str < b _str ? - 1 : a _num > b _num ? 1 : a _num < b _num ? - 1 : a _rem > b _rem ;
} * /
/ * f u n c t i o n n a t u r a l C o m p a r e ( a , b ) {
var ax = [ ] , bx = [ ] ;
a . replace ( /(\d+)|(\D+)/g , function ( _ , $1 , $2 ) { ax . push ( [ $1 || Infinity , $2 || "" ] ) } ) ;
b . replace ( /(\d+)|(\D+)/g , function ( _ , $1 , $2 ) { bx . push ( [ $1 || Infinity , $2 || "" ] ) } ) ;
while ( ax . length && bx . length ) {
var an = ax . shift ( ) ;
var bn = bx . shift ( ) ;
var nn = ( an [ 0 ] - bn [ 0 ] ) || an [ 1 ] . localeCompare ( bn [ 1 ] ) ;
if ( nn ) return nn ;
}
return ax . length - bx . length ;
} * /