rubyzip 0.9.4 → 0.9.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of rubyzip might be problematic. Click here for more details.

Files changed (45) hide show
  1. data/{README → README.md} +16 -17
  2. data/Rakefile +6 -104
  3. data/lib/zip/compressor.rb +10 -0
  4. data/lib/zip/constants.rb +10 -0
  5. data/lib/zip/decompressor.rb +13 -0
  6. data/lib/zip/deflater.rb +30 -0
  7. data/lib/zip/inflater.rb +65 -0
  8. data/lib/zip/ioextras.rb +35 -36
  9. data/lib/zip/null_compressor.rb +15 -0
  10. data/lib/zip/null_decompressor.rb +25 -0
  11. data/lib/zip/null_input_stream.rb +9 -0
  12. data/lib/zip/pass_thru_compressor.rb +23 -0
  13. data/lib/zip/pass_thru_decompressor.rb +40 -0
  14. data/lib/zip/stdrubyext.rb +10 -44
  15. data/lib/zip/zip.rb +22 -1848
  16. data/lib/zip/zip_central_directory.rb +139 -0
  17. data/lib/zip/zip_entry.rb +639 -0
  18. data/lib/zip/zip_entry_set.rb +66 -0
  19. data/lib/zip/zip_extra_field.rb +213 -0
  20. data/lib/zip/zip_file.rb +318 -0
  21. data/lib/zip/zip_input_stream.rb +134 -0
  22. data/lib/zip/zip_output_stream.rb +172 -0
  23. data/lib/zip/zip_streamable_directory.rb +15 -0
  24. data/lib/zip/zip_streamable_stream.rb +47 -0
  25. data/lib/zip/zipfilesystem.rb +90 -88
  26. data/samples/example_recursive.rb +49 -0
  27. metadata +54 -60
  28. data/ChangeLog +0 -1146
  29. data/install.rb +0 -23
  30. data/lib/zip/ziprequire.rb +0 -90
  31. data/test/alltests.rb +0 -9
  32. data/test/data/file1.txt +0 -46
  33. data/test/data/file1.txt.deflatedData +0 -0
  34. data/test/data/file2.txt +0 -1504
  35. data/test/data/notzippedruby.rb +0 -7
  36. data/test/data/rubycode.zip +0 -0
  37. data/test/data/rubycode2.zip +0 -0
  38. data/test/data/testDirectory.bin +0 -0
  39. data/test/data/zipWithDirs.zip +0 -0
  40. data/test/gentestfiles.rb +0 -157
  41. data/test/ioextrastest.rb +0 -208
  42. data/test/stdrubyexttest.rb +0 -52
  43. data/test/zipfilesystemtest.rb +0 -841
  44. data/test/ziprequiretest.rb +0 -43
  45. data/test/ziptest.rb +0 -1620
