Class: Zip::File

Inherits:
CentralDirectory show all
Includes:
FileSystem
Defined in:
lib/zip/file.rb,
lib/zip/filesystem.rb

Overview

ZipFile is modeled after java.util.zip.ZipFile from the Java SDK. The most important methods are those inherited from ZipCentralDirectory for accessing information about the entries in the archive and methods such as get_input_stream and get_output_stream for reading from and writing entries to the archive. The class includes a few convenience methods such as #extract for extracting entries to the filesystem, and #remove, #replace, #rename and #mkdir for making simple modifications to the archive.

Modifications to a zip archive are not committed until #commit or #close is called. The method #open accepts a block following the pattern from File.open offering a simple way to automatically close the archive when the block returns.

The following example opens zip archive my.zip (creating it if it doesn’t exist) and adds an entry first.txt and a directory entry a_dir to it.

require 'zip'

Zip::File.open("my.zip", Zip::File::CREATE) {
 |zipfile|
  zipfile.get_output_stream("first.txt") { |f| f.puts "Hello from ZipFile" }
  zipfile.mkdir("a_dir")
}

The next example reopens my.zip writes the contents of first.txt to standard out and deletes the entry from the archive.

require 'zip'

Zip::File.open("my.zip", Zip::File::CREATE) {
  |zipfile|
  puts zipfile.read("first.txt")
  zipfile.remove("first.txt")
}

ZipFileSystem offers an alternative API that emulates ruby’s interface for accessing the filesystem, ie. the File and Dir classes.

Constant Summary collapse

CREATE =
true
SPLIT_SIGNATURE =
0x08074b50
ZIP64_EOCD_SIGNATURE =
0x06064b50
MAX_SEGMENT_SIZE =
3_221_225_472
MIN_SEGMENT_SIZE =
65_536
DATA_BUFFER_SIZE =
8192
IO_METHODS =
[:tell, :seek, :read, :eof, :close]
DEFAULT_OPTIONS =
{
  restore_ownership:   false,
  restore_permissions: false,
  restore_times:       false
}.freeze

Constants inherited from CentralDirectory

CentralDirectory::END_OF_CDS, CentralDirectory::MAX_END_OF_CDS_SIZE, CentralDirectory::STATIC_EOCD_SIZE, CentralDirectory::ZIP64_END_OF_CDS, CentralDirectory::ZIP64_EOCD_LOCATOR

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from FileSystem

#dir, #file

Methods inherited from CentralDirectory

#==, #each, #entries, #get_64_e_o_c_d, #get_e_o_c_d, #read_64_e_o_c_d, #read_central_directory_entries, #read_e_o_c_d, #read_from_stream, read_from_stream, #size, #start_buf, #write_to_stream, #zip64_file?

Constructor Details

#initialize(path_or_io, dep_create = false, dep_buffer = false, create: false, buffer: false, **options) ⇒ File

Opens a zip archive. Pass true as the second parameter to create a new archive if it doesn’t exist already.



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/zip/file.rb', line 76

def initialize(path_or_io, dep_create = false, dep_buffer = false,
               create: false, buffer: false, **options)
  super()

  Zip.warn_about_v3_api('File#new') if dep_create || dep_buffer

  options  = DEFAULT_OPTIONS.merge(options)
  @name    = path_or_io.respond_to?(:path) ? path_or_io.path : path_or_io
  @comment = ''
  @create  = create || dep_create ? true : false # allow any truthy value to mean true
  buffer ||= dep_buffer

  if ::File.size?(@name.to_s)
    # There is a file, which exists, that is associated with this zip.
    @create = false
    @file_permissions = ::File.stat(@name).mode

    if buffer
      read_from_stream(path_or_io)
    else
      ::File.open(@name, 'rb') do |f|
        read_from_stream(f)
      end
    end
  elsif buffer && path_or_io.size > 0
    # This zip is probably a non-empty StringIO.
    @create = false
    read_from_stream(path_or_io)
  elsif @create
    # This zip is completely new/empty and is to be created.
    @entry_set = EntrySet.new
  elsif ::File.zero?(@name)
    # A file exists, but it is empty.
    raise Error, "File #{@name} has zero size. Did you mean to pass the create flag?"
  else
    # Everything is wrong.
    raise Error, "File #{@name} not found"
  end

  @stored_entries      = @entry_set.dup
  @stored_comment      = @comment
  @restore_ownership   = options[:restore_ownership]
  @restore_permissions = options[:restore_permissions]
  @restore_times       = options[:restore_times]
