-1

I have a node application that contains several downloadable links (when you click on the link a pdf file is downloaded), and these links are dynamically created/populated. I want to implement a feature where we can somehow download all files from these links in one go. I presume for this I will somehow need to create a zip file from all these links - would anyone know how to go about this?

user11508332
  • 537
  • 1
  • 11
  • 28

1 Answers1

1

you could use the fs and archiver module:

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

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

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

// append files
archive.file('/path/to/file0.txt', {name: 'file0-or-change-this-whatever.txt'});
archive.file('/path/to/README.md', {name: 'foobar.md'});

//
archive.finalize();
Iggnaxios
  • 155
  • 2
  • 5
  • the zip file downloaded can't be opened due to restricted permissions - is there a way to solve this in the code? – user11508332 Apr 11 '20 at 15:33
  • The answer I have told you has worked without problems. Check the paths of your files. here is a link to the repository which if the extraction of the zip folder has worked for me: https://github.com/ignacioainol/testing-archiver-node – Iggnaxios Apr 12 '20 at 07:27
  • I think it might be because in the lines with archive.file, you've done it with local files, but I'm using downloadable links from the internet (where if I type the link in a browser, it directs me to a pdf to download) - can I use links in the same way you've used paths to files? – user11508332 Apr 12 '20 at 07:29
  • then no, it is not possible. But you could download the pdf first with the download-pdf library https://www.npmjs.com/package/download-pdf – Iggnaxios Apr 12 '20 at 07:38