Class: RubyArchive::Handlers::ZipHandler

Inherits:
RubyArchive::Handler show all
Defined in:
lib/ruby_archive/handlers/zip_handler.rb

Constant Summary collapse

ZIP_MAGIC =
"\x50\x4B\x03\x04"

Instance Attribute Summary

Attributes inherited from RubyArchive::Handler

#name

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from RubyArchive::Handler

#inspect, normalize_path

Constructor Details

#initialize(location) ⇒ ZipHandler

Returns a new instance of ZipHandler.



21
22
23
24
25
26
# File 'lib/ruby_archive/handlers/zip_handler.rb', line 21

def initialize location
  # use normalized location because Zip::ZipFile.open does not expand '~'
  normalized_location = RubyArchive::Handler::normalize_path(location)
  @zipfs = Zip::ZipFile.open(normalized_location)
  @name = normalized_location
end

Class Method Details

.handles?(location) ⇒ Boolean

Checks the magic of the given file to check if it’s a zip file

Returns:

  • (Boolean)


13
14
15
16
17
18
19
# File 'lib/ruby_archive/handlers/zip_handler.rb', line 13

def self.handles? location
  # use normalized location because Zip::ZipFile.open does not expand '~'
  normalized_location = RubyArchive::Handler::normalize_path(location)
  return false unless File.file?(normalized_location)
  return false unless File.read(normalized_location, ZIP_MAGIC.length) == ZIP_MAGIC
  return true
end

Instance Method Details

#closeObject



28
29
30
# File 'lib/ruby_archive/handlers/zip_handler.rb', line 28

def close
  @zipfs.close
end

#dirObject



36
37
38
# File 'lib/ruby_archive/handlers/zip_handler.rb', line 36

def dir
  @zipfs.dir
end

#fileObject



32
33
34
# File 'lib/ruby_archive/handlers/zip_handler.rb', line 32

def file
  @zipfs.file
end