Class: Inspec::ZipProvider

Inherits:
FileProvider show all
Defined in:
lib/inspec/file_provider.rb

Overview

class GemProvider

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from FileProvider

#binread, for_path, #relative_provider

Constructor Details

#initialize(path) ⇒ ZipProvider

Returns a new instance of ZipProvider.



151
152
153
154
155
156
157
158
159
160
161
# File 'lib/inspec/file_provider.rb', line 151

def initialize(path)
  @path = path
  @contents = {}
  @files = []
  walk_zip(@path) do |io|
    while (entry = io.get_next_entry)
      name = entry.name.sub(%r{/+$}, "")
      @files.push(name) unless name.empty? || name.squeeze("/") =~ %r{\.{2}(?:/|\z)}
    end
  end
end

Instance Attribute Details

#files ⇒ Object (readonly)

Returns the value of attribute files.



149
150
151
# File 'lib/inspec/file_provider.rb', line 149

def files
  @files
end

Instance Method Details

#extract(destination_path = ".") ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/inspec/file_provider.rb', line 163

def extract(destination_path = ".")
  FileUtils.mkdir_p(destination_path)

  Zip::File.open(@path) do |archive|
    archive.each do |file|
      final_path = File.join(destination_path, file.name)

      # This removes the top level directory (and any other files) to ensure
      # extracted files do not conflict.
      FileUtils.remove_entry(final_path) if File.exist?(final_path)

      # rubyzip 3.x no longer creates missing intermediate directories by
      # default, so do it ourselves.
      FileUtils.mkdir_p(File.dirname(final_path))

      # Passing an absolute entry_path alongside the default
      # destination_directory ('.') makes rubyzip 3.x join them into a
      # mangled, CWD-relative path. Pass destination_directory explicitly
      # instead, and let the entry's own (relative) name resolve against it.
      archive.extract(file, destination_directory: destination_path)
    end
  end
end

#read(file) ⇒ Object



187
188
189
190
# File 'lib/inspec/file_provider.rb', line 187

def read(file)
  # TODO: this is inefficient
  @contents[file] ||= read_from_zip(file)
end