@@ -15,12 +15,6 @@ const defaultOptions = {
15
15
method : Utils . Constants . NONE
16
16
} ;
17
17
18
- function canonical ( p ) {
19
- // trick normalize think path is absolute
20
- var safeSuffix = pth . posix . normalize ( "/" + p . split ( "\\" ) . join ( "/" ) ) ;
21
- return pth . join ( "." , safeSuffix ) ;
22
- }
23
-
24
18
module . exports = function ( /**String*/ input , /** object */ options ) {
25
19
let inBuffer = null ;
26
20
@@ -37,7 +31,7 @@ module.exports = function (/**String*/ input, /** object */ options) {
37
31
}
38
32
39
33
// if input is buffer
40
- if ( input instanceof Uint8Array ) {
34
+ if ( Buffer . isBuffer ( input ) ) {
41
35
inBuffer = input ;
42
36
opts . method = Utils . Constants . BUFFER ;
43
37
input = undefined ;
@@ -62,17 +56,7 @@ module.exports = function (/**String*/ input, /** object */ options) {
62
56
// create variable
63
57
const _zip = new ZipFile ( inBuffer , opts ) ;
64
58
65
- function sanitize ( prefix , name ) {
66
- prefix = pth . resolve ( pth . normalize ( prefix ) ) ;
67
- var parts = name . split ( "/" ) ;
68
- for ( var i = 0 , l = parts . length ; i < l ; i ++ ) {
69
- var path = pth . normalize ( pth . join ( prefix , parts . slice ( i , l ) . join ( pth . sep ) ) ) ;
70
- if ( path . indexOf ( prefix ) === 0 ) {
71
- return path ;
72
- }
73
- }
74
- return pth . normalize ( pth . join ( prefix , pth . basename ( name ) ) ) ;
75
- }
59
+ const { canonical, sanitize } = Utils ;
76
60
77
61
function getEntry ( /**Object*/ entry ) {
78
62
if ( entry && _zip ) {
@@ -636,48 +620,52 @@ module.exports = function (/**String*/ input, /** object */ options) {
636
620
return ;
637
621
}
638
622
639
- var entries = _zip . entries ;
640
- var i = entries . length ;
641
- entries . forEach ( function ( entry ) {
642
- if ( i <= 0 ) return ; // Had an error already
623
+ targetPath = pth . resolve ( targetPath ) ;
624
+ // convert entryname to
625
+ const getPath = ( entry ) => sanitize ( targetPath , pth . normalize ( canonical ( entry . entryName . toString ( ) ) ) ) ;
626
+ const getError = ( msg , file ) => new Error ( msg + ': "' + file + '"' ) ;
643
627
644
- var entryName = pth . normalize ( canonical ( entry . entryName . toString ( ) ) ) ;
628
+ const entries = _zip . entries ;
645
629
646
- if ( entry . isDirectory ) {
647
- Utils . makeDir ( sanitize ( targetPath , entryName ) ) ;
648
- if ( -- i === 0 ) callback ( undefined ) ;
649
- return ;
630
+ // Create directory entries first
631
+ for ( const entry of entries . filter ( ( e ) => e . isDirectory ) ) {
632
+ const filePath = getPath ( entry ) ;
633
+ // The reverse operation for attr depend on method addFile()
634
+ const fileAttr = entry . header . fileAttr ;
635
+ try {
636
+ Utils . makeDir ( filePath ) ;
637
+ if ( fileAttr ) fs . chmodSync ( filePath , fileAttr ) ;
638
+ // in unix timestamp will change if files are later added to folder, but still
639
+ fs . utimesSync ( filePath , entry . header . time , entry . header . time ) ;
640
+ } catch ( er ) {
641
+ callback ( getError ( "Unable to create folder" , filePath ) ) ;
650
642
}
651
- entry . getDataAsync ( function ( content , err ) {
652
- if ( i <= 0 ) return ;
653
- if ( err ) {
654
- callback ( new Error ( err ) ) ;
643
+ }
644
+
645
+ // File entries
646
+ for ( const entry of entries . filter ( ( e ) => ! e . isDirectory ) ) {
647
+ const entryName = pth . normalize ( canonical ( entry . entryName . toString ( ) ) ) ;
648
+ const filePath = sanitize ( targetPath , entryName ) ;
649
+ entry . getDataAsync ( function ( content , err_1 ) {
650
+ if ( err_1 ) {
651
+ callback ( new Error ( err_1 ) ) ;
655
652
return ;
656
653
}
657
654
if ( ! content ) {
658
- i = 0 ;
659
655
callback ( new Error ( Utils . Errors . CANT_EXTRACT_FILE ) ) ;
660
- return ;
656
+ } else {
657
+ // The reverse operation for attr depend on method addFile()
658
+ Utils . writeFileToAsync ( filePath , content , overwrite , entry . header . fileAttr , function ( succ ) {
659
+ if ( ! succ ) callback ( getError ( "Unable to write file" , filePath ) ) ;
660
+ fs . utimes ( filePath , entry . header . time , entry . header . time , function ( err_2 ) {
661
+ if ( err_2 ) callback ( getError ( "Unable to set times" , filePath ) ) ;
662
+ } ) ;
663
+ } ) ;
661
664
}
662
-
663
- // The reverse operation for attr depend on method addFile()
664
- var fileAttr = entry . attr ? ( ( ( entry . attr >>> 0 ) | 0 ) >> 16 ) & 0xfff : 0 ;
665
- Utils . writeFileToAsync ( sanitize ( targetPath , entryName ) , content , overwrite , fileAttr , function ( succ ) {
666
- try {
667
- fs . utimesSync ( pth . resolve ( targetPath , entryName ) , entry . header . time , entry . header . time ) ;
668
- } catch ( er ) {
669
- callback ( new Error ( "Unable to set utimes" ) ) ;
670
- }
671
- if ( i <= 0 ) return ;
672
- if ( ! succ ) {
673
- i = 0 ;
674
- callback ( new Error ( "Unable to write" ) ) ;
675
- return ;
676
- }
677
- if ( -- i === 0 ) callback ( undefined ) ;
678
- } ) ;
679
665
} ) ;
680
- } ) ;
666
+ }
667
+
668
+ callback ( ) ;
681
669
} ,
682
670
683
671
/**
0 commit comments