Class: Rpub::Compressor
- Inherits:
-
Object
- Object
- Rpub::Compressor
- 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
- #zip ⇒ ZipOutputStream readonly
Class Method Summary collapse
-
.open(filename) {|compressor| ... } ⇒ Object
Convenience method for opening a stream, allowing content to be written and finally closing the stream again.
Instance Method Summary collapse
-
#close ⇒ Object
Close the zip stream and write the file to disk.
-
#compress_file(filename, content) ⇒ Object
Store a file with maximum compression in the archive.
-
#initialize(filename) ⇒ Compressor
constructor
A new instance of Compressor.
-
#store_file(filename, content) ⇒ Object
Store a file in the archive without any compression.
Constructor Details
#initialize(filename) ⇒ Compressor
Returns a new instance of Compressor.
19 20 21 |
# File 'lib/rpub/compressor.rb', line 19 def initialize(filename) @zip = Zip::ZipOutputStream.new(filename) end |
Instance Attribute Details
#zip ⇒ ZipOutputStream (readonly)
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.
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
#close ⇒ Object
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.
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.
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 |