# Is it possible for rubyzip to return zipped data instead of writing it to the disk?

**URL:** <https://www.ruby-forum.com/t/is-it-possible-for-rubyzip-to-return-zipped-data-instead-of-writing-it-to-the-disk/138189>\
**Category:** Ruby\
**Created:** [May 20, 2008, 11:05pm UTC](https://www.ruby-forum.com/t/is-it-possible-for-rubyzip-to-return-zipped-data-instead-of-writing-it-to-the-disk/138189 "2008-05-20T23:05:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![g0nzo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/3bc359/32.png) [@g0nzo](https://www.ruby-forum.com/u/g0nzo)\
**Post date:** [May 20, 2008, 11:05pm UTC](https://www.ruby-forum.com/t/is-it-possible-for-rubyzip-to-return-zipped-data-instead-of-writing-it-to-the-disk/138189/1 "2008-05-20T23:05:58Z")

</div>

Hi,

I got simple Rails app and I’d like to compress xml data and send it  
to the browser. I’m trying to use rubyzip for it, but i.e. the  
following usage:

Zip::ZipFile.open(“file.zip”, Zip::ZipFile::CREATE) do |zip|  
zip.get\_output\_stream(“file.xml”) {|f| f.puts xml\_data}  
end

returns nil and creates file on the disk.

Is it possible for rubyzip to return zipped data (or file) instead of  
writing it to the disk?

---

<div class="post-metadata">

**Author:** ![g0nzo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/3bc359/32.png) [@g0nzo](https://www.ruby-forum.com/u/g0nzo)\
**Post date:** [May 20, 2008, 11:17pm UTC](https://www.ruby-forum.com/t/is-it-possible-for-rubyzip-to-return-zipped-data-instead-of-writing-it-to-the-disk/138189/2 "2008-05-20T23:17:08Z")

</div>

-----BEGIN PGP SIGNED MESSAGE-----  
Hash: SHA1

szimek wrote:  
| Hi,  
|  
| I got simple Rails app and I’d like to compress xml data and send it  
| to the browser. I’m trying to use rubyzip for it, but i.e. the  
| following usage:  
|  
| Zip::ZipFile.open(“file.zip”, Zip::ZipFile::CREATE) do |zip|  
| zip.get\_output\_stream(“file.xml”) {|f| f.puts xml\_data}  
| end  
|  
| returns nil and creates file on the disk.  
|  
| Is it possible for rubyzip to return zipped data (or file) instead of  
| writing it to the disk?

Write file to disk, give user link, delete file after a time, in case  
the download has to be repeated.

Caching saves CPU and memory. 🙂

* * *

Phillip G.  
Twitter: [twitter.com/cynicalryan](http://twitter.com/cynicalryan)  
Blog: [http://justarubyist.blogspot.com](http://justarubyist.blogspot.com)

Write first in an easy-to-understand pseudo-language; then translate  
into  
whatever language you have to use.  
~ - The Elements of Programming Style (Kernighan & Plaugher)  
-----BEGIN PGP SIGNATURE-----  
Version: GnuPG v1.4.8 (MingW32)  
Comment: Using GnuPG with Mozilla - [http://enigmail.mozdev.org](http://enigmail.mozdev.org)

iEYEARECAAYFAkgzP6sACgkQbtAgaoJTgL8o4ACfcp/wTC7QT52nXFDre9I+YRck  
iBEAn1x/BtR99VLngUBmvOSmD3EN8mCk  
=L8uK  
-----END PGP SIGNATURE-----

---

<div class="post-metadata">

**Author:** ![g0nzo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/3bc359/32.png) [@g0nzo](https://www.ruby-forum.com/u/g0nzo)\
**Post date:** [May 21, 2008, 12:12am UTC](https://www.ruby-forum.com/t/is-it-possible-for-rubyzip-to-return-zipped-data-instead-of-writing-it-to-the-disk/138189/3 "2008-05-21T00:12:23Z")

</div>

2008/5/20, Phillip G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> |  
> Write file to disk, give user link, delete file after a time, in case  
> whatever language you have to use.

Thanks.

I wanted to use Rails caching mechanisms (which I’m using i.e. to  
cache generated XML datta to file). The result is the same - I got  
zipped XML file on a disk - but by having raw data instead of a file  
on a disk, I could have slightly cleaner code.

BTW. Is there some option to force rubyzip not to make temp files or  
remove them after it’s done? I can remove them manually, but it would  
be easier 🙂

Regards
