2015-09-07 38 views
4

我使用Easyzip生成了一個zip文件。我可以直接從下載文件夾中打開它。但是當下載後嘗試打開它時,出現此錯誤: - '解壓縮文件時發生錯誤'。EasyZip生成的Zip無法正常工作

這是我的後端代碼: -

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); 
    }); 
}); 

我angular.js代碼

$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'); 

     }); 

請幫我解決這些。提前致謝。

回答

0

我有同樣的問題,並能夠通過在$ http.post()請求中使用: {responseType:'arraybuffer'}來解決它。 有關更多詳細信息,請參閱:how to download a zip file using angular以及AngularJS: Display blob (.pdf) in an angular app

+0

感謝。它正在工作..但現在我可以使用存檔管理器打開它。但是我無法使用'extract here'選項提取文件 –

+0

它顯示一些錯誤嗎? – OutOfMind

+0

是的。它顯示這個錯誤框: - '提取文件時出錯'。我無法查看我的文件 –

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

它不適用於Safari瀏覽器。無法在safari中打開zip文件。怎麼解決? –