Class: Decidim::FileZipper

Inherits:
Object
  • Object
show all
Defined in:
decidim-core/lib/decidim/file_zipper.rb

Overview

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

Instance Method Summary collapse

Constructor Details

#initialize(filename, data) ⇒ FileZipper

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.


14
15
16
17
# File 'decidim-core/lib/decidim/file_zipper.rb', line 14

def initialize(filename, data)
  @data = data
  @filename = filename
end

Instance Method Details

#zipObject

Public: Zips the file.

Returns a String with the zipped version of the file.


22
23
24
25
26
27
# File 'decidim-core/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