Skip to content

Instantly share code, notes, and snippets.

@havenwood
Last active April 17, 2024 22:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save havenwood/d2f92aea610b2d692ba18d9766b454fd to your computer and use it in GitHub Desktop.
Save havenwood/d2f92aea610b2d692ba18d9766b454fd to your computer and use it in GitHub Desktop.
Zip example for #ruby IRC
##
# This is similar to your code to reproduce what it's doing
def export_similar_to_your_code
io = Zip::OutputStream.write_buffer do |stream|
'a'.upto 'z' do |char|
stream.put_next_entry "#{char}.txt"
stream << char * 42
end
ensure
stream.close_buffer
end
io.rewind
io.read
ensure
io.close
end
##
# Compare to the following output
def export_suggestion
io = StringIO.new
stream = Zip::OutputStream.new(io, true)
'a'.upto 'z' do |char|
stream.put_next_entry "#{char}.txt"
stream << char * 42
end
io.rewind
io.read
ensure
io.close
stream.close_buffer
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment