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.

Please read the security warning in FileReader VERY CAREFULLY before you use this module.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri) ⇒ RemoteUncap

Returns a new instance of RemoteUncap.



21
22
23
# File 'lib/zip_tricks/remote_uncap.rb', line 21

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

Class Method Details

.files_within_zip_at(uri, reader_class: ZipTricks::FileReader, **options_for_zip_reader) ⇒ Array<ZipTricks::FileReader::ZipEntry>

Returns metadata about the files within the remote archive.

Parameters:

  • uri (String)

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

  • reader_class (Class) (defaults to: ZipTricks::FileReader)

    which class to use for reading

  • options_for_zip_reader (Hash)

    any additional options to give to FileReader when reading

Returns:



14
15
16
17
18
19
# File 'lib/zip_tricks/remote_uncap.rb', line 14

def self.files_within_zip_at(uri, reader_class: ZipTricks::FileReader, **options_for_zip_reader)
  fetcher = new(uri)
  fake_io = ZipTricks::RemoteIO.new(fetcher)
  reader = reader_class.new
  reader.read_zip_structure(io: fake_io, **options_for_zip_reader)
end

Instance Method Details

#request_object_sizeFixnum

Only used internally when reading the remote ZIP.

Returns:

  • (Fixnum)

    the byte size of the ranged request



39
40
41
42
# File 'lib/zip_tricks/remote_uncap.rb', line 39

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

Only used internally when reading the remote ZIP.

Parameters:

  • range (Range)

    the HTTP range of data to fetch from remote

Returns:

  • (String)

    the response body of the ranged request



29
30
31
32
33
34
# File 'lib/zip_tricks/remote_uncap.rb', line 29

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