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)
-
- (ZipOutputStream) zip
readonly
Class Method Summary (collapse)
-
+ (Object) open(filename) {|compressor| ... }
Convenience method for opening a stream, allowing content to be written and finally closing the stream again.
Instance Method Summary (collapse)
-
- (Object) close
Close the zip stream and write the file to disk.
-
- (Object) compress_file(filename, content)
Store a file with maximum compression in the archive.
-
- (Compressor) initialize(filename)
constructor
A new instance of Compressor.
-
- (Object) store_file(filename, content)
Store a file in the archive without any compression.
Constructor Details
- (Compressor) initialize(filename)
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
- (ZipOutputStream) zip (readonly)
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.
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.
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.
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 |