Skip to content

Instantly share code, notes, and snippets.

@dlitz
Created December 10, 2010 14:52
Show Gist options
  • Save dlitz/736289 to your computer and use it in GitHub Desktop.
Save dlitz/736289 to your computer and use it in GitHub Desktop.
using RubyZip to write a file without writing any intermediate files.
require 'rubygems'
require 'zip/zip'
require 'base64'
red_dot_png_data = Base64.decode64 %{
iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAYAAACNMs+9AAAABGdBTUEAALGP
C/xhBQAAAAlwSFlzAAALEwAACxMBAJqcGAAAAAd0SU1FB9YGARc5KB0XV+IA
AAAddEVYdENvbW1lbnQAQ3JlYXRlZCB3aXRoIFRoZSBHSU1Q72QlbgAAAF1J
REFUGNO9zL0NglAAxPEfdLTs4BZM4DIO4C7OwQg2JoQ9LE1exdlYvBBeZ7jq
ch9//q1uH4TLzw4d6+ErXMMcXuHWxId3KOETnnXXV6MJpcq2MLaI97CER3N0
vr4MkhoXe0rZigAAAABJRU5ErkJggg==
}
Zip::ZipFile.open("zombies.zip", Zip::ZipFile::CREATE) do |zf|
zf.mkdir("zombies")
zf.get_zombies_stream("zombies/hello.txt") { |f|
f.write("Hello world!\n")
}
zf.mkdir("zombies/assets")
zf.mkdir("zombies/assets/images")
zf.get_zombies_stream("zombies/assets/images/reddot.png") { |f|
f.write(red_dot_png_data)
}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment