Class: Omnizip::Extraction::SelectiveExtractor

Inherits:
Object
  • Object
show all
Defined in:
lib/omnizip/extraction/selective_extractor.rb

Overview

Coordinates selective extraction from archives

Extracts only files matching specified patterns, efficiently skipping non-matched files without decompression.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(archive, filter = nil) ⇒ SelectiveExtractor

Initialize a new selective extractor

Parameters:

  • archive (Object)

    Archive to extract from

  • filter (FilterChain, PatternMatcher, Object) (defaults to: nil)

    Filter to apply



16
17
18
19
# File 'lib/omnizip/extraction/selective_extractor.rb', line 16

def initialize(archive, filter = nil)
  @archive = archive
  @filter = normalize_filter(filter)
end

Instance Attribute Details

#archiveObject (readonly)

Returns the value of attribute archive.



10
11
12
# File 'lib/omnizip/extraction/selective_extractor.rb', line 10

def archive
  @archive
end

#filterObject (readonly)

Returns the value of attribute filter.



10
11
12
# File 'lib/omnizip/extraction/selective_extractor.rb', line 10

def filter
  @filter
end

Instance Method Details

#count_matchesInteger

Count matching entries

Returns:

  • (Integer)


80
81
82
# File 'lib/omnizip/extraction/selective_extractor.rb', line 80

def count_matches
  list_matches.size
end

#extract(dest, options = {}) ⇒ Array<String>

Extract matching files to destination

Parameters:

  • dest (String)

    Destination directory

  • options (Hash) (defaults to: {})

    Extraction options

Options Hash (options):

  • :preserve_paths (Boolean)

    Keep directory structure

  • :flatten (Boolean)

    Extract all to root

  • :overwrite (Boolean)

    Overwrite existing files

  • :progress (Progress::ProgressTracker)

    Progress tracker

Returns:

  • (Array<String>)

    Paths of extracted files



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/omnizip/extraction/selective_extractor.rb', line 30

def extract(dest, options = {})
  FileUtils.mkdir_p(dest)
  extracted = []

  entries_to_extract = list_matches
  total = entries_to_extract.size
  current = 0

  entries_to_extract.each do |entry|
    dest_path = build_dest_path(entry, dest, options)
    extract_entry(entry, dest_path, options)
    extracted << dest_path

    # Update progress if tracker provided
    current += 1
    update_progress(options[:progress], current, total, entry)
  end

  extracted
end

#extract_to_memoryHash<String, String>

Extract matching files to memory

Returns:

  • (Hash<String, String>)

    Hash of filename => content



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/omnizip/extraction/selective_extractor.rb', line 54

def extract_to_memory
  result = {}

  list_matches.each do |entry|
    filename = entry_filename(entry)
    content = read_entry_content(entry)
    result[filename] = content
  end

  result
end

#list_matchesArray

List matching entries without extracting

Returns:

  • (Array)

    Matching entries



69
70
71
72
73
74
75
# File 'lib/omnizip/extraction/selective_extractor.rb', line 69

def list_matches
  return list_all if @filter.nil?

  list_all.select do |entry|
    filter_matches?(entry)
  end
end

#match_resultModels::MatchResult

Get match result with statistics

Returns:



87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/omnizip/extraction/selective_extractor.rb', line 87

def match_result
  all_entries = list_all
  matches = if @filter
              all_entries.select { |entry| filter_matches?(entry) }
            else
              all_entries
            end

  Models::MatchResult.new(
    @filter&.to_s || "all",
    matches: matches,
    total_scanned: all_entries.size,
  )
end