Module: Omnizip::Convenience
- Included in:
- Omnizip
- Defined in:
- lib/omnizip/convenience.rb
Overview
Convenience methods for common archive operations.
All public methods accept an optional format: keyword (default
:zip) that selects the underlying archive handler via
Omnizip::ArchiveHandler. New formats gain access to the convenience
API by registering a handler; no edits to this file are needed.
Constant Summary collapse
- DEFAULT_FORMAT =
:zip
Instance Method Summary collapse
-
#add_to_archive(archive_path, entry_name, source_path, format: DEFAULT_FORMAT) ⇒ String
Add a file to an existing archive.
-
#compress_directory(input_dir, output_path, format: DEFAULT_FORMAT, recursive: true, **options) ⇒ String
Compress a directory into an archive.
-
#compress_file(input_path, output_path, format: DEFAULT_FORMAT, **options) ⇒ String
Compress a single file into an archive.
-
#create_rar(archive_path, **options, &block) ⇒ Object
Create a RAR archive (requires RAR license — see NotLicensedError).
-
#extract_archive(archive_path, output_dir, format: DEFAULT_FORMAT, overwrite: false, **options) ⇒ Array<String>
Extract an archive to a directory.
-
#list_archive(archive_path, format: DEFAULT_FORMAT, details: false, **options) ⇒ Array<String>, Array<Hash>
List contents of an archive.
-
#read_from_archive(archive_path, entry_name, format: DEFAULT_FORMAT) ⇒ String
Read a single entry from an archive.
-
#remove_from_archive(archive_path, entry_name, format: DEFAULT_FORMAT) ⇒ String
Remove an entry from an existing archive.
Instance Method Details
#add_to_archive(archive_path, entry_name, source_path, format: DEFAULT_FORMAT) ⇒ String
Add a file to an existing archive.
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/omnizip/convenience.rb', line 118 def add_to_archive(archive_path, entry_name, source_path, format: DEFAULT_FORMAT) require_archive!(archive_path) unless ::File.exist?(source_path) raise Errno::ENOENT, "Source file not found: #{source_path}" end handler = Omnizip::ArchiveHandler.for(format) if handler.respond_to?(:add_entry) handler.add_entry(archive_path, entry_name, source_path) else raise Omnizip::UnsupportedFormatError, "Format #{format.inspect} does not support adding entries" end archive_path end |
#compress_directory(input_dir, output_path, format: DEFAULT_FORMAT, recursive: true, **options) ⇒ String
Compress a directory into an archive.
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/omnizip/convenience.rb', line 49 def compress_directory(input_dir, output_path, format: DEFAULT_FORMAT, recursive: true, **) unless ::File.exist?(input_dir) raise Errno::ENOENT, "Input directory not found: #{input_dir}" end unless ::File.directory?(input_dir) raise ArgumentError, "Input is not a directory: #{input_dir}" end if [:profile] first_file = find_first_file(input_dir) apply_profile(first_file, ) end Omnizip::ArchiveHandler.for(format).create(output_path) do |archive| add_directory_contents(archive, input_dir, "", recursive: recursive) end output_path end |
#compress_file(input_path, output_path, format: DEFAULT_FORMAT, **options) ⇒ String
Compress a single file into an archive.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/omnizip/convenience.rb', line 20 def compress_file(input_path, output_path, format: DEFAULT_FORMAT, **) unless ::File.exist?(input_path) raise Errno::ENOENT, "Input file not found: #{input_path}" end if ::File.directory?(input_path) raise ArgumentError, "Input is a directory: #{input_path}" end = apply_profile(input_path, ) if [:profile] if [:chunked] return Omnizip::Chunked.compress_file(input_path, output_path, **) end Omnizip::ArchiveHandler.for(format).create(output_path) do |archive| archive.add(::File.basename(input_path), input_path) end output_path end |
#create_rar(archive_path, **options, &block) ⇒ Object
Create a RAR archive (requires RAR license — see NotLicensedError). rubocop:disable Naming/BlockForwarding, Style/ArgumentsForwarding -- Ruby 3.0 compatibility
156 157 158 159 |
# File 'lib/omnizip/convenience.rb', line 156 def create_rar(archive_path, **, &block) [:version] ||= 5 Omnizip::Formats::Rar.create(archive_path, , &block) end |
#extract_archive(archive_path, output_dir, format: DEFAULT_FORMAT, overwrite: false, **options) ⇒ Array<String>
Extract an archive to a directory.
78 79 80 81 82 83 84 |
# File 'lib/omnizip/convenience.rb', line 78 def extract_archive(archive_path, output_dir, format: DEFAULT_FORMAT, overwrite: false, **) require_archive!(archive_path) Omnizip::ArchiveHandler.for(format) .extract_to(archive_path, output_dir, overwrite: overwrite, **) end |
#list_archive(archive_path, format: DEFAULT_FORMAT, details: false, **options) ⇒ Array<String>, Array<Hash>
List contents of an archive.
93 94 95 96 97 98 |
# File 'lib/omnizip/convenience.rb', line 93 def list_archive(archive_path, format: DEFAULT_FORMAT, details: false, **) require_archive!(archive_path) Omnizip::ArchiveHandler.for(format) .list(archive_path, details: details, **) end |
#read_from_archive(archive_path, entry_name, format: DEFAULT_FORMAT) ⇒ String
Read a single entry from an archive.
106 107 108 109 |
# File 'lib/omnizip/convenience.rb', line 106 def read_from_archive(archive_path, entry_name, format: DEFAULT_FORMAT) require_archive!(archive_path) Omnizip::ArchiveHandler.for(format).read_entry(archive_path, entry_name) end |
#remove_from_archive(archive_path, entry_name, format: DEFAULT_FORMAT) ⇒ String
Remove an entry from an existing archive.
142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/omnizip/convenience.rb', line 142 def remove_from_archive(archive_path, entry_name, format: DEFAULT_FORMAT) require_archive!(archive_path) handler = Omnizip::ArchiveHandler.for(format) unless handler.respond_to?(:remove_entry) raise Omnizip::UnsupportedFormatError, "Format #{format.inspect} does not support removing entries" end handler.remove_entry(archive_path, entry_name) archive_path end |