0

I have to compress the contents of a directory into a single ZIP file.

I am using the following code:

var fs = require('fs');
var tar = require('tar');
var zlib = require('zlib');
var path = require('path');
var mkdirp = require('mkdirp');
var fstream = require('fstream');
fstream.Reader({path:"E:\\ddata\\electron\\Applications\\FirstApp\\js\\temp\\",type:"Directory"})
.pipe(new tar.Pack())
.pipe(zlib.Gzip()) 
.pipe(fstream.Writer({path:"C:\\Users\\Raina\\AppData\\Local\\Temp",file:'compressed_folder.tar.gz' })); 

But I am getting this error:

path.js:7 throw new TypeError('Path must be a string. Received ' + inspect(path));

How can I get rid of this error and if I have to debug where is the path.js?

waka
  • 3,362
  • 9
  • 35
  • 54
Rajeev Raina
  • 239
  • 1
  • 3
  • 8

1 Answers1

0

Find below the complete code for ZIP and UNZIP

var fs = require('fs');
var tar = require('tar');
var zlib = require('zlib');
var path = require('path');
var mkdirp = require('mkdirp');
var archiver = require('archiver');
var log = console.log;

//https://stackoverflow.com/questions/21989460/node-js-specify-files-to-unzip-with-zlib-tar

uid=Date.now().toString().substr(Date.now().toString().length - 4);

const tempDir = require('temp-dir');
var destdir = tempDir + '\\lay'+ uid + '.tmp';
mkdirp(destdir, function(err) {
    if (err) throw err;
});

function unzip() {
        
    var srcdir = 'E:/d data/electron/Applications/FirstApp/js/red.le2';
    //var destdir    = 'E:/d data/electron/Applications/FirstApp/js/temp';

    fs.createReadStream(srcdir)
        .on('error', log)
        .pipe(zlib.Unzip())
        .pipe(new tar.Parse())
        .on('entry', function(entry) {
        //  if (/\.js$/.test(entry.path))// only extract JS files, for instance
            { 
                var isDir     = 'Directory' === entry.type;
                var fullpath  = path.join(destdir, entry.path);
                var directory = isDir ? fullpath : path.dirname(fullpath);
                console.log (fullpath)
                mkdirp(directory, function(err) {
                    if (err) throw err;
                    if (! isDir) { 
                        entry.pipe(fs.createWriteStream(fullpath));
                    }
              });
            }
        });

}

        
function zip() {
    destdir='C:/Users/Raina/AppData/Local/Temp/lay0054.tmp';
    var output = fs.createWriteStream('C:/Users/Raina/AppData/Local/Temp/xx.le2');
    var archive = archiver('tar', {
        zlib: { level: 9 }
    });
    output.on('close', function() {
        console.log(archive.pointer() + ' total bytes');
        console.log('archiver has been finalized and the output file descriptor has closed.');
    });
    archive.on('warning', function(err) {
    if (err.code === 'ENOENT') {
        // log warning
    } else {
        // throw error
        throw err;
    }
    });
    archive.on('error', function(err) {
        throw err;
    });
    archive.pipe(output);
    archive.directory(destdir+'/layout', 'layout');
    archive.file(destdir+'/layout_new.xml', { name: 'layout_new.xml' });
    archive.finalize();
}

unzip();
Skulaurun Mrusal
  • 2,792
  • 1
  • 15
  • 29
Rajeev Raina
  • 239
  • 1
  • 3
  • 8