Class: ZipDir::Unzipper

Inherits:
Object
  • Object
show all
Defined in:
lib/zip_dir/unzipper.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(zip_path) ⇒ Unzipper

Returns a new instance of Unzipper.



4
5
6
# File 'lib/zip_dir/unzipper.rb', line 4

def initialize(zip_path)
  @zip_path, @unzip_path = zip_path, ::Dir.mktmpdir
end

Instance Attribute Details

#zip_pathObject (readonly)

Returns the value of attribute zip_path.



3
4
5
# File 'lib/zip_dir/unzipper.rb', line 3

def zip_path
  @zip_path
end

Instance Method Details

#cleanupObject



8
9
10
11
# File 'lib/zip_dir/unzipper.rb', line 8

def cleanup
  @unzipped = false
  FileUtils.remove_entry_secure @unzip_path
end

#unzipObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/zip_dir/unzipper.rb', line 22

def unzip
  ::Zip::File.open(zip_path) do |zip_file|
    zip_file.each do |entry|
      file_path = "#{@unzip_path}/#{entry.name}"

      # Fixes: spec/fixtures/encoded_differently.zip (in a test)
      dir_path = File.dirname(file_path).to_s
      FileUtils.mkdir_p dir_path unless File.exists?(dir_path)

      entry.extract(file_path) unless File.exists?(file_path)
    end
  end
  @unzipped = true
end

#unzip_pathObject



13
14
15
16
# File 'lib/zip_dir/unzipper.rb', line 13

def unzip_path
  unzip unless unzipped?
  @unzip_path
end

#unzipped?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/zip_dir/unzipper.rb', line 18

def unzipped?
  !!@unzipped
end