Class: SafeZip::Entry

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(zip_archive, zip_entry, params) ⇒ Entry

Returns a new instance of Entry.


8
9
10
11
12
13
# File 'lib/safe_zip/entry.rb', line 8

def initialize(zip_archive, zip_entry, params)
  @zip_archive = zip_archive
  @zip_entry = zip_entry
  @params = params
  @path = ::File.expand_path(zip_entry.name, params.extract_path)
end

Instance Attribute Details

#paramsObject (readonly)

Returns the value of attribute params.


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

def params
  @params
end

#pathObject (readonly)

Returns the value of attribute path.


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

def path
  @path
end

#zip_archiveObject (readonly)

Returns the value of attribute zip_archive.


5
6
7
# File 'lib/safe_zip/entry.rb', line 5

def zip_archive
  @zip_archive
end

#zip_entryObject (readonly)

Returns the value of attribute zip_entry.


5
6
7
# File 'lib/safe_zip/entry.rb', line 5

def zip_entry
  @zip_entry
end

Instance Method Details

#exist?Boolean

Returns:

  • (Boolean)

23
24
25
# File 'lib/safe_zip/entry.rb', line 23

def exist?
  ::File.exist?(path)
end

#extractObject


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/safe_zip/entry.rb', line 27

def extract
  # do not extract if file is not part of target directory
  return false unless matching_target_directory

  # do not overwrite existing file
  raise SafeZip::Extract::AlreadyExistsError, "File already exists #{zip_entry.name}" if exist?

  create_path_dir

  if zip_entry.file?
    extract_file
  elsif zip_entry.directory?
    extract_dir
  elsif zip_entry.symlink?
    extract_symlink
  else
    raise SafeZip::Extract::UnsupportedEntryError, "File #{zip_entry.name} cannot be extracted"
  end
rescue SafeZip::Extract::Error
  raise
rescue StandardError => e
  raise SafeZip::Extract::ExtractError, e.message
end

#path_dirObject


15
16
17
# File 'lib/safe_zip/entry.rb', line 15

def path_dir
  ::File.dirname(path)
end

#real_path_dirObject


19
20
21
# File 'lib/safe_zip/entry.rb', line 19

def real_path_dir
  ::File.realpath(path_dir)
end