Class: HttpZip::File

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

Overview

HttpZip reads ZIP-files over a HTTP connection that supports the Content-Range header. It is a helpful tool to extract single files from large HTTP archives without having to download them fully.

Resources regarding the ZIP file format: en.wikipedia.org/wiki/ZIP_(file_format) pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ File

Create a HttpZip file object that is located at url.

Parameters:

  • url (String)

    where the file is hosted

Raises:



22
23
24
25
26
# File 'lib/http_zip/file.rb', line 22

def initialize(url)
  @url = url
  @entries = nil
  @range_request = RangeRequest.new(url)
end

Instance Method Details

#entriesObject

Get all entries in the zip archive as an array of HttpZip::Entry. Makes up to 4 HTTP requests (HEAD, GET, GET, GET?)



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/http_zip/file.rb', line 30

def entries
  return @entries if @entries

  @entries = []
  last_bytes_of_file = @range_request.last(MAXIMUM_EOCD_AND_EOCD64_LOCATOR_SIZE)
  central_directory_bytes = get_central_directory(last_bytes_of_file)

  # iterate through central directory and spit out file entries
  until central_directory_bytes.empty?
    # get information about the current file entry
    file_header = HttpZip::Parser::CentralDirectoryFileHeader.new(central_directory_bytes)
    @entries << HttpZip::Entry.new(
      @url,
      file_header.file_name,
      file_header.header_offset,
      file_header.compressed_size,
      file_header.uncompressed_size
    )

    # skip ahead to next file entry
    central_directory_bytes = central_directory_bytes[(file_header.end_of_entry)..-1]
  end

  @entries
end