Class: ZipTricks::RemoteUncap

Inherits:
Object
  • Object
show all
Defined in:
lib/zip_tricks/remote_uncap.rb

Overview

Alows reading the central directory of a remote ZIP file without downloading the entire file. The central directory provides the offsets at which the actual file contents is located. You can then use the Range: HTTP headers to download those entries separately.

Defined Under Namespace

Classes: RemoteZipEntry

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri) ⇒ RemoteUncap

Returns a new instance of RemoteUncap.



55
56
57
# File 'lib/zip_tricks/remote_uncap.rb', line 55

def initialize(uri)
  @uri = URI(uri)
end

Class Method Details

.files_within_zip_at(uri) ⇒ Array<RemoteZipEntry>

Returns metadata about the files within the remote archive.

Parameters:

  • uri (String)

    the HTTP(S) URL to read the ZIP footer from

Returns:

  • (Array<RemoteZipEntry>)

    metadata about the files within the remote archive



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/zip_tricks/remote_uncap.rb', line 37

def self.files_within_zip_at(uri)
  fetcher = new(uri)
  fake_io = ZipTricks::RemoteIO.new(fetcher)
  dir = Zip::CentralDirectory.read_from_stream(fake_io)

  dir.entries.map do | rubyzip_entry |
    RemoteZipEntry.new do | entry |
      entry.name = rubyzip_entry.name
      entry.size_uncompressed = rubyzip_entry.size
      entry.size_compressed = rubyzip_entry.compressed_size
      entry.compression_method = rubyzip_entry.compression_method

      entry.starts_at_offset = rubyzip_entry.local_header_offset + rubyzip_entry.calculate_local_header_size
      entry.ends_at_offset = entry.starts_at_offset + rubyzip_entry.compressed_size
    end
  end
end

Instance Method Details

#request_object_sizeFixnum

Returns the byte size of the ranged request.

Returns:

  • (Fixnum)

    the byte size of the ranged request



69
70
71
72
# File 'lib/zip_tricks/remote_uncap.rb', line 69

def request_object_size
  http = Net::HTTP.start(@uri.hostname, @uri.port)
  http.request_head(uri)['Content-Length'].to_i
end

#request_range(range) ⇒ String

Returns the response body of the ranged request.

Parameters:

  • range (Range)

    the HTTP range of data to fetch from remote

Returns:

  • (String)

    the response body of the ranged request



61
62
63
64
65
66
# File 'lib/zip_tricks/remote_uncap.rb', line 61

def request_range(range)
  request = Net::HTTP::Get.new(@uri)
  request.range = range
  http = Net::HTTP.start(@uri.hostname, @uri.port)
  http.request(request).body
end