Files
fizzy/app/models/zip_file/reader.rb
T
Stanko K.R. 992f15066b Replace RubyZip with ZipKit
ZipKit can read and write files directly from S3 which makes both imports and exports way more efficient in terms of disk usage.
2026-02-02 12:36:48 +01:00

30 lines
738 B
Ruby

class ZipFile::Reader
def initialize(io)
@io = io
@reader = ZipKit::FileReader.read_zip_structure(io: io)
end
def read(file_path)
entry = @reader.find { |e| e.filename == file_path }
raise ArgumentError, "File not found in zip: #{file_path}" unless entry
raise ArgumentError, "Cannot read directory entry: #{file_path}" if entry.filename.end_with?("/")
extractor = entry.extractor_from(@io)
content = extractor.extract
if block_given?
yield StringIO.new(content)
else
content
end
end
def glob(pattern)
@reader.map(&:filename).select { |name| File.fnmatch(pattern, name) }.sort
end
def exists?(file_path)
@reader.any? { |e| e.filename == file_path }
end
end