class Decidim::FileZipper

This class performs a simple task: Zipping a single file. Originally meant for mailers to attach files, but other usage can be found.

Public Class Methods

new(filename, data) click to toggle source

Public: Initializes the zipper with a filename and the data to be zipped.

filename - The file name of the file inside the zip. data - A string with the data to be zipped.

# File lib/decidim/file_zipper.rb, line 14
def initialize(filename, data)
  @data = data
  @filename = filename
end

Public Instance Methods

zip() click to toggle source

Public: Zips the file.

Returns a String with the zipped version of the file.

# File lib/decidim/file_zipper.rb, line 22
def zip
  @zip ||= Zip::OutputStream.write_buffer do |zipfile|
    zipfile.put_next_entry(@filename)
    zipfile.write @data
  end.string
end