From ec81c30382cc6ee229ec049e5f9bde55866dae1d Mon Sep 17 00:00:00 2001 From: Alexander Simonov Date: Fri, 24 Jan 2014 11:37:38 +0200 Subject: [PATCH] Fix for #126 and #127 --- README.md | 34 +++++---------------- TODO | 1 - lib/zip.rb | 3 +- lib/zip/central_directory.rb | 16 +++++----- lib/zip/entry.rb | 40 +++++++++++++------------ lib/zip/errors.rb | 12 ++++---- lib/zip/extra_field.rb | 2 +- lib/zip/file.rb | 44 +++++++++++++--------------- lib/zip/input_stream.rb | 2 +- lib/zip/output_stream.rb | 28 +++++++++--------- test/central_directory_entry_test.rb | 2 +- test/central_directory_test.rb | 5 ++-- test/entry_test.rb | 2 +- test/file_extract_directory_test.rb | 2 +- test/file_extract_test.rb | 2 +- test/file_test.rb | 28 ++++++++++++++++-- test/local_entry_test.rb | 22 +++++++++++++- test/settings_test.rb | 4 +-- testAllRubies.sh | 4 --- 19 files changed, 138 insertions(+), 115 deletions(-) delete mode 100755 testAllRubies.sh diff --git a/README.md b/README.md index b659c7a6..b22a040c 100644 --- a/README.md +++ b/README.md @@ -116,32 +116,6 @@ end File.open(new_path, "w") {|f| f.write(buffer.string) } ``` -## Further Documentation - -There is more than one way to access or create a zip archive with -rubyzip. The basic API is modeled after the classes in -java.util.zip from the Java SDK. This means there are classes such -as Zip::InputStream, Zip::OutputStream and -Zip::File. Zip::InputStream provides a basic interface for -iterating through the entries in a zip archive and reading from the -entries in the same way as from a regular File or IO -object. OutputStream is the corresponding basic output -facility. Zip::File provides a mean for accessing the archives -central directory and provides means for accessing any entry without -having to iterate through the archive. Unlike Java's -java.util.zip.ZipFile rubyzip's Zip::File is mutable, which means -it can be used to change zip files as well. - -Another way to access a zip archive with rubyzip is to use rubyzip's -Zip::FileSystem API. Using this API files can be read from and -written to the archive in much the same manner as ruby's builtin -classes allows files to be read from and written to the file system. - -For details about the specific behaviour of classes and methods refer -to the test suite. Finally you can generate the rdoc documentation or -visit http://rubyzip.sourceforge.net. - - ## Configuration By default, rubyzip will not overwrite files if they already exist inside of the extracted path. To change this behavior, you may specify a configuration option like so: @@ -183,6 +157,14 @@ All settings in same time end ``` +By default Zip64 support is disabled for writing. To enable it do next: + +```ruby +Zip.write_zip64_support = true +``` + +_NOTE_: If you will enable Zip64 writing then you will need zip extractor with Zip64 support to extract archive. + ## Developing To run tests you need run next commands: diff --git a/TODO b/TODO index 0e89e394..16b9a2e7 100644 --- a/TODO +++ b/TODO @@ -4,7 +4,6 @@ * Suggestion: Add ZipFile/ZipInputStream example that demonstrates extracting all entries. * Suggestion: ZipFile#extract destination should default to "." * Suggestion: ZipEntry should have extract(), get_input_stream() methods etc -* Suggestion: ZipInputStream/ZipOutputStream should accept an IO object in addition to a filename. * (is buffering used anywhere with write?) * Inflater.sysread should pass the buffer to produce_input. * Implement ZipFsDir.glob diff --git a/lib/zip.rb b/lib/zip.rb index 55f78f27..1ddafa8e 100644 --- a/lib/zip.rb +++ b/lib/zip.rb @@ -34,7 +34,7 @@ module Zip extend self - attr_accessor :unicode_names, :on_exists_proc, :continue_on_exists_proc, :sort_entries, :default_compression + attr_accessor :unicode_names, :on_exists_proc, :continue_on_exists_proc, :sort_entries, :default_compression, :write_zip64_support def reset! @_ran_once = false @@ -43,6 +43,7 @@ def reset! @continue_on_exists_proc = false @sort_entries = false @default_compression = ::Zlib::DEFAULT_COMPRESSION + @write_zip64_support = false end def setup diff --git a/lib/zip/central_directory.rb b/lib/zip/central_directory.rb index 50d3756f..33aeebb2 100755 --- a/lib/zip/central_directory.rb +++ b/lib/zip/central_directory.rb @@ -93,7 +93,7 @@ def read_64_e_o_c_d(buf) #:nodoc: @size_in_bytes = Entry.read_zip_64_long(buf) @cdir_offset = Entry.read_zip_64_long(buf) @zip_64_extensible = buf.slice!(0, buf.bytesize) - raise ZipError, "Zip consistency problem while reading eocd structure" unless buf.size == 0 + raise Error, "Zip consistency problem while reading eocd structure" unless buf.size == 0 end def read_e_o_c_d(buf) #:nodoc: @@ -110,14 +110,14 @@ def read_e_o_c_d(buf) #:nodoc: else buf.read(comment_length) end - raise ZipError, "Zip consistency problem while reading eocd structure" unless buf.size == 0 + raise Error, "Zip consistency problem while reading eocd structure" unless buf.size == 0 end def read_central_directory_entries(io) #:nodoc: begin io.seek(@cdir_offset, IO::SEEK_SET) rescue Errno::EINVAL - raise ZipError, "Zip consistency problem while reading central directory entry" + raise Error, "Zip consistency problem while reading central directory entry" end @entry_set = EntrySet.new @size.times do @@ -137,7 +137,7 @@ def read_from_stream(io) #:nodoc: def get_e_o_c_d(buf) #:nodoc: sig_index = buf.rindex([END_OF_CDS].pack('V')) - raise ZipError, "Zip end of central directory signature not found" unless sig_index + raise Error, "Zip end of central directory signature not found" unless sig_index buf = buf.slice!((sig_index + 4)..(buf.bytesize)) def buf.read(count) @@ -162,9 +162,9 @@ def start_buf(io) def get_64_e_o_c_d(buf) #:nodoc: zip_64_start = buf.rindex([ZIP64_END_OF_CDS].pack('V')) - raise ZipError, "Zip64 end of central directory signature not found" unless zip_64_start + raise Error, "Zip64 end of central directory signature not found" unless zip_64_start zip_64_locator = buf.rindex([ZIP64_EOCD_LOCATOR].pack('V')) - raise ZipError, "Zip64 end of central directory signature locator not found" unless zip_64_locator + raise Error, "Zip64 end of central directory signature locator not found" unless zip_64_locator buf = buf.slice!((zip_64_start + 4)..zip_64_locator) def buf.read(count) @@ -179,7 +179,7 @@ def each(&proc) @entry_set.each(&proc) end - # Returns the number of entries in the central directory (and + # Returns the number of entries in the central directory (and # consequently in the zip archive). def size @entry_set.size @@ -189,7 +189,7 @@ def self.read_from_stream(io) #:nodoc: cdir = new cdir.read_from_stream(io) return cdir - rescue ZipError + rescue Error return nil end diff --git a/lib/zip/entry.rb b/lib/zip/entry.rb index 44558d90..320b5b2b 100755 --- a/lib/zip/entry.rb +++ b/lib/zip/entry.rb @@ -46,7 +46,7 @@ def set_default_vars_values def check_name(name) if name.start_with?('/') - raise ::Zip::ZipEntryNameError, "Illegal ZipEntry name '#{name}', name must not start with /" + raise ::Zip::EntryNameError, "Illegal ZipEntry name '#{name}', name must not start with /" end end @@ -92,7 +92,7 @@ def time=(value) end def file_type_is?(type) - raise ZipInternalError, "current filetype is unknown: #{self.inspect}" unless @ftype + raise InternalError, "current filetype is unknown: #{self.inspect}" unless @ftype @ftype == type end @@ -132,7 +132,7 @@ def calculate_local_header_size #:nodoc:all def verify_local_header_size! return if @local_header_size == 0 new_size = calculate_local_header_size - raise ZipError, "local header size changed (#{@local_header_size} -> #{new_size})" if @local_header_size != new_size + raise Error, "local header size changed (#{@local_header_size} -> #{new_size})" if @local_header_size != new_size end def cdir_header_size #:nodoc:all @@ -185,15 +185,15 @@ def read_c_dir_entry(io) #:nodoc:all entry = new(path) entry.read_c_dir_entry(io) entry - rescue ZipError + rescue Error nil end def read_local_entry(io) - entry = self.new + entry = self.new(io) entry.read_local_entry(io) entry - rescue ZipError + rescue Error nil end @@ -222,13 +222,13 @@ def read_local_entry(io) #:nodoc:all static_sized_fields_buf = io.read(::Zip::LOCAL_ENTRY_STATIC_HEADER_LENGTH) unless static_sized_fields_buf.bytesize == ::Zip::LOCAL_ENTRY_STATIC_HEADER_LENGTH - raise ZipError, "Premature end of file. Not enough data for zip entry local header" + raise Error, "Premature end of file. Not enough data for zip entry local header" end unpack_local_entry(static_sized_fields_buf) unless @header_signature == ::Zip::LOCAL_ENTRY_SIGNATURE - raise ::Zip::ZipError, "Zip local header magic not found at location '#{local_header_offset}'" + raise ::Zip::Error, "Zip local header magic not found at location '#{local_header_offset}'" end set_time(@last_mod_date, @last_mod_time) @@ -238,7 +238,7 @@ def read_local_entry(io) #:nodoc:all @name.gsub!('\\', '/') if extra && extra.bytesize != @extra_length - raise ::Zip::ZipError, "Truncated local zip entry header" + raise ::Zip::Error, "Truncated local zip entry header" else if ::Zip::ExtraField === @extra @extra.merge(extra) @@ -332,19 +332,19 @@ def set_ftype_from_c_dir_entry def check_c_dir_entry_static_header_length(buf) unless buf.bytesize == ::Zip::CDIR_ENTRY_STATIC_HEADER_LENGTH - raise ZipError, 'Premature end of file. Not enough data for zip cdir entry header' + raise Error, 'Premature end of file. Not enough data for zip cdir entry header' end end def check_c_dir_entry_signature unless header_signature == ::Zip::CENTRAL_DIRECTORY_ENTRY_SIGNATURE - raise ZipError, "Zip local header magic not found at location '#{local_header_offset}'" + raise Error, "Zip local header magic not found at location '#{local_header_offset}'" end end def check_c_dir_entry_comment_size unless @comment && @comment.bytesize == @comment_length - raise ::Zip::ZipError, "Truncated cdir zip entry header" + raise ::Zip::Error, "Truncated cdir zip entry header" end end @@ -427,7 +427,10 @@ def pack_c_dir_entry (zip64 && zip64.disk_start_number) ? 0xFFFF : 0, # disk number start @internal_file_attributes, # file type (binary=0, text=1) @external_file_attributes, # native filesystem attributes - (zip64 && zip64.relative_header_offset) ? 0xFFFFFFFF : @local_header_offset + (zip64 && zip64.relative_header_offset) ? 0xFFFFFFFF : @local_header_offset, + @name, + @extra, + @comment ].pack('VCCvvvvvVVVvvvvvVV') end @@ -516,7 +519,7 @@ def gather_fileinfo_from_srcpath(src_path) # :nodoc: end :file when 'directory' - @name += "/" unless name_is_directory? + @name += '/' unless name_is_directory? :directory when 'link' if name_is_directory? @@ -568,7 +571,7 @@ def set_time(binary_dos_date, binary_dos_time) def create_file(dest_path, continue_on_exists_proc = proc { Zip.continue_on_exists_proc }) if ::File.exists?(dest_path) && !yield(self, dest_path) - raise ::Zip::ZipDestinationFileExistsError, + raise ::Zip::DestinationFileExistsError, "Destination '#{dest_path}' already exists" end ::File.open(dest_path, "wb") do |os| @@ -589,7 +592,7 @@ def create_directory(dest_path) if block_given? && yield(self, dest_path) ::FileUtils::rm_f dest_path else - raise ::Zip::ZipDestinationFileExistsError, + raise ::Zip::DestinationFileExistsError, "Cannot create directory '#{dest_path}'. "+ "A file already exists with that name" end @@ -614,12 +617,12 @@ def create_symlink(dest_path) if ::File.readlink(dest_path) == linkto return else - raise ZipDestinationFileExistsError, + raise ::Zip::DestinationFileExistsError, "Cannot create symlink '#{dest_path}'. "+ "A symlink already exists with that name" end else - raise ZipDestinationFileExistsError, + raise ::Zip::DestinationFileExistsError, "Cannot create symlink '#{dest_path}'. "+ "A file already exists with that name" end @@ -642,6 +645,7 @@ def parse_zip64_extra(for_local_header) #:nodoc:all # create a zip64 extra information field if we need one def prep_zip64_extra(for_local_header) #:nodoc:all + return unless ::Zip.write_zip64_support need_zip64 = @size >= 0xFFFFFFFF || @compressed_size >= 0xFFFFFFFF unless for_local_header need_zip64 ||= @local_header_offset >= 0xFFFFFFFF diff --git a/lib/zip/errors.rb b/lib/zip/errors.rb index 7043ee95..78e31564 100644 --- a/lib/zip/errors.rb +++ b/lib/zip/errors.rb @@ -1,8 +1,8 @@ module Zip - class ZipError < StandardError; end - class ZipEntryExistsError < ZipError; end - class ZipDestinationFileExistsError < ZipError; end - class ZipCompressionMethodError < ZipError; end - class ZipEntryNameError < ZipError; end - class ZipInternalError < ZipError; end + class Error < StandardError; end + class EntryExistsError < Error; end + class DestinationFileExistsError < Error; end + class CompressionMethodError < Error; end + class EntryNameError < Error; end + class InternalError < Error; end end diff --git a/lib/zip/extra_field.rb b/lib/zip/extra_field.rb index 6760a4a1..80b70f11 100755 --- a/lib/zip/extra_field.rb +++ b/lib/zip/extra_field.rb @@ -52,7 +52,7 @@ def merge(binstr) def create(name) unless field_class = ID_MAP.values.find { |k| k.name == name } - raise ZipError, "Unknown extra field '#{name}'" + raise Error, "Unknown extra field '#{name}'" end self[name] = field_class.new end diff --git a/lib/zip/file.rb b/lib/zip/file.rb index 19002ff8..848c3301 100755 --- a/lib/zip/file.rb +++ b/lib/zip/file.rb @@ -79,7 +79,7 @@ def initialize(file_name, create = nil, buffer = false, options = {}) when create @entry_set = EntrySet.new else - raise ZipError, "File #{file_name} not found" + raise Error, "File #{file_name} not found" end @stored_entries = @entry_set.dup @stored_comment = @comment @@ -94,14 +94,11 @@ class << self # ruby's builtin File.open method. def open(file_name, create = nil) zf = ::Zip::File.new(file_name, create) - if block_given? - begin - yield zf - ensure - zf.close - end - else - zf + return zf unless block_given? + begin + yield zf + ensure + zf.close end end @@ -197,7 +194,7 @@ def save_splited_part(zip_file, partial_zip_file_name, zip_file_size, szip_file_ # Splits an archive into parts with segment size def split(zip_file_name, segment_size = MAX_SEGMENT_SIZE, delete_zip_file = true, partial_zip_file_name = nil) - raise ZipError, "File #{zip_file_name} not found" unless ::File.exists?(zip_file_name) + raise Error, "File #{zip_file_name} not found" unless ::File.exists?(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) @@ -259,12 +256,13 @@ def read(entry) end # Convenience method for adding the contents of a file to the archive - def add(entry, srcPath, &continue_on_exists_proc) - continue_on_exists_proc ||= proc { Zip.continue_on_exists_proc } + 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") - newEntry = entry.kind_of?(Entry) ? entry : Entry.new(@name, entry.to_s) - newEntry.gather_fileinfo_from_srcpath(srcPath) - @entry_set << newEntry + 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 # Removes the specified entry. @@ -291,7 +289,7 @@ def replace(entry, srcPath) # Extracts entry to file dest_path. def extract(entry, dest_path, &block) - block ||= proc { ::Zip.on_exists_proc } + block ||= proc { ::Zip.on_exists_proc } found_entry = get_entry(entry) found_entry.extract(dest_path, &block) end @@ -299,9 +297,9 @@ def extract(entry, dest_path, &block) # Commits changes that has been made since the previous commit to # the zip archive. def commit - return if !commit_required? - on_success_replace do |tmpFile| - ::Zip::OutputStream.open(tmpFile) do |zos| + 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 @@ -315,7 +313,7 @@ def commit # Write buffer write changes to buffer and return def write_buffer(io) - OutputStream.write_buffer(io) do |zos| + ::Zip::OutputStream.write_buffer(io) do |zos| @entry_set.each { |e| e.write_to_zip_output_stream(zos) } zos.comment = comment end @@ -332,7 +330,7 @@ def commit_required? @entry_set.each do |e| return true if e.dirty end - @comment != @stored_comment || @entry_set != @stored_entries || @create == File::CREATE + @comment != @stored_comment || @entry_set != @stored_entries || @create == ::Zip::File::CREATE end # Searches for entry with the specified name. Returns nil if @@ -389,7 +387,7 @@ def check_entry_exists(entryName, continue_on_exists_proc, procedureName) if continue_on_exists_proc.call remove get_entry(entryName) else - raise ZipEntryExistsError, + raise ::Zip::EntryExistsError, procedureName + " failed. Entry #{entryName} already exists" end end @@ -402,7 +400,7 @@ def check_file(path) end def on_success_replace - tmpfile = get_tempfile + tmpfile = get_tempfile tmp_filename = tmpfile.path tmpfile.close if yield tmp_filename diff --git a/lib/zip/input_stream.rb b/lib/zip/input_stream.rb index 815fc57b..9ca039b8 100755 --- a/lib/zip/input_stream.rb +++ b/lib/zip/input_stream.rb @@ -138,7 +138,7 @@ def get_decompressor when @current_entry.compression_method == ::Zip::Entry::DEFLATED ::Zip::Inflater.new(@archive_io) else - raise ZipCompressionMethodError, + raise ::Zip::CompressionMethodError, "Unsupported compression method #{@current_entry.compression_method}" end end diff --git a/lib/zip/output_stream.rb b/lib/zip/output_stream.rb index b89f9b68..3409b76e 100755 --- a/lib/zip/output_stream.rb +++ b/lib/zip/output_stream.rb @@ -84,26 +84,26 @@ def close_buffer # Closes the current entry and opens a new for writing. # +entry+ can be a ZipEntry object or a string. - def put_next_entry(entryname, comment = nil, extra = nil, compression_method = Entry::DEFLATED, level = Zip.default_compression) - raise ZipError, "zip stream is closed" if @closed - if entryname.kind_of?(Entry) - new_entry = entryname + def put_next_entry(entry_name, comment = nil, extra = nil, compression_method = Entry::DEFLATED, level = Zip.default_compression) + raise Error, "zip stream is closed" if @closed + if entry_name.kind_of?(Entry) + new_entry = entry_name else - new_entry = Entry.new(@file_name, entryname.to_s) + new_entry = Entry.new(@file_name, entry_name.to_s) end - new_entry.comment = comment if !comment.nil? - if (!extra.nil?) + new_entry.comment = comment unless comment.nil? + unless extra.nil? new_entry.extra = ExtraField === extra ? extra : ExtraField.new(extra.to_s) end - new_entry.compression_method = compression_method if !compression_method.nil? + new_entry.compression_method = compression_method unless compression_method.nil? init_next_entry(new_entry, level) @current_entry = new_entry end def copy_raw_entry(entry) entry = entry.dup - raise ZipError, "zip stream is closed" if @closed - raise ZipError, "entry is not a ZipEntry" unless entry.is_a?(Entry) + raise Error, "zip stream is closed" if @closed + raise Error, "entry is not a ZipEntry" unless entry.is_a?(Entry) finalize_current_entry @entry_set << entry src_pos = entry.local_entry_offset @@ -126,7 +126,7 @@ def finalize_current_entry @current_entry.size = @compressor.size @current_entry.crc = @compressor.crc @current_entry = nil - @compressor = NullCompressor.instance + @compressor = ::Zip::NullCompressor.instance end def init_next_entry(entry, level = Zip.default_compression) @@ -139,11 +139,11 @@ def init_next_entry(entry, level = Zip.default_compression) def get_compressor(entry, level) case entry.compression_method when Entry::DEFLATED then - Deflater.new(@output_stream, level) + ::Zip::Deflater.new(@output_stream, level) when Entry::STORED then - PassThruCompressor.new(@output_stream) + ::Zip::PassThruCompressor.new(@output_stream) else - raise ZipCompressionMethodError, + raise ::Zip::CompressionMethodError, "Invalid compression method: '#{entry.compression_method}'" end end diff --git a/test/central_directory_entry_test.rb b/test/central_directory_entry_test.rb index 1bc856c8..3901d12a 100644 --- a/test/central_directory_entry_test.rb +++ b/test/central_directory_entry_test.rb @@ -67,7 +67,7 @@ def test_ReadEntryFromTruncatedZipFile entry = ::Zip::Entry.new entry.read_c_dir_entry(fragment) fail "ZipError expected" - rescue ::Zip::ZipError + rescue ::Zip::Error end end diff --git a/test/central_directory_test.rb b/test/central_directory_test.rb index bf60bdc3..d16c9e8a 100644 --- a/test/central_directory_test.rb +++ b/test/central_directory_test.rb @@ -23,7 +23,7 @@ def test_readFromInvalidStream cdir.read_from_stream(zipFile) } fail "ZipError expected!" - rescue ::Zip::ZipError + rescue ::Zip::Error end def test_ReadFromTruncatedZipFile @@ -34,7 +34,7 @@ def test_ReadFromTruncatedZipFile entry = ::Zip::CentralDirectory.new entry.read_from_stream(fragment) fail "ZipError expected" - rescue ::Zip::ZipError + rescue ::Zip::Error end def test_write_to_stream @@ -50,6 +50,7 @@ def test_write_to_stream end def test_write64_to_stream + ::Zip.write_zip64_support = true entries = [::Zip::Entry.new("file.zip", "file1-little", "comment1", "", 200, 101, ::Zip::Entry::STORED, 200), ::Zip::Entry.new("file.zip", "file2-big", "comment2", "", 18000000000, 102, ::Zip::Entry::DEFLATED, 20000000000), ::Zip::Entry.new("file.zip", "file3-alsobig", "comment3", "", 15000000000, 103, ::Zip::Entry::DEFLATED, 21000000000), diff --git a/test/entry_test.rb b/test/entry_test.rb index cc6d4f18..1b0a578b 100644 --- a/test/entry_test.rb +++ b/test/entry_test.rb @@ -127,6 +127,6 @@ def test_parentAsString end def test_entry_name_cannot_start_with_slash - assert_raises(::Zip::ZipEntryNameError) { ::Zip::Entry.new("zf.zip", "/hej/der") } + assert_raises(::Zip::EntryNameError) { ::Zip::Entry.new("zf.zip", "/hej/der") } end end diff --git a/test/file_extract_directory_test.rb b/test/file_extract_directory_test.rb index d523dcc9..1c8f82d7 100644 --- a/test/file_extract_directory_test.rb +++ b/test/file_extract_directory_test.rb @@ -36,7 +36,7 @@ def test_extractDirectoryExistsAsDir def test_extractDirectoryExistsAsFile File.open(TEST_OUT_NAME, "w") { |f| f.puts "something" } - assert_raises(::Zip::ZipDestinationFileExistsError) { extract_test_dir } + assert_raises(::Zip::DestinationFileExistsError) { extract_test_dir } end def test_extractDirectoryExistsAsFileOverwrite diff --git a/test/file_extract_test.rb b/test/file_extract_test.rb index b3041586..8c8326f1 100644 --- a/test/file_extract_test.rb +++ b/test/file_extract_test.rb @@ -36,7 +36,7 @@ def test_extractExists writtenText = "written text" ::File.open(EXTRACTED_FILENAME, "w") { |f| f.write(writtenText) } - assert_raises(::Zip::ZipDestinationFileExistsError) { + assert_raises(::Zip::DestinationFileExistsError) { ::Zip::File.open(TEST_ZIP.zip_name) { |zf| zf.extract(zf.entries.first, EXTRACTED_FILENAME) } diff --git a/test/file_test.rb b/test/file_test.rb index c2115950..77cc351c 100644 --- a/test/file_test.rb +++ b/test/file_test.rb @@ -4,6 +4,10 @@ class ZipFileTest < MiniTest::Unit::TestCase include CommonZipFileFixture + def teardown + ::Zip.write_zip64_support = false + end + def test_createFromScratchToBuffer comment = "a short comment" @@ -115,7 +119,7 @@ def test_recover_permissions_after_add_files_to_archive end def test_addExistingEntryName - assert_raises(::Zip::ZipEntryExistsError) { + assert_raises(::Zip::EntryExistsError) { ::Zip::File.open(TEST_ZIP.zip_name) { |zf| zf.add(zf.entries.first.name, "test/data/file2.txt") @@ -228,7 +232,7 @@ def test_renameToExistingEntry oldEntries = nil ::Zip::File.open(TEST_ZIP.zip_name) { |zf| oldEntries = zf.entries } - assert_raises(::Zip::ZipEntryExistsError) do + assert_raises(::Zip::EntryExistsError) do ::Zip::File.open(TEST_ZIP.zip_name) do |zf| zf.rename(zf.entries[0], zf.entries[1].name) end @@ -275,7 +279,7 @@ def test_renameNonEntry def test_renameEntryToExistingEntry entry1, entry2, * = TEST_ZIP.entry_names zf = ::Zip::File.new(TEST_ZIP.zip_name) - assert_raises(::Zip::ZipEntryExistsError) { + assert_raises(::Zip::EntryExistsError) { zf.rename(entry1, entry2) } ensure @@ -324,6 +328,24 @@ def test_commit zfRead.close zf.close + res = system("unzip -t #{TEST_ZIP.zip_name}") + assert_equal(res, true) + end + + def test_double_commit + ::FileUtils.touch('test/data/generated/test_double_commit1.txt') + ::FileUtils.touch('test/data/generated/test_double_commit2.txt') + zf = ::Zip::File.open('test/data/generated/double_commit_test.zip', ::Zip::File::CREATE) + zf.add('test1.txt', 'test/data/generated/test_double_commit1.txt') + zf.commit + zf.add('test2.txt', 'test/data/generated/test_double_commit2.txt') + zf.commit + zf.close + zf2 = ::Zip::File.open('test/data/generated/double_commit_test.zip') + assert(zf2.entries.detect {|e| e.name == 'test1.txt'} != nil ) + assert(zf2.entries.detect {|e| e.name == 'test2.txt'} != nil ) + res = system("unzip -t test/data/generated/double_commit_test.zip") + assert_equal(res, true) end def test_write_buffer diff --git a/test/local_entry_test.rb b/test/local_entry_test.rb index cba362e6..038bfd93 100644 --- a/test/local_entry_test.rb +++ b/test/local_entry_test.rb @@ -1,6 +1,11 @@ require 'test_helper' class ZipLocalEntryTest < MiniTest::Unit::TestCase + + def teardown + ::Zip.write_zip64_support = false + end + def test_read_local_entryHeaderOfFirstTestZipEntry ::File.open(TestZipFile::TEST_ZIP3.zip_name, "rb") do |file| entry = ::Zip::Entry.read_local_entry(file) @@ -42,10 +47,22 @@ def test_read_local_entryFromTruncatedZipFile entry = ::Zip::Entry.new entry.read_local_entry(zipFragment) fail "ZipError expected" - rescue ::Zip::ZipError + rescue ::Zip::Error end def test_writeEntry + entry = ::Zip::Entry.new("file.zip", "entryName", "my little comment", + "thisIsSomeExtraInformation", 100, 987654, + ::Zip::Entry::DEFLATED, 400) + write_to_file("localEntryHeader.bin", "centralEntryHeader.bin", entry) + entryReadLocal, entryReadCentral = read_from_file("localEntryHeader.bin", "centralEntryHeader.bin") + assert(entryReadCentral.extra['Zip64Placeholder'].nil?, 'zip64 placeholder should not be used in central directory') + compare_local_entry_headers(entry, entryReadLocal) + compare_c_dir_entry_headers(entry, entryReadCentral) + end + + def test_writeEntryWithZip64 + ::Zip.write_zip64_support = true entry = ::Zip::Entry.new("file.zip", "entryName", "my little comment", "thisIsSomeExtraInformation", 100, 987654, ::Zip::Entry::DEFLATED, 400) @@ -59,6 +76,7 @@ def test_writeEntry end def test_write64Entry + ::Zip.write_zip64_support = true entry = ::Zip::Entry.new("bigfile.zip", "entryName", "my little equine", "malformed extra field because why not", 0x7766554433221100, 0xDEADBEEF, ::Zip::Entry::DEFLATED, @@ -70,6 +88,7 @@ def test_write64Entry end def test_rewriteLocalHeader64 + ::Zip.write_zip64_support = true buf1 = StringIO.new entry = ::Zip::Entry.new("file.zip", "entryName") entry.write_local_entry(buf1) @@ -94,6 +113,7 @@ def test_readLocalOffset end def test_read64LocalOffset + ::Zip.write_zip64_support = true entry = ::Zip::Entry.new("file.zip", "entryName") entry.local_header_offset = 0x0123456789ABCDEF ::File.open('centralEntryHeader.bin', 'wb') { |f| entry.write_c_dir_entry(f) } diff --git a/test/settings_test.rb b/test/settings_test.rb index 24953179..c84e084d 100644 --- a/test/settings_test.rb +++ b/test/settings_test.rb @@ -34,13 +34,13 @@ def test_true_on_exists_proc def test_false_on_exists_proc Zip.on_exists_proc = false File.open(TEST_OUT_NAME, "w") { |f| f.puts "something" } - assert_raises(Zip::ZipDestinationFileExistsError) { extract_test_dir } + assert_raises(Zip::DestinationFileExistsError) { extract_test_dir } end def test_false_continue_on_exists_proc Zip.continue_on_exists_proc = false - assert_raises(::Zip::ZipEntryExistsError) do + assert_raises(::Zip::EntryExistsError) do ::Zip::File.open(TEST_ZIP.zip_name) do |zf| zf.add(zf.entries.first.name, "test/data/file2.txt") end diff --git a/testAllRubies.sh b/testAllRubies.sh deleted file mode 100755 index a23177fc..00000000 --- a/testAllRubies.sh +++ /dev/null @@ -1,4 +0,0 @@ -for i in ree 1.8.7 1.9.2 1.9.3; do - git clean -nfx - rvm $i do rake -done