2010-11-30 16:27:59 +08:00
|
|
|
module Zip
|
|
|
|
# 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
|
2011-01-07 21:23:22 +08:00
|
|
|
# #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.
|
2010-11-30 16:27:59 +08:00
|
|
|
#
|
2011-01-07 21:23:22 +08:00
|
|
|
# The following example opens zip archive <code>my.zip</code>
|
|
|
|
# (creating it if it doesn't exist) and adds an entry
|
|
|
|
# <code>first.txt</code> and a directory entry <code>a_dir</code>
|
2010-11-30 16:27:59 +08:00
|
|
|
# to it.
|
|
|
|
#
|
2013-08-27 04:26:14 +08:00
|
|
|
# require 'zip'
|
2011-01-07 21:23:22 +08:00
|
|
|
#
|
2013-08-27 04:26:14 +08:00
|
|
|
# Zip::File.open("my.zip", Zip::File::CREATE) {
|
2010-11-30 16:27:59 +08:00
|
|
|
# |zipfile|
|
|
|
|
# zipfile.get_output_stream("first.txt") { |f| f.puts "Hello from ZipFile" }
|
|
|
|
# zipfile.mkdir("a_dir")
|
|
|
|
# }
|
|
|
|
#
|
|
|
|
# The next example reopens <code>my.zip</code> writes the contents of
|
2011-01-07 21:23:22 +08:00
|
|
|
# <code>first.txt</code> to standard out and deletes the entry from
|
2010-11-30 16:27:59 +08:00
|
|
|
# the archive.
|
|
|
|
#
|
2013-08-27 04:26:14 +08:00
|
|
|
# require 'zip'
|
2011-01-07 21:23:22 +08:00
|
|
|
#
|
2013-08-27 04:26:14 +08:00
|
|
|
# Zip::File.open("my.zip", Zip::File::CREATE) {
|
2010-11-30 16:27:59 +08:00
|
|
|
# |zipfile|
|
|
|
|
# puts zipfile.read("first.txt")
|
|
|
|
# zipfile.remove("first.txt")
|
|
|
|
# }
|
|
|
|
#
|
2011-01-07 21:23:22 +08:00
|
|
|
# ZipFileSystem offers an alternative API that emulates ruby's
|
2010-11-30 16:27:59 +08:00
|
|
|
# interface for accessing the filesystem, ie. the File and Dir classes.
|
2011-01-07 21:23:22 +08:00
|
|
|
|
2013-06-03 15:56:24 +08:00
|
|
|
class File < CentralDirectory
|
2010-11-30 16:27:59 +08:00
|
|
|
|
2013-10-20 03:04:54 +08:00
|
|
|
CREATE = 1
|
|
|
|
SPLIT_SIGNATURE = 0x08074b50
|
2013-08-27 04:26:14 +08:00
|
|
|
ZIP64_EOCD_SIGNATURE = 0x06064b50
|
2013-10-20 03:04:54 +08:00
|
|
|
MAX_SEGMENT_SIZE = 3221225472
|
|
|
|
MIN_SEGMENT_SIZE = 65536
|
|
|
|
DATA_BUFFER_SIZE = 8192
|
2010-11-30 16:27:59 +08:00
|
|
|
|
|
|
|
attr_reader :name
|
|
|
|
|
|
|
|
# default -> false
|
|
|
|
attr_accessor :restore_ownership
|
|
|
|
# default -> false
|
|
|
|
attr_accessor :restore_permissions
|
|
|
|
# default -> true
|
|
|
|
attr_accessor :restore_times
|
2013-08-27 04:26:14 +08:00
|
|
|
# Returns the zip files comment, if it has one
|
|
|
|
attr_accessor :comment
|
2010-11-30 16:27:59 +08:00
|
|
|
|
|
|
|
# Opens a zip archive. Pass true as the second parameter to create
|
|
|
|
# a new archive if it doesn't exist already.
|
2013-10-27 22:30:48 +08:00
|
|
|
def initialize(file_name, create = nil, buffer = false, options = {})
|
2010-11-30 16:27:59 +08:00
|
|
|
super()
|
2013-10-20 03:04:54 +08:00
|
|
|
@name = file_name
|
2013-08-27 04:26:14 +08:00
|
|
|
@comment = ''
|
2013-10-20 03:04:54 +08:00
|
|
|
@create = create
|
2012-02-14 06:03:34 +08:00
|
|
|
case
|
2014-02-07 07:00:38 +08:00
|
|
|
when !buffer && ::File.exist?(file_name)
|
2013-08-01 08:25:43 +08:00
|
|
|
@create = nil
|
2013-10-20 03:04:54 +08:00
|
|
|
@exist_file_perms = ::File.stat(file_name).mode
|
2013-08-27 04:26:14 +08:00
|
|
|
::File.open(name, 'rb') do |f|
|
2013-06-03 18:36:06 +08:00
|
|
|
read_from_stream(f)
|
|
|
|
end
|
|
|
|
when create
|
|
|
|
@entry_set = EntrySet.new
|
|
|
|
else
|
2014-01-24 17:37:38 +08:00
|
|
|
raise Error, "File #{file_name} not found"
|
2010-11-30 16:27:59 +08:00
|
|
|
end
|
2013-10-20 03:04:54 +08:00
|
|
|
@stored_entries = @entry_set.dup
|
|
|
|
@stored_comment = @comment
|
2013-10-27 22:30:48 +08:00
|
|
|
@restore_ownership = options[:restore_ownership] || false
|
|
|
|
@restore_permissions = options[:restore_permissions] || true
|
|
|
|
@restore_times = options[:restore_times] || true
|
2010-11-30 16:27:59 +08:00
|
|
|
end
|
|
|
|
|
2012-02-14 00:55:08 +08:00
|
|
|
class << self
|
|
|
|
# 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 File.open method.
|
2013-10-20 03:04:54 +08:00
|
|
|
def open(file_name, create = nil)
|
|
|
|
zf = ::Zip::File.new(file_name, create)
|
2014-01-24 17:37:38 +08:00
|
|
|
return zf unless block_given?
|
|
|
|
begin
|
|
|
|
yield zf
|
2014-06-18 00:08:26 +08:00
|
|
|
zf
|
2014-01-24 17:37:38 +08:00
|
|
|
ensure
|
|
|
|
zf.close
|
2011-11-16 23:11:18 +08:00
|
|
|
end
|
2010-11-30 16:27:59 +08:00
|
|
|
end
|
|
|
|
|
2012-02-14 00:55:08 +08:00
|
|
|
# Same as #open. But outputs data to a buffer instead of a file
|
|
|
|
def add_buffer
|
2014-01-19 19:45:58 +08:00
|
|
|
io = ::StringIO.new('')
|
|
|
|
zf = ::Zip::File.new(io, true, true)
|
2011-01-07 21:23:22 +08:00
|
|
|
yield zf
|
2014-01-19 19:45:58 +08:00
|
|
|
zf.write_buffer(io)
|
2012-02-14 00:55:08 +08:00
|
|
|
end
|
|
|
|
|
2012-05-21 17:21:12 +08:00
|
|
|
# Like #open, but reads zip archive contents from a String or open IO
|
|
|
|
# stream, and outputs data to a buffer.
|
2014-01-19 19:45:58 +08:00
|
|
|
# (This can be used to extract data from a
|
2012-05-21 17:21:12 +08:00
|
|
|
# downloaded zip archive without first saving it to disk.)
|
2013-10-27 22:30:48 +08:00
|
|
|
def open_buffer(io, options = {})
|
2013-12-03 07:57:42 +08:00
|
|
|
unless io.is_a?(IO) || io.is_a?(String) || io.is_a?(Tempfile)
|
|
|
|
raise "Zip::File.open_buffer expects an argument of class String, IO, or Tempfile. Found: #{io.class}"
|
2013-08-27 04:26:14 +08:00
|
|
|
end
|
2013-09-07 00:20:06 +08:00
|
|
|
if io.is_a?(::String)
|
2012-05-21 17:21:12 +08:00
|
|
|
require 'stringio'
|
2013-08-27 04:26:14 +08:00
|
|
|
io = ::StringIO.new(io)
|
2012-05-21 17:21:12 +08:00
|
|
|
end
|
2014-01-19 19:45:58 +08:00
|
|
|
zf = ::Zip::File.new(io, true, true, options)
|
2013-08-27 04:26:14 +08:00
|
|
|
zf.read_from_stream(io)
|
2012-05-21 17:21:12 +08:00
|
|
|
yield zf
|
2014-01-19 19:45:58 +08:00
|
|
|
zf.write_buffer(io)
|
2012-05-21 17:21:12 +08:00
|
|
|
end
|
|
|
|
|
2012-02-14 00:55:08 +08:00
|
|
|
# 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).
|
|
|
|
def foreach(aZipFileName, &block)
|
|
|
|
open(aZipFileName) do |zipFile|
|
|
|
|
zipFile.each(&block)
|
|
|
|
end
|
2011-01-07 21:23:22 +08:00
|
|
|
end
|
2013-04-07 13:16:18 +08:00
|
|
|
|
2013-06-03 18:36:06 +08:00
|
|
|
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
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2013-07-01 05:11:45 +08:00
|
|
|
def put_split_signature(szip_file, segment_size)
|
|
|
|
signature_packed = [SPLIT_SIGNATURE].pack('V')
|
|
|
|
szip_file << signature_packed
|
|
|
|
segment_size - signature_packed.size
|
|
|
|
end
|
|
|
|
|
2013-06-03 18:36:06 +08:00
|
|
|
#
|
|
|
|
# TODO: Make the code more understandable
|
|
|
|
#
|
|
|
|
def save_splited_part(zip_file, partial_zip_file_name, zip_file_size, szip_file_index, segment_size, segment_count)
|
2013-10-20 03:04:54 +08:00
|
|
|
ssegment_size = zip_file_size - zip_file.pos
|
|
|
|
ssegment_size = segment_size if ssegment_size > segment_size
|
2013-07-01 05:11:45 +08:00
|
|
|
szip_file_name = "#{partial_zip_file_name}.#{'%03d'%(szip_file_index)}"
|
2013-06-03 18:36:06 +08:00
|
|
|
::File.open(szip_file_name, 'wb') do |szip_file|
|
|
|
|
if szip_file_index == 1
|
2013-07-01 05:11:45 +08:00
|
|
|
ssegment_size = put_split_signature(szip_file, segment_size)
|
2013-06-03 18:36:06 +08:00
|
|
|
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
|
2013-07-01 05:11:45 +08:00
|
|
|
chunk = zip_file.read(buffer_size)
|
|
|
|
chunk_bytes += buffer_size
|
2013-06-03 18:36:06 +08:00
|
|
|
szip_file << chunk
|
|
|
|
# Info for track splitting
|
|
|
|
yield segment_count, szip_file_index, chunk_bytes, ssegment_size if block_given?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-04-07 13:16:18 +08:00
|
|
|
# Splits an archive into parts with segment size
|
2013-06-03 18:36:06 +08:00
|
|
|
def split(zip_file_name, segment_size = MAX_SEGMENT_SIZE, delete_zip_file = true, partial_zip_file_name = nil)
|
2014-02-07 07:00:38 +08:00
|
|
|
raise Error, "File #{zip_file_name} not found" unless ::File.exist?(zip_file_name)
|
2013-04-07 13:16:18 +08:00
|
|
|
raise Errno::ENOENT, zip_file_name unless ::File.readable?(zip_file_name)
|
|
|
|
zip_file_size = ::File.size(zip_file_name)
|
2013-07-01 05:11:45 +08:00
|
|
|
segment_size = get_segment_size_for_split(segment_size)
|
2013-06-03 18:36:06 +08:00
|
|
|
return if zip_file_size <= segment_size
|
|
|
|
segment_count = get_segment_count_for_split(zip_file_size, segment_size)
|
|
|
|
# Checking for correct zip structure
|
|
|
|
self.open(zip_file_name) {}
|
|
|
|
partial_zip_file_name = get_partial_zip_file_name(zip_file_name, partial_zip_file_name)
|
2013-07-01 05:11:45 +08:00
|
|
|
szip_file_index = 0
|
2013-04-07 13:16:18 +08:00
|
|
|
::File.open(zip_file_name, 'rb') do |zip_file|
|
|
|
|
until zip_file.eof?
|
|
|
|
szip_file_index += 1
|
2013-06-03 18:36:06 +08:00
|
|
|
save_splited_part(zip_file, partial_zip_file_name, zip_file_size, szip_file_index, segment_size, segment_count)
|
2013-04-07 13:16:18 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
::File.delete(zip_file_name) if delete_zip_file
|
|
|
|
szip_file_index
|
|
|
|
end
|
2011-01-07 21:23:22 +08:00
|
|
|
end
|
|
|
|
|
2010-11-30 16:27:59 +08:00
|
|
|
|
|
|
|
# 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.
|
|
|
|
def get_input_stream(entry, &aProc)
|
|
|
|
get_entry(entry).get_input_stream(&aProc)
|
|
|
|
end
|
|
|
|
|
2013-10-11 17:40:44 +08:00
|
|
|
# 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
|
2014-01-19 19:45:58 +08:00
|
|
|
# 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
|
2013-10-11 17:40:44 +08:00
|
|
|
# File.open method.
|
2014-01-19 19:45:58 +08:00
|
|
|
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 =
|
2013-10-11 17:40:44 +08:00
|
|
|
if entry.kind_of?(Entry)
|
|
|
|
entry
|
|
|
|
else
|
|
|
|
Entry.new(@name, entry.to_s, comment, extra, compressed_size, crc, compression_method, size, time)
|
|
|
|
end
|
2014-01-19 19:45:58 +08:00
|
|
|
if new_entry.directory?
|
2011-11-17 17:48:42 +08:00
|
|
|
raise ArgumentError,
|
2014-01-19 19:45:58 +08:00
|
|
|
"cannot open stream to directory entry - '#{new_entry}'"
|
2010-11-30 16:27:59 +08:00
|
|
|
end
|
2014-01-19 19:45:58 +08:00
|
|
|
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)
|
2010-11-30 16:27:59 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
# Returns the name of the zip archive
|
|
|
|
def to_s
|
|
|
|
@name
|
|
|
|
end
|
|
|
|
|
|
|
|
# Returns a string containing the contents of the specified entry
|
|
|
|
def read(entry)
|
2011-01-07 21:23:22 +08:00
|
|
|
get_input_stream(entry) { |is| is.read }
|
2010-11-30 16:27:59 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
# Convenience method for adding the contents of a file to the archive
|
2014-01-24 17:37:38 +08:00
|
|
|
def add(entry, src_path, &continue_on_exists_proc)
|
|
|
|
continue_on_exists_proc ||= proc { ::Zip.continue_on_exists_proc }
|
2013-08-15 06:00:27 +08:00
|
|
|
check_entry_exists(entry, continue_on_exists_proc, "add")
|
2014-01-24 17:37:38 +08:00
|
|
|
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
|
2010-11-30 16:27:59 +08:00
|
|
|
end
|
2011-01-07 21:23:22 +08:00
|
|
|
|
2010-11-30 16:27:59 +08:00
|
|
|
# Removes the specified entry.
|
|
|
|
def remove(entry)
|
2013-06-03 02:33:03 +08:00
|
|
|
@entry_set.delete(get_entry(entry))
|
2010-11-30 16:27:59 +08:00
|
|
|
end
|
2011-01-07 21:23:22 +08:00
|
|
|
|
2010-11-30 16:27:59 +08:00
|
|
|
# Renames the specified entry.
|
2013-08-15 06:00:27 +08:00
|
|
|
def rename(entry, new_name, &continue_on_exists_proc)
|
2010-11-30 16:27:59 +08:00
|
|
|
foundEntry = get_entry(entry)
|
2013-08-15 06:00:27 +08:00
|
|
|
check_entry_exists(new_name, continue_on_exists_proc, 'rename')
|
2013-06-03 02:33:03 +08:00
|
|
|
@entry_set.delete(foundEntry)
|
2013-08-15 06:00:27 +08:00
|
|
|
foundEntry.name = new_name
|
2013-06-03 02:33:03 +08:00
|
|
|
@entry_set << foundEntry
|
2010-11-30 16:27:59 +08:00
|
|
|
end
|
|
|
|
|
2011-01-07 21:23:22 +08:00
|
|
|
# Replaces the specified entry with the contents of srcPath (from
|
2010-11-30 16:27:59 +08:00
|
|
|
# the file system).
|
|
|
|
def replace(entry, srcPath)
|
|
|
|
check_file(srcPath)
|
2012-02-06 08:57:06 +08:00
|
|
|
remove(entry)
|
|
|
|
add(entry, srcPath)
|
2010-11-30 16:27:59 +08:00
|
|
|
end
|
|
|
|
|
2013-06-03 02:33:03 +08:00
|
|
|
# Extracts entry to file dest_path.
|
|
|
|
def extract(entry, dest_path, &block)
|
2014-01-24 17:37:38 +08:00
|
|
|
block ||= proc { ::Zip.on_exists_proc }
|
2013-06-03 02:33:03 +08:00
|
|
|
found_entry = get_entry(entry)
|
|
|
|
found_entry.extract(dest_path, &block)
|
2010-11-30 16:27:59 +08:00
|
|
|
end
|
|
|
|
|
2011-01-07 21:23:22 +08:00
|
|
|
# Commits changes that has been made since the previous commit to
|
2010-11-30 16:27:59 +08:00
|
|
|
# the zip archive.
|
|
|
|
def commit
|
2014-01-24 17:37:38 +08:00
|
|
|
return unless commit_required?
|
|
|
|
on_success_replace do |tmp_file|
|
|
|
|
::Zip::OutputStream.open(tmp_file) do |zos|
|
2013-10-20 03:04:54 +08:00
|
|
|
@entry_set.each do |e|
|
2011-11-17 17:48:42 +08:00
|
|
|
e.write_to_zip_output_stream(zos)
|
|
|
|
e.dirty = false
|
2014-04-05 05:32:11 +08:00
|
|
|
e.clean_up
|
2013-10-20 03:04:54 +08:00
|
|
|
end
|
2011-11-16 23:11:18 +08:00
|
|
|
zos.comment = comment
|
2013-10-20 03:04:54 +08:00
|
|
|
end
|
2011-11-16 23:11:18 +08:00
|
|
|
true
|
2013-10-20 03:04:54 +08:00
|
|
|
end
|
2010-11-30 16:27:59 +08:00
|
|
|
initialize(name)
|
|
|
|
end
|
|
|
|
|
2011-01-07 21:23:22 +08:00
|
|
|
# Write buffer write changes to buffer and return
|
2014-03-18 21:09:50 +08:00
|
|
|
def write_buffer(io = ::StringIO.new(''))
|
2014-01-24 17:37:38 +08:00
|
|
|
::Zip::OutputStream.write_buffer(io) do |zos|
|
2013-06-03 02:33:03 +08:00
|
|
|
@entry_set.each { |e| e.write_to_zip_output_stream(zos) }
|
2011-01-07 21:23:22 +08:00
|
|
|
zos.comment = comment
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-11-30 16:27:59 +08:00
|
|
|
# Closes the zip file committing any changes that has been made.
|
|
|
|
def close
|
|
|
|
commit
|
|
|
|
end
|
|
|
|
|
|
|
|
# Returns true if any changes has been made to this archive since
|
|
|
|
# the previous commit
|
|
|
|
def commit_required?
|
2013-06-03 02:33:03 +08:00
|
|
|
@entry_set.each do |e|
|
2011-11-17 17:48:42 +08:00
|
|
|
return true if e.dirty
|
|
|
|
end
|
2014-01-24 17:37:38 +08:00
|
|
|
@comment != @stored_comment || @entry_set != @stored_entries || @create == ::Zip::File::CREATE
|
2010-11-30 16:27:59 +08:00
|
|
|
end
|
|
|
|
|
2011-01-07 21:23:22 +08:00
|
|
|
# Searches for entry with the specified name. Returns nil if
|
2010-11-30 16:27:59 +08:00
|
|
|
# no entry is found. See also get_entry
|
2012-03-26 14:57:14 +08:00
|
|
|
def find_entry(entry_name)
|
2013-06-03 02:33:03 +08:00
|
|
|
@entry_set.find_entry(entry_name)
|
2010-11-30 16:27:59 +08:00
|
|
|
end
|
|
|
|
|
2012-05-19 05:57:19 +08:00
|
|
|
# Searches for entries given a glob
|
2013-06-03 18:36:06 +08:00
|
|
|
def glob(*args, &block)
|
|
|
|
@entry_set.glob(*args, &block)
|
2012-05-19 05:57:19 +08:00
|
|
|
end
|
|
|
|
|
2010-11-30 16:27:59 +08:00
|
|
|
# Searches for an entry just as find_entry, but throws Errno::ENOENT
|
|
|
|
# if no entry is found.
|
|
|
|
def get_entry(entry)
|
2013-10-21 04:09:40 +08:00
|
|
|
selected_entry = find_entry(entry)
|
|
|
|
unless selected_entry
|
2011-11-16 23:17:19 +08:00
|
|
|
raise Errno::ENOENT, entry
|
2010-11-30 16:27:59 +08:00
|
|
|
end
|
2013-10-21 04:09:40 +08:00
|
|
|
selected_entry.restore_ownership = @restore_ownership
|
|
|
|
selected_entry.restore_permissions = @restore_permissions
|
|
|
|
selected_entry.restore_times = @restore_times
|
|
|
|
selected_entry
|
2010-11-30 16:27:59 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
# Creates a directory
|
|
|
|
def mkdir(entryName, permissionInt = 0755)
|
|
|
|
if find_entry(entryName)
|
|
|
|
raise Errno::EEXIST, "File exists - #{entryName}"
|
|
|
|
end
|
2012-03-13 16:26:51 +08:00
|
|
|
entryName = entryName.dup.to_s
|
2012-02-01 17:59:56 +08:00
|
|
|
entryName << '/' unless entryName.end_with?('/')
|
2013-10-21 04:09:40 +08:00
|
|
|
@entry_set << ::Zip::StreamableDirectory.new(@name, entryName, nil, permissionInt)
|
2010-11-30 16:27:59 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def is_directory(newEntry, srcPath)
|
2012-02-01 03:46:42 +08:00
|
|
|
srcPathIsDirectory = ::File.directory?(srcPath)
|
2013-06-03 18:36:06 +08:00
|
|
|
if newEntry.is_directory && !srcPathIsDirectory
|
2011-11-16 23:11:18 +08:00
|
|
|
raise ArgumentError,
|
2013-06-03 18:36:06 +08:00
|
|
|
"entry name '#{newEntry}' indicates directory entry, but "+
|
|
|
|
"'#{srcPath}' is not a directory"
|
2012-02-01 03:46:42 +08:00
|
|
|
elsif !newEntry.is_directory && srcPathIsDirectory
|
2011-11-16 23:11:18 +08:00
|
|
|
newEntry.name += "/"
|
2010-11-30 16:27:59 +08:00
|
|
|
end
|
2012-02-01 03:46:42 +08:00
|
|
|
newEntry.is_directory && srcPathIsDirectory
|
2010-11-30 16:27:59 +08:00
|
|
|
end
|
|
|
|
|
2013-08-15 06:00:27 +08:00
|
|
|
def check_entry_exists(entryName, continue_on_exists_proc, procedureName)
|
|
|
|
continue_on_exists_proc ||= proc { Zip.continue_on_exists_proc }
|
2013-06-03 02:33:03 +08:00
|
|
|
if @entry_set.include?(entryName)
|
2013-08-15 06:00:27 +08:00
|
|
|
if continue_on_exists_proc.call
|
2011-11-16 23:11:18 +08:00
|
|
|
remove get_entry(entryName)
|
|
|
|
else
|
2014-01-24 17:37:38 +08:00
|
|
|
raise ::Zip::EntryExistsError,
|
2013-06-03 18:36:06 +08:00
|
|
|
procedureName + " failed. Entry #{entryName} already exists"
|
2011-11-16 23:11:18 +08:00
|
|
|
end
|
2010-11-30 16:27:59 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def check_file(path)
|
2012-02-01 03:46:42 +08:00
|
|
|
unless ::File.readable?(path)
|
2011-11-16 23:11:18 +08:00
|
|
|
raise Errno::ENOENT, path
|
2010-11-30 16:27:59 +08:00
|
|
|
end
|
|
|
|
end
|
2011-01-07 21:23:22 +08:00
|
|
|
|
2013-10-20 03:04:54 +08:00
|
|
|
def on_success_replace
|
2014-01-24 17:37:38 +08:00
|
|
|
tmpfile = get_tempfile
|
2013-10-20 03:04:54 +08:00
|
|
|
tmp_filename = tmpfile.path
|
2010-11-30 16:27:59 +08:00
|
|
|
tmpfile.close
|
2013-10-20 03:04:54 +08:00
|
|
|
if yield tmp_filename
|
|
|
|
::File.rename(tmp_filename, self.name)
|
|
|
|
if defined?(@exist_file_perms)
|
|
|
|
::File.chmod(@exist_file_perms, self.name)
|
|
|
|
end
|
2010-11-30 16:27:59 +08:00
|
|
|
end
|
|
|
|
end
|
2011-01-07 21:23:22 +08:00
|
|
|
|
2010-11-30 16:27:59 +08:00
|
|
|
def get_tempfile
|
2013-10-20 03:04:54 +08:00
|
|
|
temp_file = Tempfile.new(::File.basename(name), ::File.dirname(name))
|
|
|
|
temp_file.binmode
|
|
|
|
temp_file
|
2010-11-30 16:27:59 +08:00
|
|
|
end
|
2011-01-07 21:23:22 +08:00
|
|
|
|
2010-11-30 16:27:59 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Copyright (C) 2002, 2003 Thomas Sondergaard
|
|
|
|
# rubyzip is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the ruby license.
|