簡體   English   中英

如何使用 Node Js、Archiver 和 Express 正確下載 a.zip 文件

[英]How to properly download a .zip file using Node Js, Archiver and Express

我正在嘗試 zip 兩個目錄的內容並下載生成的.zip 文件。 一個目錄包含.txt 文件,另一個目錄包含.jpg。 我正在使用歸檔器來 zip 文件並在節點 js 上運行 express 框架。 我傾向於認為問題存在於下載步驟中,因為在項目根目錄中創建的壓縮文件按預期展開,但是當下載文件時,我收到“錯誤 2 - 沒有這樣的文件或目錄。 "

    app.get('/download',function(req, res){

        zipFile = new Date() + "-Backup.zip";

        var output = fs.createWriteStream(__dirname +"/backups/"+ zipFile);
        var archive = archiver('zip');

        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('error', function(err) {
            throw err;
        });

        archive.pipe(output);

        var files1 = fs.readdirSync(__dirname+'/posts');
        var files2 = fs.readdirSync(__dirname+'/uploads');
            for(var i = 0; i< files1.length; i++){
                archive.append(fs.createReadStream(__dirname+"/posts/"+files1[i]), { name: files1[i] });
            }
            for(var i = 0; i< files2.length; i++){
                archive.append(fs.createReadStream(__dirname+"/uploads/"+files2[i]), { name: files2[i] });
            }
        archive.finalize(); 
        res.download(__dirname + "/backups/" + zipFile, zipFile); 

    });

zipFile 是一個全局變量。

on 'close' 日志正確觸發並且沒有發生錯誤,但文件在下載后不會打開。 響應標頭或其他我不知道的問題是否存在問題?

謝謝您的幫助。

我使用node-zip作為存檔實用程序解決了自己的問題。

var zip = require('node-zip')(); // require the node-zip utility
var fs = require('fs'); // I use fs to read the directories for their contents

var zipName = "someArbitraryName.zip"; // This just creates a variable to store the name of the zip file that you want to create
var someDir = fs.readdirSync(__dirname+"/nameOfDirectoryToZip"); // read the directory that you would like to zip
var newZipFolder = zip.folder('nameOfDirectoryToZip'); // declare a folder with the same name as the directory you would like to zip (we'll later put the read contents into this folder)


//append each file in the directory to the declared folder
for(var i = 0; i < someDir.length,i++){
    newZipFolder.file(someDir[i], fs.readFileSync(__dirname+"/nameOfDirectoryToZip/"+someDir[i]),{base64:true});
}

var data = zip.generate({base64:false,compression:'DEFLATE'}); //generate the zip file data

//write the data to file
fs.writeFile(__dirname +"/"+ zipName, data, 'binary', function(err){
    if(err){
        console.log(err);
    }
    // do something with the new zipped file
}

本質上,正在發生的事情可以分為三個步驟:

  1. 使用fs讀取您要壓縮的目錄的內容。
  2. 使用zip.folder聲明文件夾,然后使用zip.file將文件追加到該目錄。 我只是使用了for循環來迭代地將每個文件添加到在步驟1中讀取的目錄中。
  3. 使用zip.generate創建.zip文件數據,然后使用fs將其寫入文件。

可以下載生成的文件,也可以進行任何操作。 使用此方法我沒有發現任何問題。

如果要壓縮多個目錄,請在執行zip.generate之前重復步驟1和2,為每個目錄創建一個新的zip.folder。

只需使用

archive.on("finish",function(){
    res.download(__dirname + "/backups/" + zipFile);
  })

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM