Moved String additions from filearchive.rb to zip.rb (and moved tests along too to ziptest.rb). Added ZipEntry.parentAsString and ZipEntrySet.parent
This commit is contained in:
parent
2668152853
commit
4580ff889d
|
@ -3,17 +3,6 @@
|
||||||
require 'ftools'
|
require 'ftools'
|
||||||
require 'zip'
|
require 'zip'
|
||||||
|
|
||||||
class String
|
|
||||||
def endsWith(aString)
|
|
||||||
aStringSize = aString.size
|
|
||||||
slice(-aStringSize, aStringSize) == aString
|
|
||||||
end
|
|
||||||
|
|
||||||
def ensureEnd(aString)
|
|
||||||
endsWith(aString) ? self : self + aString
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
module Glob
|
module Glob
|
||||||
|
|
|
@ -6,24 +6,6 @@ require 'rubyunit'
|
||||||
require 'filearchive'
|
require 'filearchive'
|
||||||
require 'fileutils'
|
require 'fileutils'
|
||||||
|
|
||||||
class StringExtensionsTest < RUNIT::TestCase
|
|
||||||
def test_endsWith
|
|
||||||
assert("hello".endsWith("o"))
|
|
||||||
assert("hello".endsWith("lo"))
|
|
||||||
assert("hello".endsWith("hello"))
|
|
||||||
assert(!"howdy".endsWith("o"))
|
|
||||||
assert(!"howdy".endsWith("oy"))
|
|
||||||
assert(!"howdy".endsWith("howdy doody"))
|
|
||||||
assert(!"howdy".endsWith("doody howdy"))
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_ensureEnd
|
|
||||||
assert_equals("hello!", "hello!".ensureEnd("!"))
|
|
||||||
assert_equals("hello!", "hello!".ensureEnd("o!"))
|
|
||||||
assert_equals("hello!", "hello".ensureEnd("!"))
|
|
||||||
assert_equals("hello!", "hel".ensureEnd("lo!"))
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
class GlobTest < RUNIT::TestCase
|
class GlobTest < RUNIT::TestCase
|
||||||
|
|
||||||
|
|
35
zip.rb
35
zip.rb
|
@ -16,6 +16,22 @@ unless Enumerable.instance_methods.include?("inject")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class String
|
||||||
|
def startsWith(aString)
|
||||||
|
slice(0, aString.size) == aString
|
||||||
|
end
|
||||||
|
|
||||||
|
def endsWith(aString)
|
||||||
|
aStringSize = aString.size
|
||||||
|
slice(-aStringSize, aStringSize) == aString
|
||||||
|
end
|
||||||
|
|
||||||
|
def ensureEnd(aString)
|
||||||
|
endsWith(aString) ? self : self + aString
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
class Time
|
class Time
|
||||||
|
|
||||||
#MS-DOS File Date and Time format as used in Interrupt 21H Function 57H:
|
#MS-DOS File Date and Time format as used in Interrupt 21H Function 57H:
|
||||||
|
@ -324,6 +340,9 @@ module Zip
|
||||||
compressedSize = 0, crc = 0,
|
compressedSize = 0, crc = 0,
|
||||||
compressionMethod = ZipEntry::DEFLATED, size = 0,
|
compressionMethod = ZipEntry::DEFLATED, size = 0,
|
||||||
time = Time.now)
|
time = Time.now)
|
||||||
|
if name.startsWith("/")
|
||||||
|
raise ZipEntryNameError, "Illegal ZipEntry name '#{name}', name must not start with /"
|
||||||
|
end
|
||||||
@localHeaderOffset = 0
|
@localHeaderOffset = 0
|
||||||
@zipfile, @comment, @compressedSize, @crc, @extra, @compressionMethod,
|
@zipfile, @comment, @compressedSize, @crc, @extra, @compressionMethod,
|
||||||
@name, @size = zipfile, comment, compressedSize, crc,
|
@name, @size = zipfile, comment, compressedSize, crc,
|
||||||
|
@ -553,6 +572,11 @@ module Zip
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def parentAsString
|
||||||
|
val = name[/.*(?=[^\/](\/)?)/]
|
||||||
|
val == "" ? nil : val
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
def getRawInputStream(&aProc)
|
def getRawInputStream(&aProc)
|
||||||
File.open(@zipfile, "rb", &aProc)
|
File.open(@zipfile, "rb", &aProc)
|
||||||
|
@ -762,13 +786,13 @@ module Zip
|
||||||
return @entrySet == other.entrySet
|
return @entrySet == other.entrySet
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def parent(entry)
|
||||||
|
@entrySet[entry.parentAsString]
|
||||||
|
end
|
||||||
|
|
||||||
|
#TODO attr_accessor :autoCreateDirectories
|
||||||
protected
|
protected
|
||||||
attr_accessor :entrySet
|
attr_accessor :entrySet
|
||||||
|
|
||||||
# attr_accessor :autoCreateDirectories
|
|
||||||
|
|
||||||
# def parent(entry)
|
|
||||||
# end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
@ -886,6 +910,7 @@ module Zip
|
||||||
class ZipEntryExistsError < ZipError; end
|
class ZipEntryExistsError < ZipError; end
|
||||||
class ZipDestinationFileExistsError < ZipError; end
|
class ZipDestinationFileExistsError < ZipError; end
|
||||||
class ZipCompressionMethodError < ZipError; end
|
class ZipCompressionMethodError < ZipError; end
|
||||||
|
class ZipEntryNameError < ZipError; end
|
||||||
|
|
||||||
class ZipFile < ZipCentralDirectory
|
class ZipFile < ZipCentralDirectory
|
||||||
CREATE = 1
|
CREATE = 1
|
||||||
|
|
70
ziptest.rb
70
ziptest.rb
|
@ -7,6 +7,37 @@ require 'zip'
|
||||||
|
|
||||||
include Zip
|
include Zip
|
||||||
|
|
||||||
|
class StringExtensionsTest < RUNIT::TestCase
|
||||||
|
|
||||||
|
def test_startsWith
|
||||||
|
assert("hello".startsWith(""))
|
||||||
|
assert("hello".startsWith("h"))
|
||||||
|
assert("hello".startsWith("he"))
|
||||||
|
assert(! "hello".startsWith("hello there"))
|
||||||
|
assert(! "hello".startsWith(" he"))
|
||||||
|
assert_exception(NameError, "undefined method 'size' for nil") {
|
||||||
|
"hello".startsWith(nil)
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_endsWith
|
||||||
|
assert("hello".endsWith("o"))
|
||||||
|
assert("hello".endsWith("lo"))
|
||||||
|
assert("hello".endsWith("hello"))
|
||||||
|
assert(!"howdy".endsWith("o"))
|
||||||
|
assert(!"howdy".endsWith("oy"))
|
||||||
|
assert(!"howdy".endsWith("howdy doody"))
|
||||||
|
assert(!"howdy".endsWith("doody howdy"))
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_ensureEnd
|
||||||
|
assert_equals("hello!", "hello!".ensureEnd("!"))
|
||||||
|
assert_equals("hello!", "hello!".ensureEnd("o!"))
|
||||||
|
assert_equals("hello!", "hello".ensureEnd("!"))
|
||||||
|
assert_equals("hello!", "hel".ensureEnd("lo!"))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
class AbstractInputStreamTest < RUNIT::TestCase
|
class AbstractInputStreamTest < RUNIT::TestCase
|
||||||
# AbstractInputStream subclass that provides a read method
|
# AbstractInputStream subclass that provides a read method
|
||||||
|
|
||||||
|
@ -184,6 +215,26 @@ class ZipEntryTest < RUNIT::TestCase
|
||||||
assert_equals("4", entries[4].to_s)
|
assert_equals("4", entries[4].to_s)
|
||||||
assert_equals("5", entries[5].to_s)
|
assert_equals("5", entries[5].to_s)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def testParentAsString
|
||||||
|
entry1 = ZipEntry.new("zf.zip", "a")
|
||||||
|
entry2 = ZipEntry.new("zf.zip", "a/")
|
||||||
|
entry3 = ZipEntry.new("zf.zip", "a/b")
|
||||||
|
entry4 = ZipEntry.new("zf.zip", "a/b/")
|
||||||
|
entry5 = ZipEntry.new("zf.zip", "a/b/c")
|
||||||
|
entry6 = ZipEntry.new("zf.zip", "a/b/c/")
|
||||||
|
|
||||||
|
assert_equals(nil, entry1.parentAsString)
|
||||||
|
assert_equals(nil, entry2.parentAsString)
|
||||||
|
assert_equals("a/", entry3.parentAsString)
|
||||||
|
assert_equals("a/", entry4.parentAsString)
|
||||||
|
assert_equals("a/b/", entry5.parentAsString)
|
||||||
|
assert_equals("a/b/", entry6.parentAsString)
|
||||||
|
end
|
||||||
|
|
||||||
|
def testEntryNameCannotStartWithSlash
|
||||||
|
assert_exception(ZipEntryNameError) { ZipEntry.new("zf.zip", "/hej/der") }
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
module IOizeString
|
module IOizeString
|
||||||
|
@ -1021,6 +1072,25 @@ class ZipEntrySetTest < RUNIT::TestCase
|
||||||
copy.entries[0].name = "a totally different name"
|
copy.entries[0].name = "a totally different name"
|
||||||
assert(@zipEntrySet != copy)
|
assert(@zipEntrySet != copy)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def testParent
|
||||||
|
entries = [
|
||||||
|
ZipEntry.new("zf.zip", "a"),
|
||||||
|
ZipEntry.new("zf.zip", "a/"),
|
||||||
|
ZipEntry.new("zf.zip", "a/b"),
|
||||||
|
ZipEntry.new("zf.zip", "a/b/"),
|
||||||
|
ZipEntry.new("zf.zip", "a/b/c"),
|
||||||
|
ZipEntry.new("zf.zip", "a/b/c/")
|
||||||
|
]
|
||||||
|
entrySet = ZipEntrySet.new(entries)
|
||||||
|
|
||||||
|
assert_equals(nil, entrySet.parent(entries[0]))
|
||||||
|
assert_equals(nil, entrySet.parent(entries[1]))
|
||||||
|
assert_equals(entries[1], entrySet.parent(entries[2]))
|
||||||
|
assert_equals(entries[1], entrySet.parent(entries[3]))
|
||||||
|
assert_equals(entries[3], entrySet.parent(entries[4]))
|
||||||
|
assert_equals(entries[3], entrySet.parent(entries[5]))
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue