Class: Rpub::Compressor

Inherits:
Object
  • Object
show all
Defined in:
lib/rpub/compressor.rb

Overview

Wrapper around a +ZipOutputStream+ object provided by the +rubyzip+ gem. This writes string contents straight into a zip file, without first saving them to disk.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ Compressor

Returns a new instance of Compressor.

Parameters:

  • filename (String)

    of the archive to write to disk



19
20
21
# File 'lib/rpub/compressor.rb', line 19

def initialize(filename)
  @zip = Zip::ZipOutputStream.new(filename)
end

Instance Attribute Details

#zipZipOutputStream (readonly)

Returns:

  • (ZipOutputStream)


8
9
10
# File 'lib/rpub/compressor.rb', line 8

def zip
  @zip
end

Class Method Details

.open(filename) {|compressor| ... } ⇒ Object

Convenience method for opening a stream, allowing content to be written and finally closing the stream again.

Yields:

  • (compressor)


12
13
14
15
16
# File 'lib/rpub/compressor.rb', line 12

def self.open(filename)
  compressor = new(filename)
  yield compressor
  compressor.close
end

Instance Method Details

#closeObject

Close the zip stream and write the file to disk.



24
25
26
# File 'lib/rpub/compressor.rb', line 24

def close
  zip.close
end

#compress_file(filename, content) ⇒ Object

Store a file with maximum compression in the archive.

Parameters:

  • filename (String)

    under the which the data should be stored

  • content (#to_s)

    to be compressed



41
42
43
44
# File 'lib/rpub/compressor.rb', line 41

def compress_file(filename, content)
  zip.put_next_entry filename, nil, nil, Zip::ZipEntry::DEFLATED, Zlib::BEST_COMPRESSION
  zip.write content.to_s
end

#store_file(filename, content) ⇒ Object

Store a file in the archive without any compression.

Parameters:

  • filename (String)

    under the which the data should be stored

  • content (#to_s)

    to be compressed



32
33
34
35
# File 'lib/rpub/compressor.rb', line 32

def store_file(filename, content)
  zip.put_next_entry filename, nil, nil, Zip::ZipEntry::STORED, Zlib::NO_COMPRESSION
  zip.write content.to_s
end