Sha256: 3cf70036d9923f2901ff9265052ea93a0681e721dad5b2612384d1253fa010b5

Contents?: true

Size: 1.42 KB

Versions: 3

Compression:

Stored size: 1.42 KB

Contents

module Rpub
  # 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.
  class Compressor

    # @return [ZipOutputStream]
    attr_reader :zip

    # Convenience method for opening a stream, allowing content to be written
    # and finally closing the stream again.
    def self.open(filename)
      compressor = new(filename)
      yield compressor
      compressor.close
    end

    # @param [String] filename of the archive to write to disk
    def initialize(filename)
      @zip = Zip::ZipOutputStream.new(filename)
    end

    # Close the zip stream and write the file to disk.
    def close
      zip.close
    end

    # Store a file in the archive without any compression.
    #
    # @param [String] filename under the which the data should be stored
    # @param [#to_s] content to be compressed
    def store_file(filename, content)
      zip.put_next_entry filename, nil, nil, Zip::ZipEntry::STORED, Zlib::NO_COMPRESSION
      zip.write content.to_s
    end

    # Store a file with maximum compression in the archive.
    #
    # @param [String] filename under the which the data should be stored
    # @param [#to_s] content to be compressed
    def compress_file(filename, content)
      zip.put_next_entry filename, nil, nil, Zip::ZipEntry::DEFLATED, Zlib::BEST_COMPRESSION
      zip.write content.to_s
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rpub-0.4.0 lib/rpub/compressor.rb
rpub-0.3.0 lib/rpub/compressor.rb
rpub-0.2.1 lib/rpub/compressor.rb