end

Instance Attribute Details

#commentObject

Returns the zip files comment, if it has one



72
73
74
# File 'lib/zip/file.rb', line 72

def comment
  @comment
end

#nameObject (readonly)

Returns the value of attribute name.



60
61
62
# File 'lib/zip/file.rb', line 60

def name
  @name
end

#restore_ownershipObject

default -> false.



63
64
65
# File 'lib/zip/file.rb', line 63

def restore_ownership
  @restore_ownership
end

#restore_permissionsObject

default -> false, but will be set to true in a future version.



66
67
68
# File 'lib/zip/file.rb', line 66

def restore_permissions
  @restore_permissions
end

#restore_timesObject

default -> false, but will be set to true in a future version.



69
70
71
# File 'lib/zip/file.rb', line 69

def restore_times
  @restore_times
end

Class Method Details

.add_buffer {|zf| ... } ⇒ Object

Same as #open. But outputs data to a buffer instead of a file

Yields:

  • (zf)


140
141
142
143
144
145
146
147
# File 'lib/zip/file.rb', line 140

def add_buffer
  Zip.warn_about_v3_api('Zip::File.add_buffer')

  io = ::StringIO.new
  zf = ::Zip::File.new(io, true, true)
  yield zf
  zf.write_buffer(io)
end

.foreach(zip_file_name, &block) ⇒ Object

Iterates over the contents of the ZipFile. This is more efficient than using a ZipInputStream since this methods simply iterates through the entries in the central directory structure in the archive whereas ZipInputStream jumps through the entire archive accessing the local entry headers (which contain the same information as the central directory).



181
182
183
184
185
# File 'lib/zip/file.rb', line 181

def foreach(zip_file_name, &block)
  ::Zip::File.open(zip_file_name) do |zip_file|
    zip_file.each(&block)
  end
end

.get_partial_zip_file_name(zip_file_name, partial_zip_file_name) ⇒ Object



197
198
199
200
201
202
203
204
# File 'lib/zip/file.rb', line 197

def get_partial_zip_file_name(zip_file_name, partial_zip_file_name)
  unless partial_zip_file_name.nil?
    partial_zip_file_name = zip_file_name.sub(/#{::File.basename(zip_file_name)}\z/,
                                              partial_zip_file_name + ::File.extname(zip_file_name))
  end
  partial_zip_file_name ||= zip_file_name
  partial_zip_file_name
end

.get_segment_count_for_split(zip_file_size, segment_size) ⇒ Object



206
207
208
# File 'lib/zip/file.rb', line 206

def get_segment_count_for_split(zip_file_size, segment_size)
  (zip_file_size / segment_size).to_i + (zip_file_size % segment_size == 0 ? 0 : 1)
end

.get_segment_size_for_split(segment_size) ⇒ Object



187
188
189
190
191
192
193
194
195
# File 'lib/zip/file.rb', line 187

def get_segment_size_for_split(segment_size)
  if MIN_SEGMENT_SIZE > segment_size
    MIN_SEGMENT_SIZE
  elsif MAX_SEGMENT_SIZE < segment_size
    MAX_SEGMENT_SIZE
  else
    segment_size
  end
end

.open(file_name, dep_create = false, create: false, **options) ⇒ Object

Similar to ::new. If a block is passed the Zip::File object is passed to the block and is automatically closed afterwards, just as with ruby’s builtin File::open method.



126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/zip/file.rb', line 126

def open(file_name, dep_create = false, create: false, **options)
  Zip.warn_about_v3_api('Zip::File.open') if dep_create

  zf = ::Zip::File.new(file_name, create: (dep_create || create), buffer: false, **options)
  return zf unless block_given?

  begin
    yield zf
  ensure
    zf.close
  end
end

.open_buffer(io, **options) {|zf| ... } ⇒ Object

Like #open, but reads zip archive contents from a String or open IO stream, and outputs data to a buffer. (This can be used to extract data from a downloaded zip archive without first saving it to disk.)

Yields:

  • (zf)


153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/zip/file.rb', line 153

def open_buffer(io, **options)
  unless IO_METHODS.map { |method| io.respond_to?(method) }.all? || io.kind_of?(String)
    raise "Zip::File.open_buffer expects a String or IO-like argument (responds to #{IO_METHODS.join(', ')}). Found: #{io.class}"
  end

  io = ::StringIO.new(io) if io.kind_of?(::String)

  # https://github.com/rubyzip/rubyzip/issues/119
  io.binmode if io.respond_to?(:binmode)

  zf = ::Zip::File.new(io, create: true, buffer: true, **options)
  return zf unless block_given?

  yield zf

  begin
    zf.write_buffer(io)
  rescue IOError => e
    raise unless e.message == 'not opened for writing'
  end
