Class: HttpZip::Entry

Inherits:
Object
  • Object
show all
Defined in:
lib/http_zip/entry.rb

Overview

Describes one entry in an HTTP zip archive

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, name, header_offset, central_directory_file_compressed_size, central_directory_file_uncompressed_size) ⇒ Entry

Returns a new instance of Entry.


9
10
11
12
13
14
15
# File 'lib/http_zip/entry.rb', line 9

def initialize(url, name, header_offset, central_directory_file_compressed_size, central_directory_file_uncompressed_size)
  @range_request = HttpZip::RangeRequest.new(url)
  @name = name
  @header_offset = header_offset
  @compressed_size = central_directory_file_compressed_size
  @uncompressed_size = central_directory_file_uncompressed_size
end

Instance Attribute Details

#compressed_sizeObject (readonly)

Returns the value of attribute compressed_size.


7
8
9
# File 'lib/http_zip/entry.rb', line 7

def compressed_size
  @compressed_size
end

#nameString (readonly)

filename of the entry

Returns:

  • (String)

    the current value of name


6
7
8
# File 'lib/http_zip/entry.rb', line 6

def name
  @name
end

#uncompressed_sizeObject (readonly)

Returns the value of attribute uncompressed_size.


7
8
9
# File 'lib/http_zip/entry.rb', line 7

def uncompressed_size
  @uncompressed_size
end

Instance Method Details

#readObject

Get the decompressed content of the file entry Makes 2 HTTP requests (GET, GET)


19
20
21
22
23
24
25
26
27
# File 'lib/http_zip/entry.rb', line 19

def read
  # decompress the file
  from = @header_offset + header_size
  to = @header_offset + header_size + @compressed_size

  decompressor = compression_method
  compressed_contents = @range_request.get(from, to)
  decompressor.decompress(compressed_contents)
end

#write_to_file(filename) ⇒ Object

Get the decompressed content of the file entry Makes 2 HTTP requests (GET, GET)


31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/http_zip/entry.rb', line 31

def write_to_file(filename)
  from = @header_offset + header_size
  to = @header_offset + header_size + @compressed_size

  decompressor = compression_method
  ::File.open(filename, "wb") do |out_file|
    @range_request.get(from, to) do |chunk|
      decompressed = decompressor.decompress(chunk)
      out_file.write(decompressed)
    end
    decompressor.finish
  end
end