Class: Zippo::Filter::Uncompressor

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

Overview

An Uncompressor is responsible for reading (and likely uncompressing) member data from a Zip file.

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

Examples:

Obtaining an uncompressor

compression_method = 8
uncompressor = Uncompressor.for(compression_method).new(io)
uncompressor.uncompress_to out

Direct Known Subclasses

DeflateUncompressor, StoreUncompressor

Constant Summary

Constants included from Base

Base::BLOCK_SIZE

Instance Method Summary collapse

Methods included from Base

included

Constructor Details

#initialize(io, compressed_size) ⇒ Uncompressor

Returns a new instance of Uncompressor.

Parameters:

  • io (IO)

    the IO the member data is located in

  • compressed_size (Integer)

    the compressed size of the data



21
22
23
24
25
26
27
28
# File 'lib/zippo/filter/uncompressor.rb', line 21

def initialize(io, compressed_size)
  # XXX should probably capture the offset here
  #     current usage assumes the IO has already been positioned at
  #     the appropriate location
  @io = io
  @compressed_size = compressed_size
  @remaining = @compressed_size
end

Instance Method Details

#read(n, buf = nil) ⇒ Object



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

def read(n, buf = nil)
  if @remaining >= n
    @remaining -= n
  elsif (n = @remaining) > 0
    @remaining = 0
  else
    return nil
  end

  @io.read n, buf
  buf.replace filter(buf)
end

#uncompressString

Returns the uncompressed data.

Returns:

  • (String)

    the uncompressed data



53
54
55
# File 'lib/zippo/filter/uncompressor.rb', line 53

def uncompress
  uncompress_to ""
end

#uncompress_to(io) ⇒ Object

Uncompresses the data to the specified IO

Parameters:

  • io (IO)

    the object to uncompress to, must respond to #<<



46
47
48
49
50
# File 'lib/zippo/filter/uncompressor.rb', line 46

def uncompress_to(io)
  buf = ""
  io << buf while read BLOCK_SIZE, buf
  io << tail_filter
end