end

.put_split_signature(szip_file, segment_size) ⇒ Object



210
211
212
213
214
# File 'lib/zip/file.rb', line 210

def put_split_signature(szip_file, segment_size)
  signature_packed = [SPLIT_SIGNATURE].pack('V')
  szip_file << signature_packed
  segment_size - signature_packed.size
end

.save_splited_part(zip_file, partial_zip_file_name, zip_file_size, szip_file_index, segment_size, segment_count) ⇒ Object

TODO: Make the code more understandable



219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/zip/file.rb', line 219

def save_splited_part(zip_file, partial_zip_file_name, zip_file_size, szip_file_index, segment_size, segment_count)
  ssegment_size  = zip_file_size - zip_file.pos
  ssegment_size  = segment_size if ssegment_size > segment_size
  szip_file_name = "#{partial_zip_file_name}.#{format('%03d', szip_file_index)}"
  ::File.open(szip_file_name, 'wb') do |szip_file|
    if szip_file_index == 1
      ssegment_size = put_split_signature(szip_file, segment_size)
    end
    chunk_bytes = 0
    until ssegment_size == chunk_bytes || zip_file.eof?
      segment_bytes_left = ssegment_size - chunk_bytes
      buffer_size        = segment_bytes_left < DATA_BUFFER_SIZE ? segment_bytes_left : DATA_BUFFER_SIZE
      chunk              = zip_file.read(buffer_size)
      chunk_bytes += buffer_size
      szip_file << chunk
      # Info for track splitting
      yield segment_count, szip_file_index, chunk_bytes, ssegment_size if block_given?
    end
  end
end

.split(zip_file_name, dep_segment_size = MAX_SEGMENT_SIZE, dep_delete_zip_file = true, dep_partial_zip_file_name = nil, segment_size: MAX_SEGMENT_SIZE, delete_zip_file: nil, partial_zip_file_name: nil) ⇒ Object

Splits an archive into parts with segment size

Raises:



241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/zip/file.rb', line 241

def split(zip_file_name,
          dep_segment_size = MAX_SEGMENT_SIZE, dep_delete_zip_file = true, dep_partial_zip_file_name = nil,
          segment_size: MAX_SEGMENT_SIZE, delete_zip_file: nil, partial_zip_file_name: nil)
  raise Error, "File #{zip_file_name} not found" unless ::File.exist?(zip_file_name)
  raise Errno::ENOENT, zip_file_name unless ::File.readable?(zip_file_name)

  if dep_segment_size != MAX_SEGMENT_SIZE || !dep_delete_zip_file || dep_partial_zip_file_name
    Zip.warn_about_v3_api('Zip::File.split')
  end

  zip_file_size = ::File.size(zip_file_name)
  segment_size  = get_segment_size_for_split(segment_size || dep_segment_size)
  return if zip_file_size <= segment_size

  segment_count = get_segment_count_for_split(zip_file_size, segment_size)
  # Checking for correct zip structure
  ::Zip::File.open(zip_file_name) {}
  partial_zip_file_name = get_partial_zip_file_name(zip_file_name, (partial_zip_file_name || dep_partial_zip_file_name))
  szip_file_index       = 0
  ::File.open(zip_file_name, 'rb') do |zip_file|
    until zip_file.eof?
      szip_file_index += 1
      save_splited_part(zip_file, partial_zip_file_name, zip_file_size, szip_file_index, segment_size, segment_count)
    end
  end
  delete_zip_file = delete_zip_file.nil? ? dep_delete_zip_file : delete_zip_file
  ::File.delete(zip_file_name) if delete_zip_file
  szip_file_index
end

Instance Method Details

#add(entry, src_path, &continue_on_exists_proc) ⇒ Object

Convenience method for adding the contents of a file to the archive



335
336
337
338
339
340
341
342
# File 'lib/zip/file.rb', line 335

def add(entry, src_path, &continue_on_exists_proc)
  continue_on_exists_proc ||= proc { ::Zip.continue_on_exists_proc }
  check_entry_exists(entry, continue_on_exists_proc, 'add')
  new_entry = entry.kind_of?(::Zip::Entry) ? entry : ::Zip::Entry.new(@name, entry.to_s)
  new_entry.gather_fileinfo_from_srcpath(src_path)
  new_entry.dirty = true
  @entry_set << new_entry
