簡體   English   中英

使用帶有可下載在線鏈接的歸檔模塊

[英]using archiver module with downloadable online links

在節點應用程序中,我希望下載一個 zip 文件,其中包含從 Internet 上的各種 url 下載的 pdf(如果我在瀏覽器中鍵入 url,它只會指示我下載 pdf)。 我一直在使用歸檔器模塊,該模塊記錄在https://github.com/archiverjs/node-archiver的 github 上,官方文檔位於Z5E056C500A1C4B6A7110.archiverjs.com7A7110BADEiverD8

我被困在它提供以下示例的部分,用於將文件添加到 zip 文件。

// append a file from stream
var file1 = __dirname + '/file1.txt';
archive.append(fs.createReadStream(file1), { name: 'file1.txt' });

// append a file from string
archive.append('string cheese!', { name: 'file2.txt' });

// append a file from buffer
var buffer3 = Buffer.from('buff it!');
archive.append(buffer3, { name: 'file3.txt' });

// append a file
archive.file('file1.txt', { name: 'file4.txt' });

// append files from a sub-directory and naming it `new-subdir` within the archive
archive.directory('subdir/', 'new-subdir');

// append files from a sub-directory, putting its contents at the root of archive
archive.directory('subdir/', false);

// append files from a glob pattern
archive.glob('subdir/*.txt');

不幸的是,似乎只是將 url 粘貼到 .append 或 .directory 的第一個參數中不起作用 - 有人知道我如何將可下載文件(在線)添加到 zip 文件中嗎?

當然,首先使用download-pdf類似的東西

var download = require('download-pdf')
var fs = require('fs');
var archiver = require('archiver');
var output = fs.createWriteStream('./example.zip');
var archive = archiver('zip', {
    gzip: true,
zlib: { level: 9 } // Sets the compression level.
});

var pdf = "http://www.consejoconsultivoemt.cl/wp-content/uploads/2018/12 /Presentaci%C3%B3n-Lineamientos-Estrat%C3%A9gicos-de-Corfo.pdf"
var pdf2 = "https://www.biobiochile.cl/static/tarifas.pdf"

var options = {
    directory: "./files/",
    filename: "first.pdf"
}
var options2 = {
    directory: "./files/",
    filename: "second.pdf"
}

download(pdf, options, function (err) {
    if (err) throw err
    console.log("meow")
})

download(pdf2, options2, function (err) {
    if (err) throw err
    console.log("meow2")
})

archive.on('error', function (err) {
    throw err;
});

// pipe archive data to the output file
archive.pipe(output);

// append files
archive.file('./files/first.pdf', { name: 'first.pdf' });
archive.file('./files/second.pdf', { name: 'second.pdf' });

archive.finalize();

暫無
暫無

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

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