Class: Omnizip::Formats::Zip::Writer
- Inherits:
-
Object
- Object
- Omnizip::Formats::Zip::Writer
- Includes:
- Constants
- Defined in:
- lib/omnizip/formats/zip/writer.rb
Overview
ZIP archive writer
Constant Summary
Constants included from Constants
Constants::ATTR_ARCHIVE, Constants::ATTR_DIRECTORY, Constants::CENTRAL_DIRECTORY_SIGNATURE, Constants::COMPRESSION_BZIP2, Constants::COMPRESSION_DEFLATE, Constants::COMPRESSION_DEFLATE64, Constants::COMPRESSION_IMPLODED, Constants::COMPRESSION_LZMA, Constants::COMPRESSION_PPMD, Constants::COMPRESSION_REDUCED_1, Constants::COMPRESSION_REDUCED_2, Constants::COMPRESSION_REDUCED_3, Constants::COMPRESSION_REDUCED_4, Constants::COMPRESSION_SHRUNK, Constants::COMPRESSION_STORE, Constants::COMPRESSION_ZSTANDARD, Constants::DATA_DESCRIPTOR_SIGNATURE, Constants::END_OF_CENTRAL_DIRECTORY_SIGNATURE, Constants::FLAG_DATA_DESCRIPTOR, Constants::FLAG_ENCRYPTED, Constants::FLAG_STRONG_ENCRYPTION, Constants::FLAG_UTF8, Constants::LOCAL_FILE_HEADER_SIGNATURE, Constants::MAX_COMMENT_LENGTH, Constants::UNIX_DIR_PERMISSIONS, Constants::UNIX_EXTRA_FIELD_TAG, Constants::UNIX_FILE_PERMISSIONS, Constants::UNIX_SYMLINK_PERMISSIONS, Constants::VERSION_BZIP2, Constants::VERSION_DEFAULT, Constants::VERSION_DEFLATE, Constants::VERSION_LZMA, Constants::VERSION_MADE_BY_UNIX, Constants::VERSION_MADE_BY_WINDOWS, Constants::VERSION_ZIP64, Constants::ZIP64_END_OF_CENTRAL_DIRECTORY_LOCATOR_SIGNATURE, Constants::ZIP64_END_OF_CENTRAL_DIRECTORY_SIGNATURE, Constants::ZIP64_EXTRA_FIELD_TAG, Constants::ZIP64_LIMIT
Instance Attribute Summary collapse
-
#entries ⇒ Object
readonly
Returns the value of attribute entries.
-
#file_path ⇒ Object
readonly
Returns the value of attribute file_path.
Instance Method Summary collapse
-
#add_data(archive_path, data, stat = nil) ⇒ Object
Add data directly to the archive.
-
#add_directory(archive_path) ⇒ Object
Add a directory to the archive.
-
#add_file(file_path, archive_path = nil, preserve_links: true) ⇒ Object
Add a file to the archive.
-
#add_hardlink(archive_path, target_path) ⇒ Object
Add a hard link to the archive.
-
#add_precompressed_entry(filename:, uncompressed_size:, compressed_size:, crc32:, compressed_data:, stat: nil) ⇒ Object
Add an entry whose payload is already compressed.
-
#add_symlink(archive_path, target) ⇒ Object
Add a symbolic link to the archive.
-
#initialize(file_path) ⇒ Writer
constructor
A new instance of Writer.
-
#write(compression_method: COMPRESSION_DEFLATE, level: 6) ⇒ Object
Write the archive to disk.
-
#write_precompressed(compression_method: COMPRESSION_DEFLATE) ⇒ Object
Write the archive using pre-compressed entry payloads (set via
#add_precompressed_entry). - #write_precompressed_to_io(io, compression_method: COMPRESSION_DEFLATE) ⇒ Object
-
#write_to_io(io, compression_method: COMPRESSION_DEFLATE, level: 6) ⇒ Object
Write to an IO object.
Constructor Details
#initialize(file_path) ⇒ Writer
Returns a new instance of Writer.
14 15 16 17 18 |
# File 'lib/omnizip/formats/zip/writer.rb', line 14 def initialize(file_path) @file_path = file_path @entries = [] @local_headers = [] end |
Instance Attribute Details
#entries ⇒ Object (readonly)
Returns the value of attribute entries.
12 13 14 |
# File 'lib/omnizip/formats/zip/writer.rb', line 12 def entries @entries end |
#file_path ⇒ Object (readonly)
Returns the value of attribute file_path.
12 13 14 |
# File 'lib/omnizip/formats/zip/writer.rb', line 12 def file_path @file_path end |
Instance Method Details
#add_data(archive_path, data, stat = nil) ⇒ Object
Add data directly to the archive
96 97 98 99 100 101 102 103 104 |
# File 'lib/omnizip/formats/zip/writer.rb', line 96 def add_data(archive_path, data, stat = nil) entry = create_entry( filename: archive_path, uncompressed_data: data, stat: stat, ) @entries << entry end |
#add_directory(archive_path) ⇒ Object
Add a directory to the archive
83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/omnizip/formats/zip/writer.rb', line 83 def add_directory(archive_path) archive_path = "#{archive_path}/" unless archive_path.end_with?("/") entry = create_entry( filename: archive_path, uncompressed_data: "", directory: true, ) @entries << entry end |
#add_file(file_path, archive_path = nil, preserve_links: true) ⇒ Object
Add a file to the archive
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/omnizip/formats/zip/writer.rb', line 21 def add_file(file_path, archive_path = nil, preserve_links: true) archive_path ||= File.basename(file_path) if File.directory?(file_path) add_directory(archive_path) elsif preserve_links && LinkHandler.symlink?(file_path) add_symlink(archive_path, LinkHandler.read_link_target(file_path)) elsif preserve_links && LinkHandler.hardlink?(file_path) # For hard links, we add as regular file but track inode data = File.binread(file_path) add_data(archive_path, data, File.stat(file_path)) else data = File.binread(file_path) add_data(archive_path, data, File.stat(file_path)) end end |
#add_hardlink(archive_path, target_path) ⇒ Object
Add a hard link to the archive
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/omnizip/formats/zip/writer.rb', line 57 def add_hardlink(archive_path, target_path) unless LinkHandler.hardlink_supported? warn "Warning: Hard links not supported on #{RUBY_PLATFORM}, storing as regular file" # Store the target file content instead if File.exist?(target_path) data = File.binread(target_path) add_data(archive_path, data, File.stat(target_path)) else add_data(archive_path, "") end return end # For hard links, we store the first occurrence as a regular file # and subsequent ones reference the original entry = create_entry( filename: archive_path, uncompressed_data: "", hardlink: true, hardlink_target: target_path, ) @entries << entry end |
#add_precompressed_entry(filename:, uncompressed_size:, compressed_size:, crc32:, compressed_data:, stat: nil) ⇒ Object
Add an entry whose payload is already compressed. Bypasses
per-entry compression in #write, so #write_precompressed
must be used instead of #write to emit the archive.
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/omnizip/formats/zip/writer.rb', line 124 def add_precompressed_entry(filename:, uncompressed_size:, compressed_size:, crc32:, compressed_data:, stat: nil) entry = create_entry( filename: filename, uncompressed_data: "", stat: stat, ) entry[:compressed_size] = compressed_size entry[:uncompressed_size] = uncompressed_size entry[:crc32] = crc32 entry[:compressed_data] = compressed_data @entries << entry entry end |
#add_symlink(archive_path, target) ⇒ Object
Add a symbolic link to the archive
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/omnizip/formats/zip/writer.rb', line 39 def add_symlink(archive_path, target) unless LinkHandler.symlink_supported? warn "Warning: Symbolic links not supported on #{RUBY_PLATFORM}, storing as regular file" add_data(archive_path, target) return end entry = create_entry( filename: archive_path, uncompressed_data: target, symlink: true, symlink_target: target, ) @entries << entry end |
#write(compression_method: COMPRESSION_DEFLATE, level: 6) ⇒ Object
Write the archive to disk
107 108 109 110 111 112 |
# File 'lib/omnizip/formats/zip/writer.rb', line 107 def write(compression_method: COMPRESSION_DEFLATE, level: 6) File.open(file_path, "wb") do |io| write_to_io(io, compression_method: compression_method, level: level) end end |
#write_precompressed(compression_method: COMPRESSION_DEFLATE) ⇒ Object
Write the archive using pre-compressed entry payloads (set
via #add_precompressed_entry).
145 146 147 148 149 |
# File 'lib/omnizip/formats/zip/writer.rb', line 145 def write_precompressed(compression_method: COMPRESSION_DEFLATE) File.open(file_path, "wb") do |io| write_precompressed_to_io(io, compression_method: compression_method) end end |
#write_precompressed_to_io(io, compression_method: COMPRESSION_DEFLATE) ⇒ Object
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/omnizip/formats/zip/writer.rb', line 151 def write_precompressed_to_io(io, compression_method: COMPRESSION_DEFLATE) local_header_offsets = [] entries.each do |entry| offset = io.pos local_header_offsets << offset local_header = create_local_header(entry, compression_method) local_header.compressed_size = entry[:compressed_size] local_header.uncompressed_size = entry[:uncompressed_size] local_header.crc32 = entry[:crc32] io.write(local_header.to_binary) io.write(entry[:compressed_data]) unless entry[:directory] end central_directory_offset = io.pos entries.each_with_index do |entry, index| central_header = create_central_header( entry, compression_method, local_header_offsets[index], ) io.write(central_header.to_binary) end central_directory_size = io.pos - central_directory_offset eocd = create_eocd( total_entries: entries.size, central_directory_size: central_directory_size, central_directory_offset: central_directory_offset, ) io.write(eocd.to_binary) end |
#write_to_io(io, compression_method: COMPRESSION_DEFLATE, level: 6) ⇒ Object
Write to an IO object
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 |
# File 'lib/omnizip/formats/zip/writer.rb', line 188 def write_to_io(io, compression_method: COMPRESSION_DEFLATE, level: 6) local_header_offsets = [] # Write local file headers and data entries.each do |entry| offset = io.pos local_header_offsets << offset # Create local file header local_header = create_local_header(entry, compression_method) # Compress data if not a directory if entry[:directory] compressed_data = "" entry[:compressed_size] = 0 entry[:uncompressed_size] = 0 entry[:crc32] = 0 else compressed_data = compress_data( entry[:uncompressed_data], compression_method, level, ) entry[:compressed_size] = compressed_data.bytesize entry[:uncompressed_size] = entry[:uncompressed_data].bytesize entry[:crc32] = calculate_crc32(entry[:uncompressed_data]) end # Update local header with compressed sizes local_header.compressed_size = entry[:compressed_size] local_header.uncompressed_size = entry[:uncompressed_size] local_header.crc32 = entry[:crc32] # Write local header io.write(local_header.to_binary) # Write compressed data io.write(compressed_data) unless entry[:directory] end # Record start of central directory central_directory_offset = io.pos # Write central directory headers entries.each_with_index do |entry, index| central_header = create_central_header( entry, compression_method, local_header_offsets[index], ) io.write(central_header.to_binary) end # Calculate central directory size central_directory_size = io.pos - central_directory_offset # Write end of central directory record eocd = create_eocd( total_entries: entries.size, central_directory_size: central_directory_size, central_directory_offset: central_directory_offset, ) io.write(eocd.to_binary) end |