@@ -1,7 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- class NotZippedRuby
4
- def returnTrue
5
- true
6
- end
7
- end
Binary file
Binary file
Binary file
Binary file
@@ -1,157 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- $VERBOSE = true
4
-
5
- class TestFiles
6
- RANDOM_ASCII_FILE1 = "data/generated/randomAscii1.txt"
7
- RANDOM_ASCII_FILE2 = "data/generated/randomAscii2.txt"
8
- RANDOM_ASCII_FILE3 = "data/generated/randomAscii3.txt"
9
- RANDOM_BINARY_FILE1 = "data/generated/randomBinary1.bin"
10
- RANDOM_BINARY_FILE2 = "data/generated/randomBinary2.bin"
11
-
12
- EMPTY_TEST_DIR = "data/generated/emptytestdir"
13
-
14
- ASCII_TEST_FILES = [ RANDOM_ASCII_FILE1, RANDOM_ASCII_FILE2, RANDOM_ASCII_FILE3 ]
15
- BINARY_TEST_FILES = [ RANDOM_BINARY_FILE1, RANDOM_BINARY_FILE2 ]
16
- TEST_DIRECTORIES = [ EMPTY_TEST_DIR ]
17
- TEST_FILES = [ ASCII_TEST_FILES, BINARY_TEST_FILES, EMPTY_TEST_DIR ].flatten!
18
-
19
- def TestFiles.create_test_files(recreate)
20
- if (recreate ||
21
- ! (TEST_FILES.inject(true) { |accum, element| accum && File.exists?(element) }))
22
-
23
- Dir.mkdir "data/generated" rescue Errno::EEXIST
24
-
25
- ASCII_TEST_FILES.each_with_index {
26
- |filename, index|
27
- create_random_ascii(filename, 1E4 * (index+1))
28
- }
29
-
30
- BINARY_TEST_FILES.each_with_index {
31
- |filename, index|
32
- create_random_binary(filename, 1E4 * (index+1))
33
- }
34
-
35
- ensure_dir(EMPTY_TEST_DIR)
36
- end
37
- end
38
-
39
- private
40
- def TestFiles.create_random_ascii(filename, size)
41
- File.open(filename, "wb") {
42
- |file|
43
- while (file.tell < size)
44
- file << rand
45
- end
46
- }
47
- end
48
-
49
- def TestFiles.create_random_binary(filename, size)
50
- File.open(filename, "wb") {
51
- |file|
52
- while (file.tell < size)
53
- file << [rand].pack("V")
54
- end
55
- }
56
- end
57
-
58
- def TestFiles.ensure_dir(name)
59
- if File.exists?(name)
60
- return if File.stat(name).directory?
61
- File.delete(name)
62
- end
63
- Dir.mkdir(name)
64
- end
65
-
66
- end
67
-
68
-
69
-
70
- # For representation and creation of
71
- # test data
72
- class TestZipFile
73
- attr_accessor :zip_name, :entry_names, :comment
74
-
75
- def initialize(zip_name, entry_names, comment = "")
76
- @zip_name=zip_name
77
- @entry_names=entry_names
78
- @comment = comment
79
- end
80
-
81
- def TestZipFile.create_test_zips(recreate)
82
- files = Dir.entries("data/generated")
83
- if (recreate ||
84
- ! (files.index(File.basename(TEST_ZIP1.zip_name)) &&
85
- files.index(File.basename(TEST_ZIP2.zip_name)) &&
86
- files.index(File.basename(TEST_ZIP3.zip_name)) &&
87
- files.index(File.basename(TEST_ZIP4.zip_name)) &&
88
- files.index("empty.txt") &&
89
- files.index("empty_chmod640.txt") &&
90
- files.index("short.txt") &&
91
- files.index("longAscii.txt") &&
92
- files.index("longBinary.bin") ))
93
- raise "failed to create test zip '#{TEST_ZIP1.zip_name}'" unless
94
- system("zip #{TEST_ZIP1.zip_name} data/file2.txt")
95
- raise "failed to remove entry from '#{TEST_ZIP1.zip_name}'" unless
96
- system("zip #{TEST_ZIP1.zip_name} -d data/file2.txt")
97
-
98
- File.open("data/generated/empty.txt", "w") {}
99
- File.open("data/generated/empty_chmod640.txt", "w") { |f| f.chmod(0640) }
100
-
101
- File.open("data/generated/short.txt", "w") { |file| file << "ABCDEF" }
102
- ziptestTxt=""
103
- File.open("data/file2.txt") { |file| ziptestTxt=file.read }
104
- File.open("data/generated/longAscii.txt", "w") {
105
- |file|
106
- while (file.tell < 1E5)
107
- file << ziptestTxt
108
- end
109
- }
110
-
111
- testBinaryPattern=""
112
- File.open("data/generated/empty.zip") { |file| testBinaryPattern=file.read }
113
- testBinaryPattern *= 4
114
-
115
- File.open("data/generated/longBinary.bin", "wb") {
116
- |file|
117
- while (file.tell < 3E5)
118
- file << testBinaryPattern << rand << "\0"
119
- end
120
- }
121
- raise "failed to create test zip '#{TEST_ZIP2.zip_name}'" unless
122
- system("zip #{TEST_ZIP2.zip_name} #{TEST_ZIP2.entry_names.join(' ')}")
123
-
124
- # without bash system interprets everything after echo as parameters to
125
- # echo including | zip -z ...
126
- raise "failed to add comment to test zip '#{TEST_ZIP2.zip_name}'" unless
127
- system("bash -c \"echo #{TEST_ZIP2.comment} | zip -z #{TEST_ZIP2.zip_name}\"")
128
-
129
- raise "failed to create test zip '#{TEST_ZIP3.zip_name}'" unless
130
- system("zip #{TEST_ZIP3.zip_name} #{TEST_ZIP3.entry_names.join(' ')}")
131
-
132
- raise "failed to create test zip '#{TEST_ZIP4.zip_name}'" unless
133
- system("zip #{TEST_ZIP4.zip_name} #{TEST_ZIP4.entry_names.join(' ')}")
134
- end
135
- rescue
136
- raise $!.to_s +
137
- "\n\nziptest.rb requires the Info-ZIP program 'zip' in the path\n" +
138
- "to create test data. If you don't have it you can download\n" +
139
- "the necessary test files at http://sf.net/projects/rubyzip."
140
- end
141
-
142
- TEST_ZIP1 = TestZipFile.new("data/generated/empty.zip", [])
143
- TEST_ZIP2 = TestZipFile.new("data/generated/5entry.zip", %w{ data/generated/longAscii.txt data/generated/empty.txt data/generated/empty_chmod640.txt data/generated/short.txt data/generated/longBinary.bin},
144
- "my zip comment")
145
- TEST_ZIP3 = TestZipFile.new("data/generated/test1.zip", %w{ data/file1.txt })
146
- TEST_ZIP4 = TestZipFile.new("data/generated/zipWithDir.zip", [ "data/file1.txt",
147
- TestFiles::EMPTY_TEST_DIR])
148
- end
149
-
150
-
151
- END {
152
- TestFiles::create_test_files(ARGV.index("recreate") != nil ||
153
- ARGV.index("recreateonly") != nil)
154
- TestZipFile::create_test_zips(ARGV.index("recreate") != nil ||
155
- ARGV.index("recreateonly") != nil)
156
- exit if ARGV.index("recreateonly") != nil
157
- }
@@ -1,208 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- $VERBOSE = true
4
-
5
- $: << "../lib"
6
-
7
- require 'test/unit'
8
- require 'zip/ioextras'
9
-
10
- include IOExtras
11
-
12
- class FakeIOTest < Test::Unit::TestCase
13
- class FakeIOUsingClass
14
- include FakeIO
15
- end
16
-
17
- def test_kind_of?
18
- obj = FakeIOUsingClass.new
19
-
20
- assert(obj.kind_of?(Object))
21
- assert(obj.kind_of?(FakeIOUsingClass))
22
- assert(obj.kind_of?(IO))
23
- assert(!obj.kind_of?(Fixnum))
24
- assert(!obj.kind_of?(String))
25
- end
26
- end
27
-
28
- class AbstractInputStreamTest < Test::Unit::TestCase
29
- # AbstractInputStream subclass that provides a read method
30
-
31
- TEST_LINES = [ "Hello world#{$/}",
32
- "this is the second line#{$/}",
33
- "this is the last line"]
34
- TEST_STRING = TEST_LINES.join
35
- class TestAbstractInputStream
36
- include AbstractInputStream
37
- def initialize(aString)
38
- super()
39
- @contents = aString
40
- @readPointer = 0
41
- end
42
-
43
- def read(charsToRead)
44
- retVal=@contents[@readPointer, charsToRead]
45
- @readPointer+=charsToRead
46
- return retVal
47
- end
48
-
49
- def produce_input
50
- read(100)
51
- end
52
-
53
- def input_finished?
54
- @contents[@readPointer] == nil
55
- end
56
- end
57
-
58
- def setup
59
- @io = TestAbstractInputStream.new(TEST_STRING)
60
- end
61
-
62
- def test_gets
63
- assert_equal(TEST_LINES[0], @io.gets)
64
- assert_equal(1, @io.lineno)
65
- assert_equal(TEST_LINES[1], @io.gets)
66
- assert_equal(2, @io.lineno)
67
- assert_equal(TEST_LINES[2], @io.gets)
68
- assert_equal(3, @io.lineno)
69
- assert_equal(nil, @io.gets)
70
- assert_equal(4, @io.lineno)
71
- end
72
-
73
- def test_getsMultiCharSeperator
74
- assert_equal("Hell", @io.gets("ll"))
75
- assert_equal("o world#{$/}this is the second l", @io.gets("d l"))
76
- end
77
-
78
- def test_each_line
79
- lineNumber=0
80
- @io.each_line {
81
- |line|
82
- assert_equal(TEST_LINES[lineNumber], line)
83
- lineNumber+=1
84
- }
85
- end
86
-
87
- def test_readlines
88
- assert_equal(TEST_LINES, @io.readlines)
89
- end
90
-
91
- def test_readline
92
- test_gets
93
- begin
94
- @io.readline
95
- fail "EOFError expected"
96
- rescue EOFError
97
- end
98
- end
99
- end
100
-
101
- class AbstractOutputStreamTest < Test::Unit::TestCase
102
- class TestOutputStream
103
- include AbstractOutputStream
104
-
105
- attr_accessor :buffer
106
-
107
- def initialize
108
- @buffer = ""
109
- end
110
-
111
- def << (data)
112
- @buffer << data
113
- self
114
- end
115
- end
116
-
117
- def setup
118
- @outputStream = TestOutputStream.new
119
-
120
- @origCommaSep = $,
121
- @origOutputSep = $\
122
- end
123
-
124
- def teardown
125
- $, = @origCommaSep
126
- $\ = @origOutputSep
127
- end
128
-
129
- def test_write
130
- count = @outputStream.write("a little string")
131
- assert_equal("a little string", @outputStream.buffer)
132
- assert_equal("a little string".length, count)
133
-
134
- count = @outputStream.write(". a little more")
135
- assert_equal("a little string. a little more", @outputStream.buffer)
136
- assert_equal(". a little more".length, count)
137
- end
138
-
139
- def test_print
140
- $\ = nil # record separator set to nil
141
- @outputStream.print("hello")
142
- assert_equal("hello", @outputStream.buffer)
143
-
144
- @outputStream.print(" world.")
145
- assert_equal("hello world.", @outputStream.buffer)
146
-
147
- @outputStream.print(" You ok ", "out ", "there?")
148
- assert_equal("hello world. You ok out there?", @outputStream.buffer)
149
-
150
- $\ = "\n"
151
- @outputStream.print
152
- assert_equal("hello world. You ok out there?\n", @outputStream.buffer)
153
-
154
- @outputStream.print("I sure hope so!")
155
- assert_equal("hello world. You ok out there?\nI sure hope so!\n", @outputStream.buffer)
156
-
157
- $, = "X"
158
- @outputStream.buffer = ""
159
- @outputStream.print("monkey", "duck", "zebra")
160
- assert_equal("monkeyXduckXzebra\n", @outputStream.buffer)
161
-
162
- $\ = nil
163
- @outputStream.buffer = ""
164
- @outputStream.print(20)
165
- assert_equal("20", @outputStream.buffer)
166
- end
167
-
168
- def test_printf
169
- @outputStream.printf("%d %04x", 123, 123)
170
- assert_equal("123 007b", @outputStream.buffer)
171
- end
172
-
173
- def test_putc
174
- @outputStream.putc("A")
175
- assert_equal("A", @outputStream.buffer)
176
- @outputStream.putc(65)
177
- assert_equal("AA", @outputStream.buffer)
178
- end
179
-
180
- def test_puts
181
- @outputStream.puts
182
- assert_equal("\n", @outputStream.buffer)
183
-
184
- @outputStream.puts("hello", "world")
185
- assert_equal("\nhello\nworld\n", @outputStream.buffer)
186
-
187
- @outputStream.buffer = ""
188
- @outputStream.puts("hello\n", "world\n")
189
- assert_equal("hello\nworld\n", @outputStream.buffer)
190
-
191
- @outputStream.buffer = ""
192
- @outputStream.puts(["hello\n", "world\n"])
193
- assert_equal("hello\nworld\n", @outputStream.buffer)
194
-
195
- @outputStream.buffer = ""
196
- @outputStream.puts(["hello\n", "world\n"], "bingo")
197
- assert_equal("hello\nworld\nbingo\n", @outputStream.buffer)
198
-
199
- @outputStream.buffer = ""
200
- @outputStream.puts(16, 20, 50, "hello")
201
- assert_equal("16\n20\n50\nhello\n", @outputStream.buffer)
202
- end
203
- end
204
-
205
-
206
- # Copyright (C) 2002-2004 Thomas Sondergaard
207
- # rubyzip is free software; you can redistribute it and/or
208
- # modify it under the terms of the ruby license.
@@ -1,52 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- $VERBOSE = true
4
-
5
- $: << "../lib"
6
-
7
- require 'test/unit'
8
- require 'zip/stdrubyext'
9
-
10
- class ModuleTest < Test::Unit::TestCase
11
-
12
- def test_select_map
13
- assert_equal([2, 4, 8, 10], [1, 2, 3, 4, 5].select_map { |e| e == 3 ? nil : 2*e })
14
- end
15
-
16
- end
17
-
18
- class StringExtensionsTest < Test::Unit::TestCase
19
-
20
- def test_starts_with
21
- assert("hello".starts_with(""))
22
- assert("hello".starts_with("h"))
23
- assert("hello".starts_with("he"))
24
- assert(! "hello".starts_with("hello there"))
25
- assert(! "hello".starts_with(" he"))
26
-
27
- assert_raise(TypeError, "type mismatch: NilClass given") {
28
- "hello".starts_with(nil)
29
- }
30
- end
31
-
32
- def test_ends_with
33
- assert("hello".ends_with("o"))
34
- assert("hello".ends_with("lo"))
35
- assert("hello".ends_with("hello"))
36
- assert(!"howdy".ends_with("o"))
37
- assert(!"howdy".ends_with("oy"))
38
- assert(!"howdy".ends_with("howdy doody"))
39
- assert(!"howdy".ends_with("doody howdy"))
40
- end
41
-
42
- def test_ensure_end
43
- assert_equal("hello!", "hello!".ensure_end("!"))
44
- assert_equal("hello!", "hello!".ensure_end("o!"))
45
- assert_equal("hello!", "hello".ensure_end("!"))
46
- assert_equal("hello!", "hel".ensure_end("lo!"))
47
- end
48
- end
49
-
50
- # Copyright (C) 2002, 2003 Thomas Sondergaard
51
- # rubyzip is free software; you can redistribute it and/or
52
- # modify it under the terms of the ruby license.
@@ -1,841 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- $VERBOSE = true
4
-
5
- $: << "../lib"
6
-
7
- require 'zip/zipfilesystem'
8
- require 'test/unit'
9
- require 'fileutils'
10
-
11
- module ExtraAssertions
12
-
13
- def assert_forwarded(anObject, method, retVal, *expectedArgs)
14
- callArgs = nil
15
- setCallArgsProc = proc { |args| callArgs = args }
16
- anObject.instance_eval <<-"end_eval"
17
- alias #{method}_org #{method}
18
- def #{method}(*args)
19
- ObjectSpace._id2ref(#{setCallArgsProc.object_id}).call(args)
20
- ObjectSpace._id2ref(#{retVal.object_id})
21
- end
22
- end_eval
23
-
24
- assert_equal(retVal, yield) # Invoke test
25
- assert_equal(expectedArgs, callArgs)
26
- ensure
27
- anObject.instance_eval "undef #{method}; alias #{method} #{method}_org"
28
- end
29
-
30
- end
31
-
32
- include Zip
33
-
34
- class ZipFsFileNonmutatingTest < Test::Unit::TestCase
35
- def setup
36
- @zipFile = ZipFile.new("data/zipWithDirs.zip")
37
- end
38
-
39
- def teardown
40
- @zipFile.close if @zipFile
41
- end
42
-
43
- def test_umask
44
- assert_equal(File.umask, @zipFile.file.umask)
45
- @zipFile.file.umask(0006)
46
- end
47
-
48
- def test_exists?
49
- assert(! @zipFile.file.exists?("notAFile"))
50
- assert(@zipFile.file.exists?("file1"))
51
- assert(@zipFile.file.exists?("dir1"))
52
- assert(@zipFile.file.exists?("dir1/"))
53
- assert(@zipFile.file.exists?("dir1/file12"))
54
- assert(@zipFile.file.exist?("dir1/file12")) # notice, tests exist? alias of exists? !
55
-
56
- @zipFile.dir.chdir "dir1/"
57
- assert(!@zipFile.file.exists?("file1"))
58
- assert(@zipFile.file.exists?("file12"))
59
- end
60
-
61
- def test_open_read
62
- blockCalled = false
63
- @zipFile.file.open("file1", "r") {
64
- |f|
65
- blockCalled = true
66
- assert_equal("this is the entry 'file1' in my test archive!",
67
- f.readline.chomp)
68
- }
69
- assert(blockCalled)
70
-
71
- blockCalled = false
72
- @zipFile.file.open("file1", "rb") { # test binary flag is ignored
73
- |f|
74
- blockCalled = true
75
- assert_equal("this is the entry 'file1' in my test archive!",
76
- f.readline.chomp)
77
- }
78
- assert(blockCalled)
79
-
80
- blockCalled = false
81
- @zipFile.dir.chdir "dir2"
82
- @zipFile.file.open("file21", "r") {
83
- |f|
84
- blockCalled = true
85
- assert_equal("this is the entry 'dir2/file21' in my test archive!",
86
- f.readline.chomp)
87
- }
88
- assert(blockCalled)
89
- @zipFile.dir.chdir "/"
90
-
91
- assert_raise(Errno::ENOENT) {
92
- @zipFile.file.open("noSuchEntry")
93
- }
94
-
95
- begin
96
- is = @zipFile.file.open("file1")
97
- assert_equal("this is the entry 'file1' in my test archive!",
98
- is.readline.chomp)
99
- ensure
100
- is.close if is
101
- end
102
- end
103
-
104
- def test_new
105
- begin
106
- is = @zipFile.file.new("file1")
107
- assert_equal("this is the entry 'file1' in my test archive!",
108
- is.readline.chomp)
109
- ensure
110
- is.close if is
111
- end
112
- begin
113
- is = @zipFile.file.new("file1") {
114
- fail "should not call block"
115
- }
116
- ensure
117
- is.close if is
118
- end
119
- end
120
-
121
- def test_symlink
122
- assert_raise(NotImplementedError) {
123
- @zipFile.file.symlink("file1", "aSymlink")
124
- }
125
- end
126
-
127
- def test_size
128
- assert_raise(Errno::ENOENT) { @zipFile.file.size("notAFile") }
129
- assert_equal(72, @zipFile.file.size("file1"))
130
- assert_equal(0, @zipFile.file.size("dir2/dir21"))
131
-
132
- assert_equal(72, @zipFile.file.stat("file1").size)
133
- assert_equal(0, @zipFile.file.stat("dir2/dir21").size)
134
- end
135
-
136
- def test_size?
137
- assert_equal(nil, @zipFile.file.size?("notAFile"))
138
- assert_equal(72, @zipFile.file.size?("file1"))
139
- assert_equal(nil, @zipFile.file.size?("dir2/dir21"))
140
-
141
- assert_equal(72, @zipFile.file.stat("file1").size?)
142
- assert_equal(nil, @zipFile.file.stat("dir2/dir21").size?)
143
- end
144
-
145
-
146
- def test_file?
147
- assert(@zipFile.file.file?("file1"))
148
- assert(@zipFile.file.file?("dir2/file21"))
149
- assert(! @zipFile.file.file?("dir1"))
150
- assert(! @zipFile.file.file?("dir1/dir11"))
151
-
152
- assert(@zipFile.file.stat("file1").file?)
153
- assert(@zipFile.file.stat("dir2/file21").file?)
154
- assert(! @zipFile.file.stat("dir1").file?)
155
- assert(! @zipFile.file.stat("dir1/dir11").file?)
156
- end
157
-
158
- include ExtraAssertions
159
-
160
- def test_dirname
161
- assert_forwarded(File, :dirname, "retVal", "a/b/c/d") {
162
- @zipFile.file.dirname("a/b/c/d")
163
- }
164
- end
165
-
166
- def test_basename
167
- assert_forwarded(File, :basename, "retVal", "a/b/c/d") {
168
- @zipFile.file.basename("a/b/c/d")
169
- }
170
- end
171
-
172
- def test_split
173
- assert_forwarded(File, :split, "retVal", "a/b/c/d") {
174
- @zipFile.file.split("a/b/c/d")
175
- }
176
- end
177
-
178
- def test_join
179
- assert_equal("a/b/c", @zipFile.file.join("a/b", "c"))
180
- assert_equal("a/b/c/d", @zipFile.file.join("a/b", "c/d"))
181
- assert_equal("/c/d", @zipFile.file.join("", "c/d"))
182
- assert_equal("a/b/c/d", @zipFile.file.join("a", "b", "c", "d"))
183
- end
184
-
185
- def test_utime
186
- t_now = Time.now
187
- t_bak = @zipFile.file.mtime("file1")
188
- @zipFile.file.utime(t_now, "file1")
189
- assert_equal(t_now, @zipFile.file.mtime("file1"))
190
- @zipFile.file.utime(t_bak, "file1")
191
- assert_equal(t_bak, @zipFile.file.mtime("file1"))
192
- end
193
-
194
-
195
- def assert_always_false(operation)
196
- assert(! @zipFile.file.send(operation, "noSuchFile"))
197
- assert(! @zipFile.file.send(operation, "file1"))
198
- assert(! @zipFile.file.send(operation, "dir1"))
199
- assert(! @zipFile.file.stat("file1").send(operation))
200
- assert(! @zipFile.file.stat("dir1").send(operation))
201
- end
202
-
203
- def assert_true_if_entry_exists(operation)
204
- assert(! @zipFile.file.send(operation, "noSuchFile"))
205
- assert(@zipFile.file.send(operation, "file1"))
206
- assert(@zipFile.file.send(operation, "dir1"))
207
- assert(@zipFile.file.stat("file1").send(operation))
208
- assert(@zipFile.file.stat("dir1").send(operation))
209
- end
210
-
211
- def test_pipe?
212
- assert_always_false(:pipe?)
213
- end
214
-
215
- def test_blockdev?
216
- assert_always_false(:blockdev?)
217
- end
218
-
219
- def test_symlink?
220
- assert_always_false(:symlink?)
221
- end
222
-
223
- def test_socket?
224
- assert_always_false(:socket?)
225
- end
226
-
227
- def test_chardev?
228
- assert_always_false(:chardev?)
229
- end
230
-
231
- def test_truncate
232
- assert_raise(StandardError, "truncate not supported") {
233
- @zipFile.file.truncate("file1", 100)
234
- }
235
- end
236
-
237
- def assert_e_n_o_e_n_t(operation, args = ["NoSuchFile"])
238
- assert_raise(Errno::ENOENT) {
239
- @zipFile.file.send(operation, *args)
240
- }
241
- end
242
-
243
- def test_ftype
244
- assert_e_n_o_e_n_t(:ftype)
245
- assert_equal("file", @zipFile.file.ftype("file1"))
246
- assert_equal("directory", @zipFile.file.ftype("dir1/dir11"))
247
- assert_equal("directory", @zipFile.file.ftype("dir1/dir11/"))
248
- end
249
-
250
- def test_link
251
- assert_raise(NotImplementedError) {
252
- @zipFile.file.link("file1", "someOtherString")
253
- }
254
- end
255
-
256
- def test_directory?
257
- assert(! @zipFile.file.directory?("notAFile"))
258
- assert(! @zipFile.file.directory?("file1"))
259
- assert(! @zipFile.file.directory?("dir1/file11"))
260
- assert(@zipFile.file.directory?("dir1"))
261
- assert(@zipFile.file.directory?("dir1/"))
262
- assert(@zipFile.file.directory?("dir2/dir21"))
263
-
264
- assert(! @zipFile.file.stat("file1").directory?)
265
- assert(! @zipFile.file.stat("dir1/file11").directory?)
266
- assert(@zipFile.file.stat("dir1").directory?)
267
- assert(@zipFile.file.stat("dir1/").directory?)
268
- assert(@zipFile.file.stat("dir2/dir21").directory?)
269
- end
270
-
271
- def test_chown
272
- assert_equal(2, @zipFile.file.chown(1,2, "dir1", "file1"))
273
- assert_equal(1, @zipFile.file.stat("dir1").uid)
274
- assert_equal(2, @zipFile.file.stat("dir1").gid)
275
- assert_equal(2, @zipFile.file.chown(nil, nil, "dir1", "file1"))
276
- end
277
-
278
- def test_zero?
279
- assert(! @zipFile.file.zero?("notAFile"))
280
- assert(! @zipFile.file.zero?("file1"))
281
- assert(@zipFile.file.zero?("dir1"))
282
- blockCalled = false
283
- ZipFile.open("data/generated/5entry.zip") {
284
- |zf|
285
- blockCalled = true
286
- assert(zf.file.zero?("data/generated/empty.txt"))
287
- }
288
- assert(blockCalled)
289
-
290
- assert(! @zipFile.file.stat("file1").zero?)
291
- assert(@zipFile.file.stat("dir1").zero?)
292
- blockCalled = false
293
- ZipFile.open("data/generated/5entry.zip") {
294
- |zf|
295
- blockCalled = true
296
- assert(zf.file.stat("data/generated/empty.txt").zero?)
297
- }
298
- assert(blockCalled)
299
- end
300
-
301
- def test_expand_path
302
- ZipFile.open("data/zipWithDirs.zip") {
303
- |zf|
304
- assert_equal("/", zf.file.expand_path("."))
305
- zf.dir.chdir "dir1"
306
- assert_equal("/dir1", zf.file.expand_path("."))
307
- assert_equal("/dir1/file12", zf.file.expand_path("file12"))
308
- assert_equal("/", zf.file.expand_path(".."))
309
- assert_equal("/dir2/dir21", zf.file.expand_path("../dir2/dir21"))
310
- }
311
- end
312
-
313
- def test_mtime
314
- assert_equal(Time.at(1027694306),
315
- @zipFile.file.mtime("dir2/file21"))
316
- assert_equal(Time.at(1027690863),
317
- @zipFile.file.mtime("dir2/dir21"))
318
- assert_raise(Errno::ENOENT) {
319
- @zipFile.file.mtime("noSuchEntry")
320
- }
321
-
322
- assert_equal(Time.at(1027694306),
323
- @zipFile.file.stat("dir2/file21").mtime)
324
- assert_equal(Time.at(1027690863),
325
- @zipFile.file.stat("dir2/dir21").mtime)
326
- end
327
-
328
- def test_ctime
329
- assert_nil(@zipFile.file.ctime("file1"))
330
- assert_nil(@zipFile.file.stat("file1").ctime)
331
- end
332
-
333
- def test_atime
334
- assert_nil(@zipFile.file.atime("file1"))
335
- assert_nil(@zipFile.file.stat("file1").atime)
336
- end
337
-
338
- def test_readable?
339
- assert(! @zipFile.file.readable?("noSuchFile"))
340
- assert(@zipFile.file.readable?("file1"))
341
- assert(@zipFile.file.readable?("dir1"))
342
- assert(@zipFile.file.stat("file1").readable?)
343
- assert(@zipFile.file.stat("dir1").readable?)
344
- end
345
-
346
- def test_readable_real?
347
- assert(! @zipFile.file.readable_real?("noSuchFile"))
348
- assert(@zipFile.file.readable_real?("file1"))
349
- assert(@zipFile.file.readable_real?("dir1"))
350
- assert(@zipFile.file.stat("file1").readable_real?)
351
- assert(@zipFile.file.stat("dir1").readable_real?)
352
- end
353
-
354
- def test_writable?
355
- assert(! @zipFile.file.writable?("noSuchFile"))
356
- assert(@zipFile.file.writable?("file1"))
357
- assert(@zipFile.file.writable?("dir1"))
358
- assert(@zipFile.file.stat("file1").writable?)
359
- assert(@zipFile.file.stat("dir1").writable?)
360
- end
361
-
362
- def test_writable_real?
363
- assert(! @zipFile.file.writable_real?("noSuchFile"))
364
- assert(@zipFile.file.writable_real?("file1"))
365
- assert(@zipFile.file.writable_real?("dir1"))
366
- assert(@zipFile.file.stat("file1").writable_real?)
367
- assert(@zipFile.file.stat("dir1").writable_real?)
368
- end
369
-
370
- def test_executable?
371
- assert(! @zipFile.file.executable?("noSuchFile"))
372
- assert(! @zipFile.file.executable?("file1"))
373
- assert(@zipFile.file.executable?("dir1"))
374
- assert(! @zipFile.file.stat("file1").executable?)
375
- assert(@zipFile.file.stat("dir1").executable?)
376
- end
377
-
378
- def test_executable_real?
379
- assert(! @zipFile.file.executable_real?("noSuchFile"))
380
- assert(! @zipFile.file.executable_real?("file1"))
381
- assert(@zipFile.file.executable_real?("dir1"))
382
- assert(! @zipFile.file.stat("file1").executable_real?)
383
- assert(@zipFile.file.stat("dir1").executable_real?)
384
- end
385
-
386
- def test_owned?
387
- assert_true_if_entry_exists(:owned?)
388
- end
389
-
390
- def test_grpowned?
391
- assert_true_if_entry_exists(:grpowned?)
392
- end
393
-
394
- def test_setgid?
395
- assert_always_false(:setgid?)
396
- end
397
-
398
- def test_setuid?
399
- assert_always_false(:setgid?)
400
- end
401
-
402
- def test_sticky?
403
- assert_always_false(:sticky?)
404
- end
405
-
406
- def test_readlink
407
- assert_raise(NotImplementedError) {
408
- @zipFile.file.readlink("someString")
409
- }
410
- end
411
-
412
- def test_stat
413
- s = @zipFile.file.stat("file1")
414
- assert(s.kind_of?(File::Stat)) # It pretends
415
- assert_raise(Errno::ENOENT, "No such file or directory - noSuchFile") {
416
- @zipFile.file.stat("noSuchFile")
417
- }
418
- end
419
-
420
- def test_lstat
421
- assert(@zipFile.file.lstat("file1").file?)
422
- end
423
-
424
-
425
- def test_chmod
426
- assert_raise(Errno::ENOENT, "No such file or directory - noSuchFile") {
427
- @zipFile.file.chmod(0644, "file1", "NoSuchFile")
428
- }
429
- assert_equal(2, @zipFile.file.chmod(0644, "file1", "dir1"))
430
- end
431
-
432
- def test_pipe
433
- assert_raise(NotImplementedError) {
434
- @zipFile.file.pipe
435
- }
436
- end
437
-
438
- def test_foreach
439
- ZipFile.open("data/generated/zipWithDir.zip") {
440
- |zf|
441
- ref = []
442
- File.foreach("data/file1.txt") { |e| ref << e }
443
-
444
- index = 0
445
- zf.file.foreach("data/file1.txt") {
446
- |l|
447
- assert_equal(ref[index], l)
448
- index = index.next
449
- }
450
- assert_equal(ref.size, index)
451
- }
452
-
453
- ZipFile.open("data/generated/zipWithDir.zip") {
454
- |zf|
455
- ref = []
456
- File.foreach("data/file1.txt", " ") { |e| ref << e }
457
-
458
- index = 0
459
- zf.file.foreach("data/file1.txt", " ") {
460
- |l|
461
- assert_equal(ref[index], l)
462
- index = index.next
463
- }
464
- assert_equal(ref.size, index)
465
- }
466
- end
467
-
468
- def test_popen
469
- cmd = /mswin/i =~ RUBY_PLATFORM ? 'dir' : 'ls'
470
-
471
- assert_equal(File.popen(cmd) { |f| f.read },
472
- @zipFile.file.popen(cmd) { |f| f.read })
473
- end
474
-
475
- # Can be added later
476
- # def test_select
477
- # fail "implement test"
478
- # end
479
-
480
- def test_readlines
481
- ZipFile.open("data/generated/zipWithDir.zip") {
482
- |zf|
483
- assert_equal(File.readlines("data/file1.txt"),
484
- zf.file.readlines("data/file1.txt"))
485
- }
486
- end
487
-
488
- def test_read
489
- ZipFile.open("data/generated/zipWithDir.zip") {
490
- |zf|
491
- assert_equal(File.read("data/file1.txt"),
492
- zf.file.read("data/file1.txt"))
493
- }
494
- end
495
-
496
- end
497
-
498
- class ZipFsFileStatTest < Test::Unit::TestCase
499
-
500
- def setup
501
- @zipFile = ZipFile.new("data/zipWithDirs.zip")
502
- end
503
-
504
- def teardown
505
- @zipFile.close if @zipFile
506
- end
507
-
508
- def test_blocks
509
- assert_equal(nil, @zipFile.file.stat("file1").blocks)
510
- end
511
-
512
- def test_ino
513
- assert_equal(0, @zipFile.file.stat("file1").ino)
514
- end
515
-
516
- def test_uid
517
- assert_equal(0, @zipFile.file.stat("file1").uid)
518
- end
519
-
520
- def test_gid
521
- assert_equal(0, @zipFile.file.stat("file1").gid)
522
- end
523
-
524
- def test_ftype
525
- assert_equal("file", @zipFile.file.stat("file1").ftype)
526
- assert_equal("directory", @zipFile.file.stat("dir1").ftype)
527
- end
528
-
529
- def test_mode
530
- assert_equal(0600, @zipFile.file.stat("file1").mode & 0777)
531
- assert_equal(0600, @zipFile.file.stat("file1").mode & 0777)
532
- assert_equal(0755, @zipFile.file.stat("dir1").mode & 0777)
533
- assert_equal(0755, @zipFile.file.stat("dir1").mode & 0777)
534
- end
535
-
536
- def test_dev
537
- assert_equal(0, @zipFile.file.stat("file1").dev)
538
- end
539
-
540
- def test_rdev
541
- assert_equal(0, @zipFile.file.stat("file1").rdev)
542
- end
543
-
544
- def test_rdev_major
545
- assert_equal(0, @zipFile.file.stat("file1").rdev_major)
546
- end
547
-
548
- def test_rdev_minor
549
- assert_equal(0, @zipFile.file.stat("file1").rdev_minor)
550
- end
551
-
552
- def test_nlink
553
- assert_equal(1, @zipFile.file.stat("file1").nlink)
554
- end
555
-
556
- def test_blksize
557
- assert_nil(@zipFile.file.stat("file1").blksize)
558
- end
559
-
560
- end
561
-
562
- class ZipFsFileMutatingTest < Test::Unit::TestCase
563
- TEST_ZIP = "zipWithDirs_copy.zip"
564
- def setup
565
- FileUtils.cp("data/zipWithDirs.zip", TEST_ZIP)
566
- end
567
-
568
- def teardown
569
- end
570
-
571
- def test_delete
572
- do_test_delete_or_unlink(:delete)
573
- end
574
-
575
- def test_unlink
576
- do_test_delete_or_unlink(:unlink)
577
- end
578
-
579
- def test_open_write
580
- ZipFile.open(TEST_ZIP) {
581
- |zf|
582
-
583
- zf.file.open("test_open_write_entry", "w") {
584
- |f|
585
- blockCalled = true
586
- f.write "This is what I'm writing"
587
- }
588
- assert_equal("This is what I'm writing",
589
- zf.file.read("test_open_write_entry"))
590
-
591
- # Test with existing entry
592
- zf.file.open("file1", "wb") { #also check that 'b' option is ignored
593
- |f|
594
- blockCalled = true
595
- f.write "This is what I'm writing too"
596
- }
597
- assert_equal("This is what I'm writing too",
598
- zf.file.read("file1"))
599
- }
600
- end
601
-
602
- def test_rename
603
- ZipFile.open(TEST_ZIP) {
604
- |zf|
605
- assert_raise(Errno::ENOENT, "") {
606
- zf.file.rename("NoSuchFile", "bimse")
607
- }
608
- zf.file.rename("file1", "newNameForFile1")
609
- }
610
-
611
- ZipFile.open(TEST_ZIP) {
612
- |zf|
613
- assert(! zf.file.exists?("file1"))
614
- assert(zf.file.exists?("newNameForFile1"))
615
- }
616
- end
617
-
618
- def do_test_delete_or_unlink(symbol)
619
- ZipFile.open(TEST_ZIP) {
620
- |zf|
621
- assert(zf.file.exists?("dir2/dir21/dir221/file2221"))
622
- zf.file.send(symbol, "dir2/dir21/dir221/file2221")
623
- assert(! zf.file.exists?("dir2/dir21/dir221/file2221"))
624
-
625
- assert(zf.file.exists?("dir1/file11"))
626
- assert(zf.file.exists?("dir1/file12"))
627
- zf.file.send(symbol, "dir1/file11", "dir1/file12")
628
- assert(! zf.file.exists?("dir1/file11"))
629
- assert(! zf.file.exists?("dir1/file12"))
630
-
631
- assert_raise(Errno::ENOENT) { zf.file.send(symbol, "noSuchFile") }
632
- assert_raise(Errno::EISDIR) { zf.file.send(symbol, "dir1/dir11") }
633
- assert_raise(Errno::EISDIR) { zf.file.send(symbol, "dir1/dir11/") }
634
- }
635
-
636
- ZipFile.open(TEST_ZIP) {
637
- |zf|
638
- assert(! zf.file.exists?("dir2/dir21/dir221/file2221"))
639
- assert(! zf.file.exists?("dir1/file11"))
640
- assert(! zf.file.exists?("dir1/file12"))
641
-
642
- assert(zf.file.exists?("dir1/dir11"))
643
- assert(zf.file.exists?("dir1/dir11/"))
644
- }
645
- end
646
-
647
- end
648
-
649
- class ZipFsDirectoryTest < Test::Unit::TestCase
650
- TEST_ZIP = "zipWithDirs_copy.zip"
651
-
652
- def setup
653
- FileUtils.cp("data/zipWithDirs.zip", TEST_ZIP)
654
- end
655
-
656
- def test_delete
657
- ZipFile.open(TEST_ZIP) {
658
- |zf|
659
- assert_raise(Errno::ENOENT, "No such file or directory - NoSuchFile.txt") {
660
- zf.dir.delete("NoSuchFile.txt")
661
- }
662
- assert_raise(Errno::EINVAL, "Invalid argument - file1") {
663
- zf.dir.delete("file1")
664
- }
665
- assert(zf.file.exists?("dir1"))
666
- zf.dir.delete("dir1")
667
- assert(! zf.file.exists?("dir1"))
668
- }
669
- end
670
-
671
- def test_mkdir
672
- ZipFile.open(TEST_ZIP) {
673
- |zf|
674
- assert_raise(Errno::EEXIST, "File exists - dir1") {
675
- zf.dir.mkdir("file1")
676
- }
677
- assert_raise(Errno::EEXIST, "File exists - dir1") {
678
- zf.dir.mkdir("dir1")
679
- }
680
- assert(!zf.file.exists?("newDir"))
681
- zf.dir.mkdir("newDir")
682
- assert(zf.file.directory?("newDir"))
683
- assert(!zf.file.exists?("newDir2"))
684
- zf.dir.mkdir("newDir2", 3485)
685
- assert(zf.file.directory?("newDir2"))
686
- }
687
- end
688
-
689
- def test_pwd_chdir_entries
690
- ZipFile.open(TEST_ZIP) {
691
- |zf|
692
- assert_equal("/", zf.dir.pwd)
693
-
694
- assert_raise(Errno::ENOENT, "No such file or directory - no such dir") {
695
- zf.dir.chdir "no such dir"
696
- }
697
-
698
- assert_raise(Errno::EINVAL, "Invalid argument - file1") {
699
- zf.dir.chdir "file1"
700
- }
701
-
702
- assert_equal(["dir1", "dir2", "file1"].sort, zf.dir.entries(".").sort)
703
- zf.dir.chdir "dir1"
704
- assert_equal("/dir1", zf.dir.pwd)
705
- assert_equal(["dir11", "file11", "file12"], zf.dir.entries(".").sort)
706
-
707
- zf.dir.chdir "../dir2/dir21"
708
- assert_equal("/dir2/dir21", zf.dir.pwd)
709
- assert_equal(["dir221"].sort, zf.dir.entries(".").sort)
710
- }
711
- end
712
-
713
- def test_foreach
714
- ZipFile.open(TEST_ZIP) {
715
- |zf|
716
-
717
- blockCalled = false
718
- assert_raise(Errno::ENOENT, "No such file or directory - noSuchDir") {
719
- zf.dir.foreach("noSuchDir") { |e| blockCalled = true }
720
- }
721
- assert(! blockCalled)
722
-
723
- assert_raise(Errno::ENOTDIR, "Not a directory - file1") {
724
- zf.dir.foreach("file1") { |e| blockCalled = true }
725
- }
726
- assert(! blockCalled)
727
-
728
- entries = []
729
- zf.dir.foreach(".") { |e| entries << e }
730
- assert_equal(["dir1", "dir2", "file1"].sort, entries.sort)
731
-
732
- entries = []
733
- zf.dir.foreach("dir1") { |e| entries << e }
734
- assert_equal(["dir11", "file11", "file12"], entries.sort)
735
- }
736
- end
737
-
738
- def test_chroot
739
- ZipFile.open(TEST_ZIP) {
740
- |zf|
741
- assert_raise(NotImplementedError) {
742
- zf.dir.chroot
743
- }
744
- }
745
- end
746
-
747
- # Globbing not supported yet
748
- #def test_glob
749
- # # test alias []-operator too
750
- # fail "implement test"
751
- #end
752
-
753
- def test_open_new
754
- ZipFile.open(TEST_ZIP) {
755
- |zf|
756
-
757
- assert_raise(Errno::ENOTDIR, "Not a directory - file1") {
758
- zf.dir.new("file1")
759
- }
760
-
761
- assert_raise(Errno::ENOENT, "No such file or directory - noSuchFile") {
762
- zf.dir.new("noSuchFile")
763
- }
764
-
765
- d = zf.dir.new(".")
766
- assert_equal(["file1", "dir1", "dir2"].sort, d.entries.sort)
767
- d.close
768
-
769
- zf.dir.open("dir1") {
770
- |d|
771
- assert_equal(["dir11", "file11", "file12"].sort, d.entries.sort)
772
- }
773
- }
774
- end
775
-
776
- end
777
-
778
- class ZipFsDirIteratorTest < Test::Unit::TestCase
779
-
780
- FILENAME_ARRAY = [ "f1", "f2", "f3", "f4", "f5", "f6" ]
781
-
782
- def setup
783
- @dirIt = ZipFileSystem::ZipFsDirIterator.new(FILENAME_ARRAY)
784
- end
785
-
786
- def test_close
787
- @dirIt.close
788
- assert_raise(IOError, "closed directory") {
789
- @dirIt.each { |e| p e }
790
- }
791
- assert_raise(IOError, "closed directory") {
792
- @dirIt.read
793
- }
794
- assert_raise(IOError, "closed directory") {
795
- @dirIt.rewind
796
- }
797
- assert_raise(IOError, "closed directory") {
798
- @dirIt.seek(0)
799
- }
800
- assert_raise(IOError, "closed directory") {
801
- @dirIt.tell
802
- }
803
-
804
- end
805
-
806
- def test_each
807
- # Tested through Enumerable.entries
808
- assert_equal(FILENAME_ARRAY, @dirIt.entries)
809
- end
810
-
811
- def test_read
812
- FILENAME_ARRAY.size.times {
813
- |i|
814
- assert_equal(FILENAME_ARRAY[i], @dirIt.read)
815
- }
816
- end
817
-
818
- def test_rewind
819
- @dirIt.read
820
- @dirIt.read
821
- assert_equal(FILENAME_ARRAY[2], @dirIt.read)
822
- @dirIt.rewind
823
- assert_equal(FILENAME_ARRAY[0], @dirIt.read)
824
- end
825
-
826
- def test_tell_seek
827
- @dirIt.read
828
- @dirIt.read
829
- pos = @dirIt.tell
830
- valAtPos = @dirIt.read
831
- @dirIt.read
832
- @dirIt.seek(pos)
833
- assert_equal(valAtPos, @dirIt.read)
834
- end
835
-
836
- end
837
-
838
-
839
- # Copyright (C) 2002, 2003 Thomas Sondergaard
840
- # rubyzip is free software; you can redistribute it and/or
841
- # modify it under the terms of the ruby license.