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



18
19
20
21
# File 'lib/omnizip/extraction/selective_extractor.rb', line 18

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

Instance Attribute Details

#archiveObject (readonly)

Returns the value of attribute archive.



12
13
14
# File 'lib/omnizip/extraction/selective_extractor.rb', line 12

def archive
  @archive
end

#filterObject (readonly)

Returns the value of attribute filter.



12
13
14
# File 'lib/omnizip/extraction/selective_extractor.rb', line 12

def filter
  @filter
end

Instance Method Details

#count_matchesInteger

Count matching entries

Returns:

  • (Integer)


82
83
84
# File 'lib/omnizip/extraction/selective_extractor.rb', line 82

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



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

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



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

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



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

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:



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

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