4

I have generated a zip file using Easyzip. I can open it directly from download folder.But when try to open it after downloading, I am getting this error:- 'error occurred while extracting files'.

This my backend 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 to solve these. Thanks in advance.

Mariya James
  • 935
  • 2
  • 9
  • 27

2 Answers2

0

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

Community
  • 1
  • 1
OutOfMind
  • 874
  • 16
  • 32
  • Thanks. It is working.. But now i can open it using archive manager. But i am unable to extract the file using 'extract here' option – Mariya James Sep 07 '15 at 11:57
  • yes. It shows this error box :-' An error while extracting files'. I can not view my files – Mariya James Sep 07 '15 at 12:17
  • Well, I am able to download as well as extract the files without any problems. There may be some problem in creating the .zip file. I see that you are using WriteToFile(). Check whether this is creating your file correctly. – OutOfMind Sep 07 '15 at 12:22
  • Can anybody suggest me good method to create zip folder in node.js or help me to find out what i am doing wrong here in my server side code. So that i can archive and extract it without errors. – Mariya James Sep 08 '15 at 05:34
0

Solved issue by using node-native-zip module.

Here is my backend code

   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);
    });
Mariya James
  • 935
  • 2
  • 9
  • 27