1414 * limitations under the License.
1515 *******************************************************************************/
1616
17- var fstream = require ( 'fstream' )
18- var tar = require ( 'tar' )
19- var zlib = require ( 'zlib' )
2017var fs = require ( 'fs' )
2118var path = require ( 'path' )
19+ var JSZip = require ( "jszip" ) ;
2220
2321var dirToWriteTo ;
2422
@@ -43,7 +41,7 @@ module.exports.setHeadlessOutputDir = function setHeadlessOutputDir(dir) {
4341
4442function timestamp ( ) {
4543 var date = new Date ( Date . now ( ) )
46- var timestamp = pad ( date . getDate ( ) . toString ( ) ) + pad ( date . getMonth ( ) . toString ( ) ) + date . getFullYear ( ) . toString ( ) . substr ( 2 , 3 ) + '_'
44+ var timestamp = pad ( date . getDate ( ) . toString ( ) ) + pad ( ( date . getMonth ( ) + 1 ) . toString ( ) ) + date . getFullYear ( ) . toString ( ) . substr ( 2 , 3 ) + '_'
4745 + pad ( date . getHours ( ) . toString ( ) ) + pad ( date . getMinutes ( ) . toString ( ) ) + pad ( date . getSeconds ( ) . toString ( ) ) + '_'
4846 + process . pid
4947 return timestamp
@@ -65,19 +63,20 @@ module.exports.headlessZip = function headlessZip(dirToZip) {
6563 outputFileName = 'nodeappmetrics' + timestamp ( ) + '.hcd'
6664 }
6765
68- var packer = tar . Pack ( { fromBase : true } )
69- . on ( 'error' , onError )
70- var zipper = zlib . Gzip ( )
71- var writer = fstream . Writer ( { 'path' : outputFileName } )
72- . on ( 'error' , onError )
73- . on ( 'close' , function ( ) {
74- deleteDir ( dirToZip )
75- } )
76-
77- fstream . Reader ( { 'path' : dirToZip , 'type' : 'Directory' } )
78- . on ( 'error' , onError )
79- . pipe ( packer )
80- . pipe ( zipper )
81- . pipe ( writer )
66+ var zip = new JSZip ( ) ;
67+ fs . readdir ( dirToZip , function ( error , files ) {
68+ if ( error ) {
69+ onError ( error )
70+ return
71+ }
72+ for ( var i = 0 , len = files . length ; i < len ; i ++ ) {
73+ zip . file ( files [ i ] , fs . readFileSync ( path . join ( dirToZip , files [ i ] ) ) , { compression : "DEFLATE" } )
74+ }
8275
76+ zip . generateNodeStream ( { type :'nodebuffer' , streamFiles :true } )
77+ . pipe ( fs . createWriteStream ( outputFileName ) )
78+ . on ( 'finish' , function ( ) {
79+ deleteDir ( dirToZip )
80+ } )
81+ } )
8382}
0 commit comments