2323*/
2424
2525import { File } from "../lib/node-utility/File" ;
26- import { ExeFile } from "../lib/node-utility/Executable" ;
26+ import { ExeFile , ExeCmd } from "../lib/node-utility/Executable" ;
2727import * as events from 'events' ;
2828import * as child_process from 'child_process' ;
2929import * as platform from './Platform' ;
@@ -44,7 +44,7 @@ export class SevenZipper {
4444 private _7za : File ;
4545 private _event : events . EventEmitter ;
4646
47- on ( event : 'progress' , listener : ( currentProgress : number ) => void ) : this;
47+ on ( event : 'progress' , listener : ( progress : number , msg ?: string ) => void ) : this;
4848 on ( event : any , listener : ( arg ?: any ) => void ) : this {
4949 this . _event . on ( event , listener ) ;
5050 return this ;
@@ -59,11 +59,54 @@ export class SevenZipper {
5959 }
6060 }
6161
62- Unzip ( zipFile : File , outDir ?: File ) : Promise < Error | null > {
62+ private _unzip_tar ( zipFile : File , outDir ?: File ) : Promise < Error | null > {
6363
64- if ( ! zipFile . IsFile ( ) ) {
65- throw new Error ( '\'' + zipFile . path + '\' is not exist' ) ;
66- }
64+ return new Promise ( ( resolve ) => {
65+
66+ let paramList : string [ ] = [
67+ `-xvf` ,
68+ zipFile . path
69+ ] ;
70+
71+ if ( outDir ) {
72+ outDir . CreateDir ( true ) ;
73+ }
74+
75+ paramList . push ( '-C' , outDir ? outDir . path : zipFile . dir ) ;
76+
77+ const process = new ExeFile ( ) ;
78+
79+ let err : Error | undefined ;
80+
81+ process . on ( 'error' , ( e ) => {
82+ err = e ;
83+ } ) ;
84+
85+ process . on ( 'close' , ( exitInfo ) => {
86+ if ( err ) {
87+ resolve ( err ) ;
88+ } else {
89+ if ( exitInfo . code === 0 ) {
90+ resolve ( ) ;
91+ } else {
92+ resolve ( new Error ( 'unzip error, code: ' + exitInfo . code . toString ( ) + ' !' ) ) ;
93+ }
94+ }
95+ } ) ;
96+
97+ process . on ( 'errLine' , ( line ) => {
98+ console . warn ( '[unzip] : ErrorLine : ' + line ) ;
99+ } ) ;
100+
101+ process . on ( 'line' , ( line ) => {
102+ this . _event . emit ( 'progress' , 20 , line ) ;
103+ } ) ;
104+
105+ process . Run ( 'tar' , paramList ) ;
106+ } ) ;
107+ }
108+
109+ private _unzip_zip_7z ( zipFile : File , outDir ?: File ) : Promise < Error | null > {
67110
68111 return new Promise ( ( resolve ) => {
69112
@@ -111,23 +154,50 @@ export class SevenZipper {
111154 } ) ;
112155 }
113156
157+ Unzip ( zipFile : File , outDir ?: File ) : Promise < Error | null > {
158+
159+ if ( ! zipFile . IsFile ( ) ) {
160+ throw new Error ( '\'' + zipFile . path + '\' is not exist' ) ;
161+ }
162+
163+ if ( platform . osType ( ) != 'win32' && zipFile . suffix . startsWith ( 'tar' ) ) {
164+ return this . _unzip_tar ( zipFile , outDir ) ;
165+ } else {
166+ return this . _unzip_zip_7z ( zipFile , outDir ) ;
167+ }
168+ }
169+
114170 UnzipSync ( zipFile : File , outDir ?: File ) : string {
115171
116172 if ( ! zipFile . IsFile ( ) ) {
117173 throw new Error ( '\'' + zipFile . path + '\' is not exist' ) ;
118174 }
119175
120- let paramList : string [ ] = [ ] ;
121- paramList . push ( 'x' ) ;
122- paramList . push ( '-y' ) ;
123- paramList . push ( '-r' ) ;
124- paramList . push ( '-aoa' ) ;
125- paramList . push ( zipFile . path ) ;
176+ // use tar
177+ if ( platform . osType ( ) != 'win32' && zipFile . suffix . startsWith ( 'tar' ) ) {
178+
179+ let paramList : string [ ] = [ ] ;
180+
181+ paramList . push ( '-xvf' ) ;
182+ paramList . push ( zipFile . path ) ;
183+ paramList . push ( '-C' , outDir ? outDir . path : zipFile . dir ) ;
184+
185+ return child_process . execFileSync ( 'tar' , paramList ) . toString ( ) ;
186+ }
187+ // use 7z
188+ else {
126189
127- const outPath = ( outDir ? outDir . path : zipFile . dir ) ;
128- paramList . push ( '-o' + outPath ) ;
190+ let paramList : string [ ] = [ ] ;
129191
130- return child_process . execFileSync ( this . _7za . path , paramList , { windowsHide : true } ) . toString ( ) ;
192+ paramList . push ( 'x' ) ;
193+ paramList . push ( '-y' ) ;
194+ paramList . push ( '-r' ) ;
195+ paramList . push ( '-aoa' ) ;
196+ paramList . push ( zipFile . path ) ;
197+ paramList . push ( '-o' + ( outDir ? outDir . path : zipFile . dir ) ) ;
198+
199+ return child_process . execFileSync ( this . _7za . path , paramList , { windowsHide : true } ) . toString ( ) ;
200+ }
131201 }
132202
133203 Zip ( dirOrFile : File , option : CompressOption , outDir ?: File ) : Promise < Error | null > {
0 commit comments