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

- (Compressor) initialize(filename)

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

- (ZipOutputStream) zip (readonly)

Returns:

  • (ZipOutputStream)


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

def zip
  @zip
end

Class Method Details

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

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

- (Object) close

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

- (Object) compress_file(filename, content)

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

- (Object) store_file(filename, content)

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