EasyZip generated zip not working properly

I created a zip file using Easyzip. I can open it directly from the download folder. But when you try to open it after loading, I get this error: - "Error while extracting files."

This is my code: -

var zip2 = new EasyZip();
    zip2.zipFolder('./downloads/'+application._id,function(){
        zip2.writeToFile('./downloads/'+application._id+'.zip');

        res.setHeader('Content-disposition', 'attachment; filename='+application._id+'.zip');
        res.setHeader('Content-type', 'application/zip');

        var fs = require('fs');
        var filestream = fs.createReadStream('./downloads/'+application._id+'.zip');

        filestream.pipe(res);
    });
});

My angular.js code

   $http.post('/download/archive/' + stateParams._id, {year: year}).success(function (data) {
            var file = new Blob([data], {type: 'application/zip'});
            console.log(file)
            saveAs(file, 'application.zip');

        });

Please help me solve them. Thanks in advance.

+4
source share
2 answers

I had the same problem, and I was able to solve it using: {responseType: 'arraybuffer'} in the request $ http.post (). For more information check: how to download a zip file using angular , as well as AngularJS: Show blob (.pdf) in angular app

0

​​ node -native-zip.

   var zip = require("node-native-zip");
   var files = []
   files.push({name:application._id+'.pdf',path:'./downloads/'+vetting_id+application._id+'.pdf'}); //push all the files along with its name and path
   var archive = new zip();
   archive.addFiles(files, function (err) {
    if (err) return res.status(400).send({
                        message: errorHandler.getErrorMessage(err)
                    });
    var buff = archive.toBuffer();
     var fs = require('fs');
    fs.writeFile('./downloads/'+ vetting._id+'.zip', buff, function () {
        console.log("Finished");
         var filestream = fs.createReadStream('./downloads/'+vetting._id+'.zip');
         filestream.pipe(res);
    });
0

Source: https://habr.com/ru/post/1606206/


All Articles