简体   繁体   English

使用带有可下载在线链接的归档模块

[英]using archiver module with downloadable online links

In a node application, I wish to download a zip file that contains pdfs downloaded from various urls on the internet (where if I type the url into a browser, it just directs me to download a pdf).在节点应用程序中,我希望下载一个 zip 文件,其中包含从 Internet 上的各种 url 下载的 pdf(如果我在浏览器中键入 url,它只会指示我下载 pdf)。 I've been using the archiver module which is documented on github at https://github.com/archiverjs/node-archiver , and the official documentation is at https://www.archiverjs.com/ .我一直在使用归档器模块,该模块记录在https://github.com/archiverjs/node-archiver的 github 上,官方文档位于Z5E056C500A1C4B6A7110.archiverjs.com7A7110BADEiverD8

I'm stuck at the part where it gives the following examples for adding files to the zip file.我被困在它提供以下示例的部分,用于将文件添加到 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');

Unfortunately, it seems just pasting urls into the first parameter of.append or.directory doesn't work - would anyone know how I can add downloadable files (that are online) into the zip file?不幸的是,似乎只是将 url 粘贴到 .append 或 .directory 的第一个参数中不起作用 - 有人知道我如何将可下载文件(在线)添加到 zip 文件中吗?

sure, using download-pdf first something like that当然,首先使用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