Class: Omnizip::ArchiveHandlers::SevenZipHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/omnizip/archive_handlers/seven_zip_handler.rb

Overview

Adapter that exposes the canonical +create+/+extract_to+/+list+ interface for the 7z format. Wraps Omnizip::Formats::SevenZip.

Defined Under Namespace

Classes: FileProxy

Instance Method Summary collapse

Instance Method Details

#create(path, &block) ⇒ Object

Creates a .7z archive. The yielded proxy only supports add(name, source_path) for files — directory entries are not part of the single-file convenience flow.



11
12
13
14
15
# File 'lib/omnizip/archive_handlers/seven_zip_handler.rb', line 11

def create(path, &block)
  Omnizip::Formats::SevenZip.create(path) do |writer|
    block&.call(FileProxy.new(writer))
  end
end

#extract_to(path, output_dir, **options) ⇒ Object



17
18
19
20
21
# File 'lib/omnizip/archive_handlers/seven_zip_handler.rb', line 17

def extract_to(path, output_dir, **options)
  Omnizip::Formats::SevenZip.open(path, options) do |reader|
    reader.extract_all(output_dir)
  end
end

#list(path, details: false, **options) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/omnizip/archive_handlers/seven_zip_handler.rb', line 23

def list(path, details: false, **options)
  with_reader(path, options) do |reader|
    entries = reader.list_files
    if details
      entries.map do |e|
        { name: e.name, size: e.size, directory: e.is_dir,
          mtime: e.mtime }
      end
    else
      entries.map(&:name)
    end
  end
end

#read_entry(path, entry_name, **options) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/omnizip/archive_handlers/seven_zip_handler.rb', line 37

def read_entry(path, entry_name, **options)
  with_reader(path, options) do |reader|
    entry = reader.list_files.find { |e| e.name == entry_name }
    raise Errno::ENOENT, "Entry not found: #{entry_name}" unless entry

    reader.extract_entry_data(File.open(path, "rb"), entry)
  end
end