Refactor the create logic in Zip::File.
Make the internal @create varible more consistent and actually match the documentation. Zip::File::CREATE is now true, rather than 1. A new test is added to check if passing 1 in still works to ensure backwards compatibility.
This commit is contained in:
parent
63ed0d970b
commit
fc23f68f77
|
@ -43,7 +43,7 @@ module Zip
|
||||||
# interface for accessing the filesystem, ie. the File and Dir classes.
|
# interface for accessing the filesystem, ie. the File and Dir classes.
|
||||||
|
|
||||||
class File < CentralDirectory
|
class File < CentralDirectory
|
||||||
CREATE = 1
|
CREATE = true
|
||||||
SPLIT_SIGNATURE = 0x08074b50
|
SPLIT_SIGNATURE = 0x08074b50
|
||||||
ZIP64_EOCD_SIGNATURE = 0x06064b50
|
ZIP64_EOCD_SIGNATURE = 0x06064b50
|
||||||
MAX_SEGMENT_SIZE = 3_221_225_472
|
MAX_SEGMENT_SIZE = 3_221_225_472
|
||||||
|
@ -64,19 +64,19 @@ module Zip
|
||||||
|
|
||||||
# Opens a zip archive. Pass true as the second parameter to create
|
# Opens a zip archive. Pass true as the second parameter to create
|
||||||
# a new archive if it doesn't exist already.
|
# a new archive if it doesn't exist already.
|
||||||
def initialize(file_name, create = nil, buffer = false, options = {})
|
def initialize(file_name, create = false, buffer = false, options = {})
|
||||||
super()
|
super()
|
||||||
@name = file_name
|
@name = file_name
|
||||||
@comment = ''
|
@comment = ''
|
||||||
@create = create
|
@create = create ? true : false # allow any truthy value to mean true
|
||||||
case
|
case
|
||||||
when !buffer && ::File.size?(file_name)
|
when !buffer && ::File.size?(file_name)
|
||||||
@create = nil
|
@create = false
|
||||||
@file_permissions = ::File.stat(file_name).mode
|
@file_permissions = ::File.stat(file_name).mode
|
||||||
::File.open(name, 'rb') do |f|
|
::File.open(name, 'rb') do |f|
|
||||||
read_from_stream(f)
|
read_from_stream(f)
|
||||||
end
|
end
|
||||||
when create
|
when @create
|
||||||
@entry_set = EntrySet.new
|
@entry_set = EntrySet.new
|
||||||
when ::File.zero?(file_name)
|
when ::File.zero?(file_name)
|
||||||
raise Error, "File #{file_name} has zero size. Did you mean to pass the create flag?"
|
raise Error, "File #{file_name} has zero size. Did you mean to pass the create flag?"
|
||||||
|
@ -94,7 +94,7 @@ module Zip
|
||||||
# Same as #new. If a block is passed the ZipFile object is passed
|
# Same as #new. If a block is passed the ZipFile object is passed
|
||||||
# to the block and is automatically closed afterwards just as with
|
# to the block and is automatically closed afterwards just as with
|
||||||
# ruby's builtin File.open method.
|
# ruby's builtin File.open method.
|
||||||
def open(file_name, create = nil)
|
def open(file_name, create = false)
|
||||||
zf = ::Zip::File.new(file_name, create)
|
zf = ::Zip::File.new(file_name, create)
|
||||||
return zf unless block_given?
|
return zf unless block_given?
|
||||||
begin
|
begin
|
||||||
|
@ -339,7 +339,7 @@ module Zip
|
||||||
@entry_set.each do |e|
|
@entry_set.each do |e|
|
||||||
return true if e.dirty
|
return true if e.dirty
|
||||||
end
|
end
|
||||||
@comment != @stored_comment || @entry_set != @stored_entries || @create == ::Zip::File::CREATE
|
@comment != @stored_comment || @entry_set != @stored_entries || @create
|
||||||
end
|
end
|
||||||
|
|
||||||
# Searches for entry with the specified name. Returns nil if
|
# Searches for entry with the specified name. Returns nil if
|
||||||
|
@ -407,7 +407,7 @@ module Zip
|
||||||
begin
|
begin
|
||||||
if yield tmp_filename
|
if yield tmp_filename
|
||||||
::File.rename(tmp_filename, name)
|
::File.rename(tmp_filename, name)
|
||||||
::File.chmod(@file_permissions, name) if @create.nil?
|
::File.chmod(@file_permissions, name) unless @create
|
||||||
end
|
end
|
||||||
ensure
|
ensure
|
||||||
::File.unlink(tmp_filename) if ::File.exist?(tmp_filename)
|
::File.unlink(tmp_filename) if ::File.exist?(tmp_filename)
|
||||||
|
|
|
@ -40,6 +40,20 @@ class ZipFileTest < MiniTest::Test
|
||||||
assert_equal(2, zfRead.entries.length)
|
assert_equal(2, zfRead.entries.length)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_create_from_scratch_with_old_create_parameter
|
||||||
|
comment = 'a short comment'
|
||||||
|
|
||||||
|
zf = ::Zip::File.new(EMPTY_FILENAME, 1)
|
||||||
|
zf.get_output_stream('myFile') { |os| os.write 'myFile contains just this' }
|
||||||
|
zf.mkdir('dir1')
|
||||||
|
zf.comment = comment
|
||||||
|
zf.close
|
||||||
|
|
||||||
|
zfRead = ::Zip::File.new(EMPTY_FILENAME)
|
||||||
|
assert_equal(comment, zfRead.comment)
|
||||||
|
assert_equal(2, zfRead.entries.length)
|
||||||
|
end
|
||||||
|
|
||||||
def test_get_output_stream
|
def test_get_output_stream
|
||||||
entryCount = nil
|
entryCount = nil
|
||||||
::Zip::File.open(TEST_ZIP.zip_name) do |zf|
|
::Zip::File.open(TEST_ZIP.zip_name) do |zf|
|
||||||
|
|
Loading…
Reference in New Issue