Class: Rubocop::Cop::Performance::Rubyzip

Inherits:
RuboCop::Cop::Base
  • Object
show all
Defined in:
lib/rubocop/cop/performance/rubyzip.rb

Overview

Flags inefficient uses of rubyzip’s Zip::File, since when instantiated it reads the file’s Central Directory into memory entirely. For zips with many files and directories, this can be very expensive even when the archive’s size in bytes is small.

See also:

Examples:

# bad
Zip::File.open('file_path.zip', &block)

# good
Zip::InputStream.open('file_path.zip')

Constant Summary collapse

MSG =
'Be careful when opening or iterating zip files via Zip::File. ' \
'Zip archives may contain many entries, and their file index is ' \
'read into memory upon construction, which can lead to ' \
'high memory use and poor performance. ' \
'Consider iterating archive entries via Zip::InputStream instead.'

Instance Method Summary collapse

Instance Method Details

#on_send(node) ⇒ Object



34
35
36
37
38
# File 'lib/rubocop/cop/performance/rubyzip.rb', line 34

def on_send(node)
  return unless reads_central_directory?(node)

  add_offense(node)
end

#reads_central_directory?(node) ⇒ Object



28
29
30
31
32
# File 'lib/rubocop/cop/performance/rubyzip.rb', line 28

def_node_matcher :reads_central_directory?, "  (send\n    (const\n      (const {nil? (cbase)} :Zip) :File) {:new :open :foreach} ...)\n"