Crisoberillo Crisoberillo - 4 months ago 24
Node.js Question

Node.js: How to append files to a (archiverjs) zip from url

I have an object containing an array of slides. Every slide can have a media parameter containing the url to an image or video file or the link to a youtube or vimeo video.

My goal is to have the slides viewer zipped and inside the zip I also must have the image or video files taken from the urls.

To create the zip I'm currently using Archiver and it works fine, but I don't know how to put the media files inside the zip (possibly) without writing them on the filesystem first. I think I have to use streams, since archiver.append() can take a stream as first parameter, but I don't know how to do that.

I have implemented some code to understand if the url points to a file or not, writing the files' url inside an array (avoiding youtube or viemo urls).

This is how the zip is created:

...
var urls_array = ["http://url1/file1.jpg", "http://url2/file2.png"]; //the array of urls I take the media files from

var zip = archiver('zip');
zip.pipe(res);

zip.directory(__dirname + '/../../zip/', 'slideshow');
zip.append( new Buffer( file ), { name: 'slideshow/assets/slides.json' });
zip.finalize();


I suppose I have to cycle the
url_array
and for each url perform a
http.get()
call, but I can't understand how to
.pipe()
the response inside the zip.

Is there anyone who can help me?
Don't hesitate to ask me more information :)

Thank you in advance.

Answer

You should use the request method to create a stream from a remote URL to be passed to the append function of archiver, as a first argument, like this:

for ( var slide in slides ) {
    archive.append( request( slide.url ), { name: slide.name } );
}

see archiver.append documentation ( https://archiverjs.com/docs/module-plugins_zip-Zip.html#append )

Hope it helps.