Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Subdirectories in Zip file using ZipOutputStream

Tags:

ruby

zip

rubyzip

I am creating a zip file using the technique described here:

http://info.michael-simons.eu/2008/01/21/using-rubyzip-to-create-zip-files-on-the-fly/

The client has asked that I include subdirectories in this zip file. I have searched the ZipOutputStream documentation but I see no way to include directories. Is there a way I can do this with ZipOutputStream? Should I be using a different class than ZipOutputStream?

Also, the files that I am zipping are not on the local filesystem, but are stored in a cloud service.

like image 443
Scott Rice Avatar asked Dec 06 '11 02:12

Scott Rice


1 Answers

Figured out the answer, thought I would put it here for search engines to find.

Anyway, the link I posted used

zos.put_next_entry("some-funny-name.jpg")

to add files to the zip file. Turns out, that string parameter isn't just a filename, it can be a path as well! So using

zos.put_next_entry("some-random-folder/some-funny-name.jpg")

Will make your zip file contain a folder called 'some-random-folder', with a file called 'some-funny-name.jpg'.

like image 171
Scott Rice Avatar answered Sep 24 '22 01:09

Scott Rice