end

#add_stored(entry, src_path, &continue_on_exists_proc) ⇒ Object

Convenience method for adding the contents of a file to the archive in Stored format (uncompressed)



346
347
348
349
# File 'lib/zip/file.rb', line 346

def add_stored(entry, src_path, &continue_on_exists_proc)
  entry = ::Zip::Entry.new(@name, entry.to_s, nil, nil, nil, nil, ::Zip::Entry::STORED)
  add(entry, src_path, &continue_on_exists_proc)
end

#closeObject

Closes the zip file committing any changes that has been made.



424
425
426
# File 'lib/zip/file.rb', line 424

def close
  commit
end

#commitObject

Commits changes that has been made since the previous commit to the zip archive.



396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
# File 'lib/zip/file.rb', line 396

def commit
  return if name.kind_of?(StringIO) || !commit_required?

  on_success_replace do |tmp_file|
    ::Zip::OutputStream.open(tmp_file) do |zos|
      @entry_set.each do |e|
        e.write_to_zip_output_stream(zos)
        e.dirty = false
        e.clean_up
      end
      zos.comment = comment
    end
    true
  end
  initialize(name)
end

#commit_required?Boolean

Returns true if any changes has been made to this archive since the previous commit

Returns:

  • (Boolean)


430
431
432
433
434
435
# File 'lib/zip/file.rb', line 430

def commit_required?
  @entry_set.each do |e|
    return true if e.dirty
  end
  @comment != @stored_comment || @entry_set != @stored_entries || @create
end

#extract(entry, dest_path, &block) ⇒ Object

Extracts entry to file dest_path.



374
375
376
377
378
379
380
# File 'lib/zip/file.rb', line 374

def extract(entry, dest_path, &block)
  Zip.warn_about_v3_api('Zip::File#extract')

  block ||= proc { ::Zip.on_exists_proc }
  found_entry = get_entry(entry)
  found_entry.extract(dest_path, &block)
end

#extract_v3(entry, entry_path = nil, destination_directory: '.', &block) ⇒ Object

Extracts ‘entry` to a file at `entry_path`, with `destination_directory` as the base location in the filesystem.

NB: The caller is responsible for making sure ‘destination_directory` is safe, if it is passed.



387
388
389
390
391
392
# File 'lib/zip/file.rb', line 387

def extract_v3(entry, entry_path = nil, destination_directory: '.', &block)
  block ||= proc { ::Zip.on_exists_proc }
  found_entry = get_entry(entry)
  entry_path ||= found_entry.name
  found_entry.extract_v3(entry_path, destination_directory: destination_directory, &block)
end

#find_entry(entry_name) ⇒ Object

Searches for entry with the specified name. Returns nil if no entry is found. See also get_entry



439
440
441
442
443
444
445
446
447
# File 'lib/zip/file.rb', line 439

def find_entry(entry_name)
  selected_entry = @entry_set.find_entry(entry_name)
  return if selected_entry.nil?

  selected_entry.restore_ownership   = @restore_ownership
  selected_entry.restore_permissions = @restore_permissions
  selected_entry.restore_times       = @restore_times
  selected_entry
end

#get_entry(entry) ⇒ Object

Searches for an entry just as find_entry, but throws Errno::ENOENT if no entry is found.

Raises:

  • (Errno::ENOENT)


456
457
458
459
460
461
# File 'lib/zip/file.rb', line 456

def get_entry(entry)
  selected_entry = find_entry(entry)
  raise Errno::ENOENT, entry if selected_entry.nil?

  selected_entry
end

#get_input_stream(entry, &a_proc) ⇒ Object

Returns an input stream to the specified entry. If a block is passed the stream object is passed to the block and the stream is automatically closed afterwards just as with ruby’s builtin File.open method.



275
276
277
# File 'lib/zip/file.rb', line 275

def get_input_stream(entry, &a_proc)
  get_entry(entry).get_input_stream(&a_proc)
end

#get_output_stream(entry, dep_permission_int = nil, dep_comment = nil, dep_extra = nil, dep_compressed_size = nil, dep_crc = nil, dep_compression_method = nil, dep_size = nil, dep_time = nil, permission_int: nil, comment: nil, extra: nil, compressed_size: nil, crc: nil, compression_method: nil, size: nil, time: nil, &a_proc) ⇒ Object

Returns an output stream to the specified entry. If entry is not an instance of Zip::Entry, a new Zip::Entry will be initialized using the arguments specified. If a block is passed the stream object is passed to the block and the stream is automatically closed afterwards just as with ruby’s builtin File.open method. rubocop:disable Metrics/ParameterLists, Metrics/PerceivedComplexity, Metrics/CyclomaticComplexity



285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
# File 'lib/zip/file.rb', line 285

def get_output_stream(entry,
                      dep_permission_int = nil, dep_comment = nil,
                      dep_extra = nil, dep_compressed_size = nil, dep_crc = nil,
                      dep_compression_method = nil, dep_size = nil, dep_time = nil,
                      permission_int: nil, comment: nil,
                      extra: nil, compressed_size: nil, crc: nil,
                      compression_method: nil, size: nil, time: nil,
                      &a_proc)

  unless dep_permission_int.nil? && dep_comment.nil? && dep_extra.nil? &&
         dep_compressed_size.nil? && dep_crc.nil? && dep_compression_method.nil? &&
         dep_size.nil? && dep_time.nil?
    Zip.warn_about_v3_api('Zip::File#get_output_stream')
  end

  new_entry =
    if entry.kind_of?(Entry)
      entry
    else
      Entry.new(@name, entry.to_s,
                comment:            (comment || dep_comment),
                extra:              (extra || dep_extra),
                compressed_size:    (compressed_size || dep_compressed_size),
                crc:                (crc || dep_crc),
                compression_method: (compression_method || dep_compression_method),
                size:               (size || dep_size),
                time:               (time || dep_time))
    end
  if new_entry.directory?
    raise ArgumentError,
          "cannot open stream to directory entry - '#{new_entry}'"
  end
  new_entry.unix_perms = (permission_int || dep_permission_int)
  zip_streamable_entry = StreamableStream.new(new_entry)
  @entry_set << zip_streamable_entry
  zip_streamable_entry.get_output_stream(&a_proc)
end

#glob(*args, &block) ⇒ Object

Searches for entries given a glob



450
451
452
# File 'lib/zip/file.rb', line 450

def glob(*args, &block)
  @entry_set.glob(*args, &block)
end

#mkdir(entry_name, permission = 0o755) ⇒ Object

Creates a directory

Raises:

  • (Errno::EEXIST)


464
465
466
467
468
469
470
# File 'lib/zip/file.rb', line 464

def mkdir(entry_name, permission = 0o755)
  raise Errno::EEXIST, "File exists - #{entry_name}" if find_entry(entry_name)

  entry_name = entry_name.dup.to_s
  entry_name << '/' unless entry_name.end_with?('/')
  @entry_set << ::Zip::StreamableDirectory.new(@name, entry_name, nil, permission)
end

#read(entry) ⇒ Object

Returns a string containing the contents of the specified entry



330
331
332
# File 'lib/zip/file.rb', line 330

def read(entry)
  get_input_stream(entry, &:read)
end

#remove(entry) ⇒ Object

Removes the specified entry.



352
353
354
# File 'lib/zip/file.rb', line 352

def remove(entry)
  @entry_set.delete(get_entry(entry))
end

#rename(entry, new_name, &continue_on_exists_proc) ⇒ Object

Renames the specified entry.



357
358
359
360
361
362
363
# File 'lib/zip/file.rb', line 357

def rename(entry, new_name, &continue_on_exists_proc)
  found_entry = get_entry(entry)
  check_entry_exists(new_name, continue_on_exists_proc, 'rename')
  @entry_set.delete(found_entry)
  found_entry.name = new_name
  @entry_set << found_entry
end

#replace(entry, src_path) ⇒ Object

Replaces the specified entry with the contents of src_path (from the file system).



367
368
369
370
371
# File 'lib/zip/file.rb', line 367

def replace(entry, src_path)
  check_file(src_path)
  remove(entry)
  add(entry, src_path)
end

#to_sObject

Returns the name of the zip archive



325
326
327
# File 'lib/zip/file.rb', line 325

def to_s
  @name
end

#write_buffer(io = ::StringIO.new) ⇒ Object

Write buffer write changes to buffer and return



414
415
416
417
418
419
420
421
# File 'lib/zip/file.rb', line 414

def write_buffer(io = ::StringIO.new)
  return io unless commit_required?

  ::Zip::OutputStream.write_buffer(io) do |zos|
    @entry_set.each { |e| e.write_to_zip_output_stream(zos) }
    zos.comment = comment
  end
end