rubyzip 1.1.7 → 2.3.2
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.
- checksums.yaml +5 -5
- data/README.md +137 -54
- data/Rakefile +6 -4
- data/lib/zip/central_directory.rb +17 -13
- data/lib/zip/compressor.rb +1 -2
- data/lib/zip/constants.rb +57 -5
- data/lib/zip/crypto/decrypted_io.rb +40 -0
- data/lib/zip/crypto/null_encryption.rb +4 -6
- data/lib/zip/crypto/traditional_encryption.rb +14 -14
- data/lib/zip/decompressor.rb +22 -4
- data/lib/zip/deflater.rb +8 -6
- data/lib/zip/dos_time.rb +17 -13
- data/lib/zip/entry.rb +171 -148
- data/lib/zip/entry_set.rb +16 -14
- data/lib/zip/errors.rb +3 -0
- data/lib/zip/extra_field/generic.rb +14 -13
- data/lib/zip/extra_field/ntfs.rb +18 -16
- data/lib/zip/extra_field/old_unix.rb +12 -11
- data/lib/zip/extra_field/universal_time.rb +46 -16
- data/lib/zip/extra_field/unix.rb +10 -9
- data/lib/zip/extra_field/zip64.rb +15 -12
- data/lib/zip/extra_field/zip64_placeholder.rb +1 -2
- data/lib/zip/extra_field.rb +18 -16
- data/lib/zip/file.rb +147 -115
- data/lib/zip/filesystem.rb +289 -272
- data/lib/zip/inflater.rb +24 -36
- data/lib/zip/input_stream.rb +44 -28
- data/lib/zip/ioextras/abstract_input_stream.rb +24 -17
- data/lib/zip/ioextras/abstract_output_stream.rb +4 -6
- data/lib/zip/ioextras.rb +2 -4
- data/lib/zip/null_compressor.rb +2 -2
- data/lib/zip/null_decompressor.rb +3 -11
- data/lib/zip/null_input_stream.rb +0 -0
- data/lib/zip/output_stream.rb +25 -17
- data/lib/zip/pass_thru_compressor.rb +6 -6
- data/lib/zip/pass_thru_decompressor.rb +14 -24
- data/lib/zip/streamable_directory.rb +3 -3
- data/lib/zip/streamable_stream.rb +7 -11
- data/lib/zip/version.rb +1 -1
- data/lib/zip.rb +15 -6
- data/samples/example.rb +29 -39
- data/samples/example_filesystem.rb +16 -18
- data/samples/example_recursive.rb +31 -25
- data/samples/gtk_ruby_zip.rb +84 -0
- data/samples/qtzip.rb +23 -32
- data/samples/write_simple.rb +10 -13
- data/samples/zipfind.rb +33 -40
- metadata +50 -141
- data/samples/gtkRubyzip.rb +0 -86
- data/test/basic_zip_file_test.rb +0 -64
- data/test/central_directory_entry_test.rb +0 -73
- data/test/central_directory_test.rb +0 -104
- data/test/crypto/null_encryption_test.rb +0 -53
- data/test/crypto/traditional_encryption_test.rb +0 -80
- data/test/data/WarnInvalidDate.zip +0 -0
- data/test/data/file1.txt +0 -46
- data/test/data/file1.txt.deflatedData +0 -0
- data/test/data/file2.txt +0 -1504
- data/test/data/globTest/foo/bar/baz/foo.txt +0 -0
- data/test/data/globTest/foo.txt +0 -0
- data/test/data/globTest/food.txt +0 -0
- data/test/data/globTest.zip +0 -0
- data/test/data/mimetype +0 -1
- data/test/data/notzippedruby.rb +0 -7
- data/test/data/ntfs.zip +0 -0
- data/test/data/rubycode.zip +0 -0
- data/test/data/rubycode2.zip +0 -0
- data/test/data/testDirectory.bin +0 -0
- data/test/data/zip64-sample.zip +0 -0
- data/test/data/zipWithDirs.zip +0 -0
- data/test/data/zipWithEncryption.zip +0 -0
- data/test/deflater_test.rb +0 -67
- data/test/encryption_test.rb +0 -42
- data/test/entry_set_test.rb +0 -138
- data/test/entry_test.rb +0 -165
- data/test/errors_test.rb +0 -36
- data/test/extra_field_test.rb +0 -78
- data/test/file_extract_directory_test.rb +0 -56
- data/test/file_extract_test.rb +0 -90
- data/test/file_split_test.rb +0 -60
- data/test/file_test.rb +0 -559
- data/test/filesystem/dir_iterator_test.rb +0 -62
- data/test/filesystem/directory_test.rb +0 -131
- data/test/filesystem/file_mutating_test.rb +0 -100
- data/test/filesystem/file_nonmutating_test.rb +0 -514
- data/test/filesystem/file_stat_test.rb +0 -66
- data/test/gentestfiles.rb +0 -134
- data/test/inflater_test.rb +0 -14
- data/test/input_stream_test.rb +0 -170
- data/test/ioextras/abstract_input_stream_test.rb +0 -103
- data/test/ioextras/abstract_output_stream_test.rb +0 -106
- data/test/ioextras/fake_io_test.rb +0 -18
- data/test/local_entry_test.rb +0 -156
- data/test/output_stream_test.rb +0 -129
- data/test/pass_thru_compressor_test.rb +0 -31
- data/test/pass_thru_decompressor_test.rb +0 -15
- data/test/settings_test.rb +0 -92
- data/test/test_helper.rb +0 -228
- data/test/unicode_file_names_and_comments_test.rb +0 -52
- data/test/zip64_full_test.rb +0 -53
- data/test/zip64_support_test.rb +0 -15
data/test/local_entry_test.rb
DELETED
@@ -1,156 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
class ZipLocalEntryTest < MiniTest::Test
|
4
|
-
|
5
|
-
CEH_FILE = 'test/data/generated/centralEntryHeader.bin'
|
6
|
-
LEH_FILE = 'test/data/generated/localEntryHeader.bin'
|
7
|
-
|
8
|
-
def teardown
|
9
|
-
::Zip.write_zip64_support = false
|
10
|
-
end
|
11
|
-
|
12
|
-
def test_read_local_entryHeaderOfFirstTestZipEntry
|
13
|
-
::File.open(TestZipFile::TEST_ZIP3.zip_name, "rb") do |file|
|
14
|
-
entry = ::Zip::Entry.read_local_entry(file)
|
15
|
-
|
16
|
-
assert_equal('', entry.comment)
|
17
|
-
# Differs from windows and unix because of CR LF
|
18
|
-
# assert_equal(480, entry.compressed_size)
|
19
|
-
# assert_equal(0x2a27930f, entry.crc)
|
20
|
-
# extra field is 21 bytes long
|
21
|
-
# probably contains some unix attrutes or something
|
22
|
-
# disabled: assert_equal(nil, entry.extra)
|
23
|
-
assert_equal(::Zip::Entry::DEFLATED, entry.compression_method)
|
24
|
-
assert_equal(TestZipFile::TEST_ZIP3.entry_names[0], entry.name)
|
25
|
-
assert_equal(::File.size(TestZipFile::TEST_ZIP3.entry_names[0]), entry.size)
|
26
|
-
assert(!entry.directory?)
|
27
|
-
end
|
28
|
-
end
|
29
|
-
|
30
|
-
def test_readDateTime
|
31
|
-
::File.open("test/data/rubycode.zip", "rb") {
|
32
|
-
|file|
|
33
|
-
entry = ::Zip::Entry.read_local_entry(file)
|
34
|
-
assert_equal("zippedruby1.rb", entry.name)
|
35
|
-
assert_equal(::Zip::DOSTime.at(1019261638), entry.time)
|
36
|
-
}
|
37
|
-
end
|
38
|
-
|
39
|
-
def test_read_local_entryFromNonZipFile
|
40
|
-
::File.open("test/data/file2.txt") {
|
41
|
-
|file|
|
42
|
-
assert_equal(nil, ::Zip::Entry.read_local_entry(file))
|
43
|
-
}
|
44
|
-
end
|
45
|
-
|
46
|
-
def test_read_local_entryFromTruncatedZipFile
|
47
|
-
zipFragment=""
|
48
|
-
::File.open(TestZipFile::TEST_ZIP2.zip_name) { |f| zipFragment = f.read(12) } # local header is at least 30 bytes
|
49
|
-
zipFragment.extend(IOizeString).reset
|
50
|
-
entry = ::Zip::Entry.new
|
51
|
-
entry.read_local_entry(zipFragment)
|
52
|
-
fail "ZipError expected"
|
53
|
-
rescue ::Zip::Error
|
54
|
-
end
|
55
|
-
|
56
|
-
def test_writeEntry
|
57
|
-
entry = ::Zip::Entry.new("file.zip", "entryName", "my little comment",
|
58
|
-
"thisIsSomeExtraInformation", 100, 987654,
|
59
|
-
::Zip::Entry::DEFLATED, 400)
|
60
|
-
write_to_file(LEH_FILE, CEH_FILE, entry)
|
61
|
-
entryReadLocal, entryReadCentral = read_from_file(LEH_FILE, CEH_FILE)
|
62
|
-
assert(entryReadCentral.extra['Zip64Placeholder'].nil?, 'zip64 placeholder should not be used in central directory')
|
63
|
-
compare_local_entry_headers(entry, entryReadLocal)
|
64
|
-
compare_c_dir_entry_headers(entry, entryReadCentral)
|
65
|
-
end
|
66
|
-
|
67
|
-
def test_writeEntryWithZip64
|
68
|
-
::Zip.write_zip64_support = true
|
69
|
-
entry = ::Zip::Entry.new("file.zip", "entryName", "my little comment",
|
70
|
-
"thisIsSomeExtraInformation", 100, 987654,
|
71
|
-
::Zip::Entry::DEFLATED, 400)
|
72
|
-
write_to_file(LEH_FILE, CEH_FILE, entry)
|
73
|
-
entryReadLocal, entryReadCentral = read_from_file(LEH_FILE, CEH_FILE)
|
74
|
-
assert(entryReadLocal.extra['Zip64Placeholder'], 'zip64 placeholder should be used in local file header')
|
75
|
-
entryReadLocal.extra.delete('Zip64Placeholder') # it was removed when writing the c_dir_entry, so remove from compare
|
76
|
-
assert(entryReadCentral.extra['Zip64Placeholder'].nil?, 'zip64 placeholder should not be used in central directory')
|
77
|
-
compare_local_entry_headers(entry, entryReadLocal)
|
78
|
-
compare_c_dir_entry_headers(entry, entryReadCentral)
|
79
|
-
end
|
80
|
-
|
81
|
-
def test_write64Entry
|
82
|
-
::Zip.write_zip64_support = true
|
83
|
-
entry = ::Zip::Entry.new("bigfile.zip", "entryName", "my little equine",
|
84
|
-
"malformed extra field because why not",
|
85
|
-
0x7766554433221100, 0xDEADBEEF, ::Zip::Entry::DEFLATED,
|
86
|
-
0x9988776655443322)
|
87
|
-
write_to_file(LEH_FILE, CEH_FILE, entry)
|
88
|
-
entryReadLocal, entryReadCentral = read_from_file(LEH_FILE, CEH_FILE)
|
89
|
-
compare_local_entry_headers(entry, entryReadLocal)
|
90
|
-
compare_c_dir_entry_headers(entry, entryReadCentral)
|
91
|
-
end
|
92
|
-
|
93
|
-
def test_rewriteLocalHeader64
|
94
|
-
::Zip.write_zip64_support = true
|
95
|
-
buf1 = StringIO.new
|
96
|
-
entry = ::Zip::Entry.new("file.zip", "entryName")
|
97
|
-
entry.write_local_entry(buf1)
|
98
|
-
assert(entry.extra['Zip64'].nil?, "zip64 extra is unnecessarily present")
|
99
|
-
|
100
|
-
buf2 = StringIO.new
|
101
|
-
entry.size = 0x123456789ABCDEF0
|
102
|
-
entry.compressed_size = 0x0123456789ABCDEF
|
103
|
-
entry.write_local_entry(buf2, true)
|
104
|
-
refute_nil(entry.extra['Zip64'])
|
105
|
-
refute_equal(buf1.size, 0)
|
106
|
-
assert_equal(buf1.size, buf2.size) # it can't grow, or we'd clobber file data
|
107
|
-
end
|
108
|
-
|
109
|
-
def test_readLocalOffset
|
110
|
-
entry = ::Zip::Entry.new("file.zip", "entryName")
|
111
|
-
entry.local_header_offset = 12345
|
112
|
-
::File.open(CEH_FILE, 'wb') { |f| entry.write_c_dir_entry(f) }
|
113
|
-
read_entry = nil
|
114
|
-
::File.open(CEH_FILE, 'rb') { |f| read_entry = ::Zip::Entry.read_c_dir_entry(f) }
|
115
|
-
compare_c_dir_entry_headers(entry, read_entry)
|
116
|
-
end
|
117
|
-
|
118
|
-
def test_read64LocalOffset
|
119
|
-
::Zip.write_zip64_support = true
|
120
|
-
entry = ::Zip::Entry.new("file.zip", "entryName")
|
121
|
-
entry.local_header_offset = 0x0123456789ABCDEF
|
122
|
-
::File.open(CEH_FILE, 'wb') { |f| entry.write_c_dir_entry(f) }
|
123
|
-
read_entry = nil
|
124
|
-
::File.open(CEH_FILE, 'rb') { |f| read_entry = ::Zip::Entry.read_c_dir_entry(f) }
|
125
|
-
compare_c_dir_entry_headers(entry, read_entry)
|
126
|
-
end
|
127
|
-
|
128
|
-
private
|
129
|
-
def compare_local_entry_headers(entry1, entry2)
|
130
|
-
assert_equal(entry1.compressed_size, entry2.compressed_size)
|
131
|
-
assert_equal(entry1.crc, entry2.crc)
|
132
|
-
assert_equal(entry1.extra, entry2.extra)
|
133
|
-
assert_equal(entry1.compression_method, entry2.compression_method)
|
134
|
-
assert_equal(entry1.name, entry2.name)
|
135
|
-
assert_equal(entry1.size, entry2.size)
|
136
|
-
assert_equal(entry1.local_header_offset, entry2.local_header_offset)
|
137
|
-
end
|
138
|
-
|
139
|
-
def compare_c_dir_entry_headers(entry1, entry2)
|
140
|
-
compare_local_entry_headers(entry1, entry2)
|
141
|
-
assert_equal(entry1.comment, entry2.comment)
|
142
|
-
end
|
143
|
-
|
144
|
-
def write_to_file(localFileName, centralFileName, entry)
|
145
|
-
::File.open(localFileName, "wb") { |f| entry.write_local_entry(f) }
|
146
|
-
::File.open(centralFileName, "wb") { |f| entry.write_c_dir_entry(f) }
|
147
|
-
end
|
148
|
-
|
149
|
-
def read_from_file(localFileName, centralFileName)
|
150
|
-
localEntry = nil
|
151
|
-
cdirEntry = nil
|
152
|
-
::File.open(localFileName, "rb") { |f| localEntry = ::Zip::Entry.read_local_entry(f) }
|
153
|
-
::File.open(centralFileName, "rb") { |f| cdirEntry = ::Zip::Entry.read_c_dir_entry(f) }
|
154
|
-
[localEntry, cdirEntry]
|
155
|
-
end
|
156
|
-
end
|
data/test/output_stream_test.rb
DELETED
@@ -1,129 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
class ZipOutputStreamTest < MiniTest::Test
|
4
|
-
include AssertEntry
|
5
|
-
|
6
|
-
TEST_ZIP = TestZipFile::TEST_ZIP2.clone
|
7
|
-
TEST_ZIP.zip_name = "test/data/generated/output.zip"
|
8
|
-
|
9
|
-
def test_new
|
10
|
-
zos = ::Zip::OutputStream.new(TEST_ZIP.zip_name)
|
11
|
-
zos.comment = TEST_ZIP.comment
|
12
|
-
write_test_zip(zos)
|
13
|
-
zos.close
|
14
|
-
assert_test_zip_contents(TEST_ZIP)
|
15
|
-
end
|
16
|
-
|
17
|
-
def test_open
|
18
|
-
::Zip::OutputStream.open(TEST_ZIP.zip_name) do |zos|
|
19
|
-
zos.comment = TEST_ZIP.comment
|
20
|
-
write_test_zip(zos)
|
21
|
-
end
|
22
|
-
assert_test_zip_contents(TEST_ZIP)
|
23
|
-
end
|
24
|
-
|
25
|
-
def test_write_buffer
|
26
|
-
io = ::StringIO.new('')
|
27
|
-
buffer = ::Zip::OutputStream.write_buffer(io) do |zos|
|
28
|
-
zos.comment = TEST_ZIP.comment
|
29
|
-
write_test_zip(zos)
|
30
|
-
end
|
31
|
-
File.open(TEST_ZIP.zip_name, 'wb') { |f| f.write buffer.string }
|
32
|
-
assert_test_zip_contents(TEST_ZIP)
|
33
|
-
end
|
34
|
-
|
35
|
-
def test_write_buffer_with_temp_file
|
36
|
-
tmp_file = Tempfile.new('')
|
37
|
-
|
38
|
-
::Zip::OutputStream.write_buffer(tmp_file) do |zos|
|
39
|
-
zos.comment = TEST_ZIP.comment
|
40
|
-
write_test_zip(zos)
|
41
|
-
end
|
42
|
-
|
43
|
-
tmp_file.rewind
|
44
|
-
File.open(TEST_ZIP.zip_name, 'wb') { |f| f.write(tmp_file.read) }
|
45
|
-
tmp_file.unlink
|
46
|
-
|
47
|
-
assert_test_zip_contents(TEST_ZIP)
|
48
|
-
end
|
49
|
-
|
50
|
-
def test_writingToClosedStream
|
51
|
-
assert_i_o_error_in_closed_stream { |zos| zos << "hello world" }
|
52
|
-
assert_i_o_error_in_closed_stream { |zos| zos.puts "hello world" }
|
53
|
-
assert_i_o_error_in_closed_stream { |zos| zos.write "hello world" }
|
54
|
-
end
|
55
|
-
|
56
|
-
def test_cannotOpenFile
|
57
|
-
name = TestFiles::EMPTY_TEST_DIR
|
58
|
-
begin
|
59
|
-
::Zip::OutputStream.open(name)
|
60
|
-
rescue Exception
|
61
|
-
assert($!.kind_of?(Errno::EISDIR) || # Linux
|
62
|
-
$!.kind_of?(Errno::EEXIST) || # Windows/cygwin
|
63
|
-
$!.kind_of?(Errno::EACCES), # Windows
|
64
|
-
"Expected Errno::EISDIR (or on win/cygwin: Errno::EEXIST), but was: #{$!.class}")
|
65
|
-
end
|
66
|
-
end
|
67
|
-
|
68
|
-
def test_put_next_entry
|
69
|
-
stored_text = "hello world in stored text"
|
70
|
-
entry_name = "file1"
|
71
|
-
comment = "my comment"
|
72
|
-
::Zip::OutputStream.open(TEST_ZIP.zip_name) do |zos|
|
73
|
-
zos.put_next_entry(entry_name, comment, nil, ::Zip::Entry::STORED)
|
74
|
-
zos << stored_text
|
75
|
-
end
|
76
|
-
|
77
|
-
assert(File.read(TEST_ZIP.zip_name)[stored_text])
|
78
|
-
::Zip::File.open(TEST_ZIP.zip_name) do |zf|
|
79
|
-
assert_equal(stored_text, zf.read(entry_name))
|
80
|
-
end
|
81
|
-
end
|
82
|
-
|
83
|
-
def test_put_next_entry_using_zip_entry_creates_entries_with_correct_timestamps
|
84
|
-
file = ::File.open("test/data/file2.txt", "rb")
|
85
|
-
::Zip::OutputStream.open(TEST_ZIP.zip_name) do |zos|
|
86
|
-
zip_entry = ::Zip::Entry.new(zos, file.path, "", "", 0, 0, ::Zip::Entry::DEFLATED, 0, ::Zip::DOSTime.at(file.mtime))
|
87
|
-
zos.put_next_entry(zip_entry)
|
88
|
-
zos << file.read
|
89
|
-
end
|
90
|
-
|
91
|
-
::Zip::InputStream::open(TEST_ZIP.zip_name) do |io|
|
92
|
-
while (entry = io.get_next_entry)
|
93
|
-
assert(::Zip::DOSTime.at(file.mtime).dos_equals(::Zip::DOSTime.at(entry.mtime))) # Compare DOS Times, since they are stored with two seconds accuracy
|
94
|
-
end
|
95
|
-
end
|
96
|
-
end
|
97
|
-
|
98
|
-
def test_chained_put_into_next_entry
|
99
|
-
stored_text = "hello world in stored text"
|
100
|
-
stored_text2 = "with chain"
|
101
|
-
entry_name = "file1"
|
102
|
-
comment = "my comment"
|
103
|
-
::Zip::OutputStream.open(TEST_ZIP.zip_name) do |zos|
|
104
|
-
zos.put_next_entry(entry_name, comment, nil, ::Zip::Entry::STORED)
|
105
|
-
zos << stored_text << stored_text2
|
106
|
-
end
|
107
|
-
|
108
|
-
assert(File.read(TEST_ZIP.zip_name)[stored_text])
|
109
|
-
::Zip::File.open(TEST_ZIP.zip_name) do |zf|
|
110
|
-
assert_equal(stored_text + stored_text2, zf.read(entry_name))
|
111
|
-
end
|
112
|
-
|
113
|
-
end
|
114
|
-
|
115
|
-
def assert_i_o_error_in_closed_stream
|
116
|
-
assert_raises(IOError) {
|
117
|
-
zos = ::Zip::OutputStream.new("test/data/generated/test_putOnClosedStream.zip")
|
118
|
-
zos.close
|
119
|
-
yield zos
|
120
|
-
}
|
121
|
-
end
|
122
|
-
|
123
|
-
def write_test_zip(zos)
|
124
|
-
TEST_ZIP.entry_names.each do |entryName|
|
125
|
-
zos.put_next_entry(entryName)
|
126
|
-
File.open(entryName, "rb") { |f| zos.write(f.read) }
|
127
|
-
end
|
128
|
-
end
|
129
|
-
end
|
@@ -1,31 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
class PassThruCompressorTest < MiniTest::Test
|
4
|
-
include CrcTest
|
5
|
-
|
6
|
-
def test_size
|
7
|
-
File.open("test/data/generated/dummy.txt", "wb") {
|
8
|
-
|file|
|
9
|
-
compressor = ::Zip::PassThruCompressor.new(file)
|
10
|
-
|
11
|
-
assert_equal(0, compressor.size)
|
12
|
-
|
13
|
-
t1 = "hello world"
|
14
|
-
t2 = ""
|
15
|
-
t3 = "bingo"
|
16
|
-
|
17
|
-
compressor << t1
|
18
|
-
assert_equal(compressor.size, t1.size)
|
19
|
-
|
20
|
-
compressor << t2
|
21
|
-
assert_equal(compressor.size, t1.size + t2.size)
|
22
|
-
|
23
|
-
compressor << t3
|
24
|
-
assert_equal(compressor.size, t1.size + t2.size + t3.size)
|
25
|
-
}
|
26
|
-
end
|
27
|
-
|
28
|
-
def test_crc
|
29
|
-
run_crc_test(::Zip::PassThruCompressor)
|
30
|
-
end
|
31
|
-
end
|
@@ -1,15 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
class PassThruDecompressorTest < MiniTest::Test
|
3
|
-
include DecompressorTests
|
4
|
-
|
5
|
-
def setup
|
6
|
-
super
|
7
|
-
@file = File.new(TEST_FILE)
|
8
|
-
@decompressor = ::Zip::PassThruDecompressor.new(@file, File.size(TEST_FILE))
|
9
|
-
end
|
10
|
-
|
11
|
-
def teardown
|
12
|
-
@file.close
|
13
|
-
end
|
14
|
-
end
|
15
|
-
|
data/test/settings_test.rb
DELETED
@@ -1,92 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
class ZipSettingsTest < MiniTest::Test
|
4
|
-
# TODO Refactor out into common test module
|
5
|
-
include CommonZipFileFixture
|
6
|
-
|
7
|
-
TEST_OUT_NAME = "test/data/generated/emptyOutDir"
|
8
|
-
|
9
|
-
def setup
|
10
|
-
super
|
11
|
-
|
12
|
-
Dir.rmdir(TEST_OUT_NAME) if File.directory? TEST_OUT_NAME
|
13
|
-
File.delete(TEST_OUT_NAME) if File.exist? TEST_OUT_NAME
|
14
|
-
end
|
15
|
-
|
16
|
-
def teardown
|
17
|
-
::Zip.reset!
|
18
|
-
end
|
19
|
-
|
20
|
-
def open_zip(&aProc)
|
21
|
-
assert(aProc != nil)
|
22
|
-
::Zip::File.open(TestZipFile::TEST_ZIP4.zip_name, &aProc)
|
23
|
-
end
|
24
|
-
|
25
|
-
def extract_test_dir(&aProc)
|
26
|
-
open_zip {
|
27
|
-
|zf|
|
28
|
-
zf.extract(TestFiles::EMPTY_TEST_DIR, TEST_OUT_NAME, &aProc)
|
29
|
-
}
|
30
|
-
end
|
31
|
-
|
32
|
-
def test_true_on_exists_proc
|
33
|
-
Zip.on_exists_proc = true
|
34
|
-
File.open(TEST_OUT_NAME, "w") { |f| f.puts "something" }
|
35
|
-
extract_test_dir
|
36
|
-
assert(File.directory?(TEST_OUT_NAME))
|
37
|
-
end
|
38
|
-
|
39
|
-
def test_false_on_exists_proc
|
40
|
-
Zip.on_exists_proc = false
|
41
|
-
File.open(TEST_OUT_NAME, "w") { |f| f.puts "something" }
|
42
|
-
assert_raises(Zip::DestinationFileExistsError) { extract_test_dir }
|
43
|
-
end
|
44
|
-
|
45
|
-
def test_false_continue_on_exists_proc
|
46
|
-
Zip.continue_on_exists_proc = false
|
47
|
-
|
48
|
-
assert_raises(::Zip::EntryExistsError) do
|
49
|
-
::Zip::File.open(TEST_ZIP.zip_name) do |zf|
|
50
|
-
zf.add(zf.entries.first.name, "test/data/file2.txt")
|
51
|
-
end
|
52
|
-
end
|
53
|
-
end
|
54
|
-
|
55
|
-
def test_true_continue_on_exists_proc
|
56
|
-
Zip.continue_on_exists_proc = true
|
57
|
-
|
58
|
-
replacedEntry = nil
|
59
|
-
|
60
|
-
::Zip::File.open(TEST_ZIP.zip_name) do |zf|
|
61
|
-
replacedEntry = zf.entries.first.name
|
62
|
-
zf.add(replacedEntry, "test/data/file2.txt")
|
63
|
-
end
|
64
|
-
|
65
|
-
::Zip::File.open(TEST_ZIP.zip_name) do |zf|
|
66
|
-
assert_contains(zf, replacedEntry, "test/data/file2.txt")
|
67
|
-
end
|
68
|
-
end
|
69
|
-
|
70
|
-
def test_false_warn_invalid_date
|
71
|
-
test_file = File.join(File.dirname(__FILE__), 'data', 'WarnInvalidDate.zip')
|
72
|
-
Zip.warn_invalid_date = false
|
73
|
-
|
74
|
-
::Zip::File.open(test_file) do |zf|
|
75
|
-
end
|
76
|
-
end
|
77
|
-
|
78
|
-
def test_true_warn_invalid_date
|
79
|
-
test_file = File.join(File.dirname(__FILE__), 'data', 'WarnInvalidDate.zip')
|
80
|
-
Zip.warn_invalid_date = true
|
81
|
-
|
82
|
-
::Zip::File.open(test_file) do |zf|
|
83
|
-
end
|
84
|
-
end
|
85
|
-
|
86
|
-
|
87
|
-
private
|
88
|
-
def assert_contains(zf, entryName, filename = entryName)
|
89
|
-
assert(zf.entries.detect { |e| e.name == entryName } != nil, "entry #{entryName} not in #{zf.entries.join(', ')} in zip file #{zf}")
|
90
|
-
assert_entryContents(zf, entryName, filename) if File.exist?(filename)
|
91
|
-
end
|
92
|
-
end
|
data/test/test_helper.rb
DELETED
@@ -1,228 +0,0 @@
|
|
1
|
-
require 'simplecov'
|
2
|
-
require 'minitest/autorun'
|
3
|
-
require 'minitest/unit'
|
4
|
-
require 'fileutils'
|
5
|
-
require 'digest/sha1'
|
6
|
-
require 'zip'
|
7
|
-
require 'gentestfiles'
|
8
|
-
|
9
|
-
TestFiles.create_test_files
|
10
|
-
TestZipFile.create_test_zips
|
11
|
-
|
12
|
-
::MiniTest.after_run do
|
13
|
-
FileUtils.rm_rf('test/data/generated')
|
14
|
-
end
|
15
|
-
|
16
|
-
module IOizeString
|
17
|
-
attr_reader :tell
|
18
|
-
|
19
|
-
def read(count = nil)
|
20
|
-
@tell ||= 0
|
21
|
-
count = size unless count
|
22
|
-
retVal = slice(@tell, count)
|
23
|
-
@tell += count
|
24
|
-
return retVal
|
25
|
-
end
|
26
|
-
|
27
|
-
def seek(index, offset)
|
28
|
-
@tell ||= 0
|
29
|
-
case offset
|
30
|
-
when IO::SEEK_END
|
31
|
-
newPos = size + index
|
32
|
-
when IO::SEEK_SET
|
33
|
-
newPos = index
|
34
|
-
when IO::SEEK_CUR
|
35
|
-
newPos = @tell + index
|
36
|
-
else
|
37
|
-
raise "Error in test method IOizeString::seek"
|
38
|
-
end
|
39
|
-
if (newPos < 0 || newPos >= size)
|
40
|
-
raise Errno::EINVAL
|
41
|
-
else
|
42
|
-
@tell=newPos
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
|
-
def reset
|
47
|
-
@tell = 0
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
module DecompressorTests
|
52
|
-
# expects @refText, @refLines and @decompressor
|
53
|
-
|
54
|
-
TEST_FILE = "test/data/file1.txt"
|
55
|
-
|
56
|
-
def setup
|
57
|
-
@refText = ''
|
58
|
-
File.open(TEST_FILE) { |f| @refText = f.read }
|
59
|
-
@refLines = @refText.split($/)
|
60
|
-
end
|
61
|
-
|
62
|
-
def test_readEverything
|
63
|
-
assert_equal(@refText, @decompressor.sysread)
|
64
|
-
end
|
65
|
-
|
66
|
-
def test_readInChunks
|
67
|
-
chunkSize = 5
|
68
|
-
while (decompressedChunk = @decompressor.sysread(chunkSize))
|
69
|
-
assert_equal(@refText.slice!(0, chunkSize), decompressedChunk)
|
70
|
-
end
|
71
|
-
assert_equal(0, @refText.size)
|
72
|
-
end
|
73
|
-
|
74
|
-
def test_mixingReadsAndProduceInput
|
75
|
-
# Just some preconditions to make sure we have enough data for this test
|
76
|
-
assert(@refText.length > 1000)
|
77
|
-
assert(@refLines.length > 40)
|
78
|
-
|
79
|
-
|
80
|
-
assert_equal(@refText[0...100], @decompressor.sysread(100))
|
81
|
-
|
82
|
-
assert(!@decompressor.input_finished?)
|
83
|
-
buf = @decompressor.produce_input
|
84
|
-
assert_equal(@refText[100...(100+buf.length)], buf)
|
85
|
-
end
|
86
|
-
end
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
module AssertEntry
|
91
|
-
def assert_next_entry(filename, zis)
|
92
|
-
assert_entry(filename, zis, zis.get_next_entry.name)
|
93
|
-
end
|
94
|
-
|
95
|
-
def assert_entry(filename, zis, entryName)
|
96
|
-
assert_equal(filename, entryName)
|
97
|
-
assert_entryContentsForStream(filename, zis, entryName)
|
98
|
-
end
|
99
|
-
|
100
|
-
def assert_entryContentsForStream(filename, zis, entryName)
|
101
|
-
File.open(filename, "rb") {
|
102
|
-
|file|
|
103
|
-
expected = file.read
|
104
|
-
actual = zis.read
|
105
|
-
if (expected != actual)
|
106
|
-
if ((expected && actual) && (expected.length > 400 || actual.length > 400))
|
107
|
-
zipEntryFilename=entryName+".zipEntry"
|
108
|
-
File.open(zipEntryFilename, "wb") { |entryfile| entryfile << actual }
|
109
|
-
fail("File '#{filename}' is different from '#{zipEntryFilename}'")
|
110
|
-
else
|
111
|
-
assert_equal(expected, actual)
|
112
|
-
end
|
113
|
-
end
|
114
|
-
}
|
115
|
-
end
|
116
|
-
|
117
|
-
def AssertEntry.assert_contents(filename, aString)
|
118
|
-
fileContents = ""
|
119
|
-
File.open(filename, "rb") { |f| fileContents = f.read }
|
120
|
-
if (fileContents != aString)
|
121
|
-
if (fileContents.length > 400 || aString.length > 400)
|
122
|
-
stringFile = filename + ".other"
|
123
|
-
File.open(stringFile, "wb") { |f| f << aString }
|
124
|
-
fail("File '#{filename}' is different from contents of string stored in '#{stringFile}'")
|
125
|
-
else
|
126
|
-
assert_equal(fileContents, aString)
|
127
|
-
end
|
128
|
-
end
|
129
|
-
end
|
130
|
-
|
131
|
-
def assert_stream_contents(zis, testZipFile)
|
132
|
-
assert(zis != nil)
|
133
|
-
testZipFile.entry_names.each do |entryName|
|
134
|
-
assert_next_entry(entryName, zis)
|
135
|
-
end
|
136
|
-
assert_equal(nil, zis.get_next_entry)
|
137
|
-
end
|
138
|
-
|
139
|
-
def assert_test_zip_contents(testZipFile)
|
140
|
-
::Zip::InputStream.open(testZipFile.zip_name) do |zis|
|
141
|
-
assert_stream_contents(zis, testZipFile)
|
142
|
-
end
|
143
|
-
end
|
144
|
-
|
145
|
-
def assert_entryContents(zipFile, entryName, filename = entryName.to_s)
|
146
|
-
zis = zipFile.get_input_stream(entryName)
|
147
|
-
assert_entryContentsForStream(filename, zis, entryName)
|
148
|
-
ensure
|
149
|
-
zis.close if zis
|
150
|
-
end
|
151
|
-
end
|
152
|
-
|
153
|
-
|
154
|
-
module CrcTest
|
155
|
-
|
156
|
-
class TestOutputStream
|
157
|
-
include ::Zip::IOExtras::AbstractOutputStream
|
158
|
-
|
159
|
-
attr_accessor :buffer
|
160
|
-
|
161
|
-
def initialize
|
162
|
-
@buffer = ""
|
163
|
-
end
|
164
|
-
|
165
|
-
def << (data)
|
166
|
-
@buffer << data
|
167
|
-
self
|
168
|
-
end
|
169
|
-
end
|
170
|
-
|
171
|
-
def run_crc_test(compressorClass)
|
172
|
-
str = "Here's a nice little text to compute the crc for! Ho hum, it is nice nice nice nice indeed."
|
173
|
-
fakeOut = TestOutputStream.new
|
174
|
-
|
175
|
-
deflater = compressorClass.new(fakeOut)
|
176
|
-
deflater << str
|
177
|
-
assert_equal(0x919920fc, deflater.crc)
|
178
|
-
end
|
179
|
-
end
|
180
|
-
|
181
|
-
|
182
|
-
module Enumerable
|
183
|
-
def compare_enumerables(otherEnumerable)
|
184
|
-
otherAsArray = otherEnumerable.to_a
|
185
|
-
each_with_index {
|
186
|
-
|element, index|
|
187
|
-
return false unless yield(element, otherAsArray[index])
|
188
|
-
}
|
189
|
-
return self.size == otherAsArray.size
|
190
|
-
end
|
191
|
-
end
|
192
|
-
|
193
|
-
|
194
|
-
module CommonZipFileFixture
|
195
|
-
include AssertEntry
|
196
|
-
|
197
|
-
EMPTY_FILENAME = "emptyZipFile.zip"
|
198
|
-
|
199
|
-
TEST_ZIP = TestZipFile::TEST_ZIP2.clone
|
200
|
-
TEST_ZIP.zip_name = "test/data/generated/5entry_copy.zip"
|
201
|
-
|
202
|
-
def setup
|
203
|
-
File.delete(EMPTY_FILENAME) if File.exist?(EMPTY_FILENAME)
|
204
|
-
FileUtils.cp(TestZipFile::TEST_ZIP2.zip_name, TEST_ZIP.zip_name)
|
205
|
-
end
|
206
|
-
end
|
207
|
-
|
208
|
-
|
209
|
-
module ExtraAssertions
|
210
|
-
|
211
|
-
def assert_forwarded(anObject, method, retVal, *expectedArgs)
|
212
|
-
callArgs = nil
|
213
|
-
setCallArgsProc = proc { |args| callArgs = args }
|
214
|
-
anObject.instance_eval <<-"end_eval"
|
215
|
-
alias #{method}_org #{method}
|
216
|
-
def #{method}(*args)
|
217
|
-
ObjectSpace._id2ref(#{setCallArgsProc.object_id}).call(args)
|
218
|
-
ObjectSpace._id2ref(#{retVal.object_id})
|
219
|
-
end
|
220
|
-
end_eval
|
221
|
-
|
222
|
-
assert_equal(retVal, yield) # Invoke test
|
223
|
-
assert_equal(expectedArgs, callArgs)
|
224
|
-
ensure
|
225
|
-
anObject.instance_eval "undef #{method}; alias #{method} #{method}_org"
|
226
|
-
end
|
227
|
-
|
228
|
-
end
|
@@ -1,52 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
require 'test_helper'
|
4
|
-
|
5
|
-
class ZipUnicodeFileNamesAndComments < MiniTest::Test
|
6
|
-
|
7
|
-
FILENAME = File.join(File.dirname(__FILE__), "test1.zip")
|
8
|
-
|
9
|
-
def test_unicode_file_name
|
10
|
-
file_entrys = ["текстовыйфайл.txt", "Résumé.txt", "슬레이어스휘.txt"]
|
11
|
-
directory_entrys = ["папка/текстовыйфайл.txt", "Résumé/Résumé.txt", "슬레이어스휘/슬레이어스휘.txt"]
|
12
|
-
stream = ::Zip::OutputStream.open(FILENAME) do |io|
|
13
|
-
file_entrys.each do |filename|
|
14
|
-
io.put_next_entry(filename)
|
15
|
-
io.write(filename)
|
16
|
-
end
|
17
|
-
directory_entrys.each do |filepath|
|
18
|
-
io.put_next_entry(filepath)
|
19
|
-
io.write(filepath)
|
20
|
-
end
|
21
|
-
end
|
22
|
-
assert(!stream.nil?)
|
23
|
-
::Zip::InputStream.open(FILENAME) do |io|
|
24
|
-
file_entrys.each do |filename|
|
25
|
-
entry = io.get_next_entry
|
26
|
-
entry_name = entry.name
|
27
|
-
entry_name = entry_name.force_encoding("UTF-8")
|
28
|
-
assert(filename == entry_name)
|
29
|
-
end
|
30
|
-
directory_entrys.each do |filepath|
|
31
|
-
entry = io.get_next_entry
|
32
|
-
entry_name = entry.name
|
33
|
-
entry_name = entry_name.force_encoding("UTF-8")
|
34
|
-
assert(filepath == entry_name)
|
35
|
-
end
|
36
|
-
end
|
37
|
-
::File.unlink(FILENAME)
|
38
|
-
end
|
39
|
-
|
40
|
-
def test_unicode_comment
|
41
|
-
str = '渠道升级'
|
42
|
-
::Zip::File.open(FILENAME, Zip::File::CREATE) do |z|
|
43
|
-
z.comment = str
|
44
|
-
end
|
45
|
-
|
46
|
-
::Zip::File.open(FILENAME) do |z|
|
47
|
-
assert(z.comment.force_encoding('UTF-8') == str)
|
48
|
-
end
|
49
|
-
::File.unlink(FILENAME)
|
50
|
-
end
|
51
|
-
|
52
|
-
end
|