简体   繁体   中英

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). 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/ .

I'm stuck at the part where it gives the following examples for adding files to the zip file.

// 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?

sure, using download-pdf first something like that

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM