class Zip::File
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 ::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.
Constants
- CREATE
- DATA_BUFFER_SIZE
- IO_METHODS
- MAX_SEGMENT_SIZE
- MIN_SEGMENT_SIZE
- SPLIT_SIGNATURE
- ZIP64_EOCD_SIGNATURE
Attributes
Returns the zip files comment, if it has one
default -> false
default -> false
default -> true
Public Class Methods
Same as open. But outputs data to a buffer instead of a file
# File lib/zip/file.rb, line 109 def add_buffer io = ::StringIO.new('') zf = ::Zip::File.new(io, true, true) yield zf zf.write_buffer(io) end
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).
# File lib/zip/file.rb, line 147 def foreach(aZipFileName, &block) open(aZipFileName) do |zipFile| zipFile.each(&block) end end
# File lib/zip/file.rb, line 164 def get_partial_zip_file_name(zip_file_name, partial_zip_file_name) partial_zip_file_name = zip_file_name.sub(/#{::File.basename(zip_file_name)}\z/, partial_zip_file_name + ::File.extname(zip_file_name)) unless partial_zip_file_name.nil? partial_zip_file_name ||= zip_file_name partial_zip_file_name end
# File lib/zip/file.rb, line 171 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
# File lib/zip/file.rb, line 153 def get_segment_size_for_split(segment_size) case when MIN_SEGMENT_SIZE > segment_size MIN_SEGMENT_SIZE when MAX_SEGMENT_SIZE < segment_size MAX_SEGMENT_SIZE else segment_size end end
Opens a zip archive. Pass true as the second parameter to create a new archive if it doesn't exist already.
# File lib/zip/file.rb, line 67 def initialize(file_name, create = nil, buffer = false, options = {}) super() @name = file_name @comment = '' @create = create case when !buffer && ::File.size?(file_name) @create = nil @file_permissions = ::File.stat(file_name).mode ::File.open(name, 'rb') do |f| read_from_stream(f) end when create @file_permissions = create_file_permissions @entry_set = EntrySet.new when ::File.zero?(file_name) raise Error, "File #{file_name} has zero size. Did you mean to pass the create flag?" else raise Error, "File #{file_name} not found" end @stored_entries = @entry_set.dup @stored_comment = @comment @restore_ownership = options[:restore_ownership] || false @restore_permissions = options[:restore_permissions] || true @restore_times = options[:restore_times] || true end
Same as new. If a block is passed the ZipFile object is passed to the block and is automatically closed afterwards just as with ruby's builtin ::open method.
# File lib/zip/file.rb, line 98 def open(file_name, create = nil) zf = ::Zip::File.new(file_name, create) return zf unless block_given? begin yield zf ensure zf.close end end
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.)
# File lib/zip/file.rb, line 120 def open_buffer(io, options = {}) unless IO_METHODS.map { |method| io.respond_to?(method) }.all? || io.is_a?(String) raise "Zip::File.open_buffer expects a String or IO-like argument (responds to #{IO_METHODS.join(', ')}). Found: #{io.class}" end if io.is_a?(::String) require 'stringio' io = ::StringIO.new(io) elsif io.respond_to?(:binmode) # https://github.com/rubyzip/rubyzip/issues/119 io.binmode end zf = ::Zip::File.new(io, true, true, options) zf.read_from_stream(io) yield zf begin zf.write_buffer(io) rescue IOError => e raise unless e.message == 'not opened for writing' end end
# File lib/zip/file.rb, line 175 def put_split_signature(szip_file, segment_size) signature_packed = [SPLIT_SIGNATURE].pack('V') szip_file << signature_packed segment_size - signature_packed.size end
TODO: Make the code more understandable
# File lib/zip/file.rb, line 184 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
Splits an archive into parts with segment size
# File lib/zip/file.rb, line 206 def split(zip_file_name, segment_size = MAX_SEGMENT_SIZE, delete_zip_file = true, 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) zip_file_size = ::File.size(zip_file_name) segment_size = get_segment_size_for_split(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 open(zip_file_name) {} partial_zip_file_name = get_partial_zip_file_name(zip_file_name, 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 ::File.delete(zip_file_name) if delete_zip_file szip_file_index end
Public Instance Methods
Convenience method for adding the contents of a file to the archive
# File lib/zip/file.rb, line 268 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
Closes the zip file committing any changes that has been made.
# File lib/zip/file.rb, line 333 def close commit end
Commits changes that has been made since the previous commit to the zip archive.
# File lib/zip/file.rb, line 308 def commit return unless 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
Returns true if any changes has been made to this archive since the previous commit
# File lib/zip/file.rb, line 339 def commit_required? @entry_set.each do |e| return true if e.dirty end @comment != @stored_comment || @entry_set != @stored_entries || @create == ::Zip::File::CREATE end
Extracts entry to file dest_path.
# File lib/zip/file.rb, line 300 def extract(entry, dest_path, &block) block ||= proc { ::Zip.on_exists_proc } found_entry = get_entry(entry) found_entry.extract(dest_path, &block) end
Searches for entry with the specified name. Returns nil if no entry is found. See also #get_entry
# File lib/zip/file.rb, line 348 def find_entry(entry_name) @entry_set.find_entry(entry_name) end
Searches for an entry just as #find_entry, but throws Errno::ENOENT if no entry is found.
# File lib/zip/file.rb, line 359 def get_entry(entry) selected_entry = find_entry(entry) raise Errno::ENOENT, entry unless selected_entry selected_entry.restore_ownership = @restore_ownership selected_entry.restore_permissions = @restore_permissions selected_entry.restore_times = @restore_times selected_entry end
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 ::open method.
# File lib/zip/file.rb, line 231 def get_input_stream(entry, &aProc) get_entry(entry).get_input_stream(&aProc) end
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 ::open method.
# File lib/zip/file.rb, line 240 def get_output_stream(entry, permission_int = nil, comment = nil, extra = nil, compressed_size = nil, crc = nil, compression_method = nil, size = nil, time = nil, &aProc) new_entry = if entry.kind_of?(Entry) entry else Entry.new(@name, entry.to_s, comment, extra, compressed_size, crc, compression_method, size, time) end if new_entry.directory? raise ArgumentError, "cannot open stream to directory entry - '#{new_entry}'" end new_entry.unix_perms = permission_int zip_streamable_entry = StreamableStream.new(new_entry) @entry_set << zip_streamable_entry zip_streamable_entry.get_output_stream(&aProc) end
Searches for entries given a glob
# File lib/zip/file.rb, line 353 def glob(*args, &block) @entry_set.glob(*args, &block) end
Creates a directory
# File lib/zip/file.rb, line 369 def mkdir(entryName, permissionInt = 0755) raise Errno::EEXIST, "File exists - #{entryName}" if find_entry(entryName) entryName = entryName.dup.to_s entryName << '/' unless entryName.end_with?('/') @entry_set << ::Zip::StreamableDirectory.new(@name, entryName, nil, permissionInt) end
Returns a string containing the contents of the specified entry
# File lib/zip/file.rb, line 263 def read(entry) get_input_stream(entry) { |is| is.read } end
Removes the specified entry.
# File lib/zip/file.rb, line 278 def remove(entry) @entry_set.delete(get_entry(entry)) end
Renames the specified entry.
# File lib/zip/file.rb, line 283 def rename(entry, new_name, &continue_on_exists_proc) foundEntry = get_entry(entry) check_entry_exists(new_name, continue_on_exists_proc, 'rename') @entry_set.delete(foundEntry) foundEntry.name = new_name @entry_set << foundEntry end
Replaces the specified entry with the contents of srcPath (from the file system).
# File lib/zip/file.rb, line 293 def replace(entry, srcPath) check_file(srcPath) remove(entry) add(entry, srcPath) end
Returns the name of the zip archive
# File lib/zip/file.rb, line 258 def to_s @name end
Write buffer write changes to buffer and return
# File lib/zip/file.rb, line 325 def write_buffer(io = ::StringIO.new('')) ::Zip::OutputStream.write_buffer(io) do |zos| @entry_set.each { |e| e.write_to_zip_output_stream(zos) } zos.comment = comment end end
Private Instance Methods
# File lib/zip/file.rb, line 390 def check_entry_exists(entryName, continue_on_exists_proc, procedureName) continue_on_exists_proc ||= proc { Zip.continue_on_exists_proc } return unless @entry_set.include?(entryName) if continue_on_exists_proc.call remove get_entry(entryName) else raise ::Zip::EntryExistsError, procedureName + " failed. Entry #{entryName} already exists" end end
# File lib/zip/file.rb, line 401 def check_file(path) raise Errno::ENOENT, path unless ::File.readable?(path) end
# File lib/zip/file.rb, line 424 def create_file_permissions ::Zip::RUNNING_ON_WINDOWS ? 0644 : 0666 - ::File.umask end
# File lib/zip/file.rb, line 415 def create_tmpname dirname, basename = ::File.split(name) ::Dir::Tmpname.create(basename, dirname) do |tmpname| opts = {perm: 0600, mode: ::File::CREAT | ::File::WRONLY | ::File::EXCL} f = File.open(tmpname, opts) f.close end end
# File lib/zip/file.rb, line 378 def directory?(newEntry, srcPath) srcPathIsDirectory = ::File.directory?(srcPath) if newEntry.directory? && !srcPathIsDirectory raise ArgumentError, "entry name '#{newEntry}' indicates directory entry, but " "'#{srcPath}' is not a directory" elsif !newEntry.directory? && srcPathIsDirectory newEntry.name += '/' end newEntry.directory? && srcPathIsDirectory end
# File lib/zip/file.rb, line 405 def on_success_replace tmp_filename = create_tmpname if yield tmp_filename ::File.rename(tmp_filename, name) ::File.chmod(@file_permissions, name) if defined?(@file_permissions) end ensure ::File.unlink(tmp_filename) if ::File.exist?(tmp_filename) end