Class: Zippo::Filter::Compressor

Inherits:
Object
  • Object
show all
Includes:
Base
Defined in:
lib/zippo/filter/compressor.rb

Overview

A compressor is responsible for writing (and likely compressing) data into a zip file.

Subclasse should include a METHOD constant to indicate which zip compression method they handle. The actual compression should be implemented in the filter and tail_filter methods.

Examples:

Obtaining a compressor

compression_method = 8
compressor = Compressor.for(compression_method).new(io)
compressor.compress_to out

Direct Known Subclasses

DeflateCompressor, StoreCompressor

Constant Summary

Constants included from Base

Base::BLOCK_SIZE

Instance Method Summary collapse

Methods included from Base

included

Constructor Details

#initialize(io) ⇒ Compressor

Returns a new instance of Compressor.



20
21
22
# File 'lib/zippo/filter/compressor.rb', line 20

def initialize(io)
  @io = io
end

Instance Method Details

#compress_to(io) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/zippo/filter/compressor.rb', line 28

def compress_to(io)
  data_size = 0
  compressed_size = 0
  crc32 = 0
  buf = ""
  while read BLOCK_SIZE, buf
    data_size += buf.bytesize
    compressed_size += io.write filter(buf)
    crc32 = Zlib.crc32(buf, crc32)
  end
  compressed_size += io.write tail_filter
  [compressed_size, data_size, crc32]
end

#read(count = nil, buf = nil) ⇒ Object



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

def read(count = nil, buf = nil)
  @io.read count, buf
end