rubyzip 1.2.1 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (87) hide show
  1. checksums.yaml +5 -5
  2. data/README.md +83 -35
  3. data/lib/zip.rb +11 -1
  4. data/lib/zip/central_directory.rb +3 -3
  5. data/lib/zip/compressor.rb +1 -2
  6. data/lib/zip/constants.rb +3 -3
  7. data/lib/zip/crypto/null_encryption.rb +2 -4
  8. data/lib/zip/decompressor.rb +1 -1
  9. data/lib/zip/dos_time.rb +1 -1
  10. data/lib/zip/entry.rb +67 -57
  11. data/lib/zip/entry_set.rb +3 -3
  12. data/lib/zip/errors.rb +1 -0
  13. data/lib/zip/extra_field.rb +2 -2
  14. data/lib/zip/extra_field/generic.rb +1 -1
  15. data/lib/zip/extra_field/zip64_placeholder.rb +1 -2
  16. data/lib/zip/file.rb +47 -27
  17. data/lib/zip/filesystem.rb +17 -13
  18. data/lib/zip/inflater.rb +2 -2
  19. data/lib/zip/input_stream.rb +10 -7
  20. data/lib/zip/ioextras/abstract_input_stream.rb +1 -1
  21. data/lib/zip/ioextras/abstract_output_stream.rb +1 -1
  22. data/lib/zip/output_stream.rb +5 -5
  23. data/lib/zip/pass_thru_decompressor.rb +1 -1
  24. data/lib/zip/streamable_stream.rb +1 -1
  25. data/lib/zip/version.rb +1 -1
  26. data/samples/example_recursive.rb +14 -15
  27. data/samples/gtk_ruby_zip.rb +1 -1
  28. data/samples/qtzip.rb +1 -1
  29. data/samples/zipfind.rb +2 -2
  30. metadata +25 -121
  31. data/test/basic_zip_file_test.rb +0 -60
  32. data/test/case_sensitivity_test.rb +0 -69
  33. data/test/central_directory_entry_test.rb +0 -69
  34. data/test/central_directory_test.rb +0 -100
  35. data/test/crypto/null_encryption_test.rb +0 -57
  36. data/test/crypto/traditional_encryption_test.rb +0 -80
  37. data/test/data/WarnInvalidDate.zip +0 -0
  38. data/test/data/file1.txt +0 -46
  39. data/test/data/file1.txt.deflatedData +0 -0
  40. data/test/data/file2.txt +0 -1504
  41. data/test/data/globTest.zip +0 -0
  42. data/test/data/globTest/foo.txt +0 -0
  43. data/test/data/globTest/foo/bar/baz/foo.txt +0 -0
  44. data/test/data/globTest/food.txt +0 -0
  45. data/test/data/mimetype +0 -1
  46. data/test/data/notzippedruby.rb +0 -7
  47. data/test/data/ntfs.zip +0 -0
  48. data/test/data/oddExtraField.zip +0 -0
  49. data/test/data/rubycode.zip +0 -0
  50. data/test/data/rubycode2.zip +0 -0
  51. data/test/data/test.xls +0 -0
  52. data/test/data/testDirectory.bin +0 -0
  53. data/test/data/zip64-sample.zip +0 -0
  54. data/test/data/zipWithDirs.zip +0 -0
  55. data/test/data/zipWithEncryption.zip +0 -0
  56. data/test/deflater_test.rb +0 -65
  57. data/test/encryption_test.rb +0 -42
  58. data/test/entry_set_test.rb +0 -163
  59. data/test/entry_test.rb +0 -154
  60. data/test/errors_test.rb +0 -34
  61. data/test/extra_field_test.rb +0 -76
  62. data/test/file_extract_directory_test.rb +0 -54
  63. data/test/file_extract_test.rb +0 -83
  64. data/test/file_permissions_test.rb +0 -69
  65. data/test/file_split_test.rb +0 -57
  66. data/test/file_test.rb +0 -583
  67. data/test/filesystem/dir_iterator_test.rb +0 -58
  68. data/test/filesystem/directory_test.rb +0 -121
  69. data/test/filesystem/file_mutating_test.rb +0 -88
  70. data/test/filesystem/file_nonmutating_test.rb +0 -508
  71. data/test/filesystem/file_stat_test.rb +0 -64
  72. data/test/gentestfiles.rb +0 -126
  73. data/test/inflater_test.rb +0 -14
  74. data/test/input_stream_test.rb +0 -182
  75. data/test/ioextras/abstract_input_stream_test.rb +0 -102
  76. data/test/ioextras/abstract_output_stream_test.rb +0 -106
  77. data/test/ioextras/fake_io_test.rb +0 -18
  78. data/test/local_entry_test.rb +0 -154
  79. data/test/output_stream_test.rb +0 -128
  80. data/test/pass_thru_compressor_test.rb +0 -30
  81. data/test/pass_thru_decompressor_test.rb +0 -14
  82. data/test/samples/example_recursive_test.rb +0 -37
  83. data/test/settings_test.rb +0 -95
  84. data/test/test_helper.rb +0 -234
  85. data/test/unicode_file_names_and_comments_test.rb +0 -50
  86. data/test/zip64_full_test.rb +0 -51
  87. data/test/zip64_support_test.rb +0 -14
@@ -1,34 +0,0 @@
1
- # encoding: utf-8
2
- require 'test_helper'
3
-
4
- class ErrorsTest < MiniTest::Test
5
- def test_rescue_legacy_zip_error
6
- raise ::Zip::Error
7
- rescue ::Zip::ZipError
8
- end
9
-
10
- def test_rescue_legacy_zip_entry_exists_error
11
- raise ::Zip::EntryExistsError
12
- rescue ::Zip::ZipEntryExistsError
13
- end
14
-
15
- def test_rescue_legacy_zip_destination_file_exists_error
16
- raise ::Zip::DestinationFileExistsError
17
- rescue ::Zip::ZipDestinationFileExistsError
18
- end
19
-
20
- def test_rescue_legacy_zip_compression_method_error
21
- raise ::Zip::CompressionMethodError
22
- rescue ::Zip::ZipCompressionMethodError
23
- end
24
-
25
- def test_rescue_legacy_zip_entry_name_error
26
- raise ::Zip::EntryNameError
27
- rescue ::Zip::ZipEntryNameError
28
- end
29
-
30
- def test_rescue_legacy_zip_internal_error
31
- raise ::Zip::InternalError
32
- rescue ::Zip::ZipInternalError
33
- end
34
- end
@@ -1,76 +0,0 @@
1
- require 'test_helper'
2
-
3
- class ZipExtraFieldTest < MiniTest::Test
4
- def test_new
5
- extra_pure = ::Zip::ExtraField.new('')
6
- extra_withstr = ::Zip::ExtraField.new('foo')
7
- assert_instance_of(::Zip::ExtraField, extra_pure)
8
- assert_instance_of(::Zip::ExtraField, extra_withstr)
9
- end
10
-
11
- def test_unknownfield
12
- extra = ::Zip::ExtraField.new('foo')
13
- assert_equal(extra['Unknown'], 'foo')
14
- extra.merge('a')
15
- assert_equal(extra['Unknown'], 'fooa')
16
- extra.merge('barbaz')
17
- assert_equal(extra.to_s, 'fooabarbaz')
18
- end
19
-
20
- def test_ntfs
21
- str = "\x0A\x00 \x00\x00\x00\x00\x00\x01\x00\x18\x00\xC0\x81\x17\xE8B\xCE\xCF\x01\xC0\x81\x17\xE8B\xCE\xCF\x01\xC0\x81\x17\xE8B\xCE\xCF\x01"
22
- extra = ::Zip::ExtraField.new(str)
23
- assert(extra.member?('NTFS'))
24
- t = ::Zip::DOSTime.at(1_410_496_497.405178)
25
- assert_equal(t, extra['NTFS'].mtime)
26
- assert_equal(t, extra['NTFS'].atime)
27
- assert_equal(t, extra['NTFS'].ctime)
28
- end
29
-
30
- def test_merge
31
- str = "UT\x5\0\x3\250$\r@Ux\0\0"
32
- extra1 = ::Zip::ExtraField.new('')
33
- extra2 = ::Zip::ExtraField.new(str)
34
- assert(!extra1.member?('UniversalTime'))
35
- assert(extra2.member?('UniversalTime'))
36
- extra1.merge(str)
37
- assert_equal(extra1['UniversalTime'].mtime, extra2['UniversalTime'].mtime)
38
- end
39
-
40
- def test_length
41
- str = "UT\x5\0\x3\250$\r@Ux\0\0Te\0\0testit"
42
- extra = ::Zip::ExtraField.new(str)
43
- assert_equal(extra.local_size, extra.to_local_bin.size)
44
- assert_equal(extra.c_dir_size, extra.to_c_dir_bin.size)
45
- extra.merge('foo')
46
- assert_equal(extra.local_size, extra.to_local_bin.size)
47
- assert_equal(extra.c_dir_size, extra.to_c_dir_bin.size)
48
- end
49
-
50
- def test_to_s
51
- str = "UT\x5\0\x3\250$\r@Ux\0\0Te\0\0testit"
52
- extra = ::Zip::ExtraField.new(str)
53
- assert_instance_of(String, extra.to_s)
54
-
55
- s = extra.to_s
56
- extra.merge('foo')
57
- assert_equal(s.length + 3, extra.to_s.length)
58
- end
59
-
60
- def test_equality
61
- str = "UT\x5\0\x3\250$\r@"
62
- extra1 = ::Zip::ExtraField.new(str)
63
- extra2 = ::Zip::ExtraField.new(str)
64
- extra3 = ::Zip::ExtraField.new(str)
65
- assert_equal(extra1, extra2)
66
-
67
- extra2['UniversalTime'].mtime = ::Zip::DOSTime.now
68
- assert(extra1 != extra2)
69
-
70
- extra3.create('IUnix')
71
- assert(extra1 != extra3)
72
-
73
- extra1.create('IUnix')
74
- assert_equal(extra1, extra3)
75
- end
76
- end
@@ -1,54 +0,0 @@
1
- require 'test_helper'
2
-
3
- class ZipFileExtractDirectoryTest < MiniTest::Test
4
- include CommonZipFileFixture
5
-
6
- TEST_OUT_NAME = 'test/data/generated/emptyOutDir'
7
-
8
- def open_zip(&aProc)
9
- assert(!aProc.nil?)
10
- ::Zip::File.open(TestZipFile::TEST_ZIP4.zip_name, &aProc)
11
- end
12
-
13
- def extract_test_dir(&aProc)
14
- open_zip do |zf|
15
- zf.extract(TestFiles::EMPTY_TEST_DIR, TEST_OUT_NAME, &aProc)
16
- end
17
- end
18
-
19
- def setup
20
- super
21
-
22
- Dir.rmdir(TEST_OUT_NAME) if File.directory? TEST_OUT_NAME
23
- File.delete(TEST_OUT_NAME) if File.exist? TEST_OUT_NAME
24
- end
25
-
26
- def test_extract_directory
27
- extract_test_dir
28
- assert(File.directory?(TEST_OUT_NAME))
29
- end
30
-
31
- def test_extract_directory_exists_as_dir
32
- Dir.mkdir TEST_OUT_NAME
33
- extract_test_dir
34
- assert(File.directory?(TEST_OUT_NAME))
35
- end
36
-
37
- def test_extract_directory_exists_as_file
38
- File.open(TEST_OUT_NAME, 'w') { |f| f.puts 'something' }
39
- assert_raises(::Zip::DestinationFileExistsError) { extract_test_dir }
40
- end
41
-
42
- def test_extract_directory_exists_as_file_overwrite
43
- File.open(TEST_OUT_NAME, 'w') { |f| f.puts 'something' }
44
- gotCalled = false
45
- extract_test_dir do |entry, destPath|
46
- gotCalled = true
47
- assert_equal(TEST_OUT_NAME, destPath)
48
- assert(entry.directory?)
49
- true
50
- end
51
- assert(gotCalled)
52
- assert(File.directory?(TEST_OUT_NAME))
53
- end
54
- end
@@ -1,83 +0,0 @@
1
- require 'test_helper'
2
-
3
- class ZipFileExtractTest < MiniTest::Test
4
- include CommonZipFileFixture
5
- EXTRACTED_FILENAME = 'test/data/generated/extEntry'
6
- ENTRY_TO_EXTRACT, *REMAINING_ENTRIES = TEST_ZIP.entry_names.reverse
7
-
8
- def setup
9
- super
10
- ::File.delete(EXTRACTED_FILENAME) if ::File.exist?(EXTRACTED_FILENAME)
11
- end
12
-
13
- def test_extract
14
- ::Zip::File.open(TEST_ZIP.zip_name) do |zf|
15
- zf.extract(ENTRY_TO_EXTRACT, EXTRACTED_FILENAME)
16
-
17
- assert(File.exist?(EXTRACTED_FILENAME))
18
- AssertEntry.assert_contents(EXTRACTED_FILENAME,
19
- zf.get_input_stream(ENTRY_TO_EXTRACT) { |is| is.read })
20
-
21
- ::File.unlink(EXTRACTED_FILENAME)
22
-
23
- entry = zf.get_entry(ENTRY_TO_EXTRACT)
24
- entry.extract(EXTRACTED_FILENAME)
25
-
26
- assert(File.exist?(EXTRACTED_FILENAME))
27
- AssertEntry.assert_contents(EXTRACTED_FILENAME,
28
- entry.get_input_stream { |is| is.read })
29
- end
30
- end
31
-
32
- def test_extract_exists
33
- writtenText = 'written text'
34
- ::File.open(EXTRACTED_FILENAME, 'w') { |f| f.write(writtenText) }
35
-
36
- assert_raises(::Zip::DestinationFileExistsError) do
37
- ::Zip::File.open(TEST_ZIP.zip_name) do |zf|
38
- zf.extract(zf.entries.first, EXTRACTED_FILENAME)
39
- end
40
- end
41
- File.open(EXTRACTED_FILENAME, 'r') do |f|
42
- assert_equal(writtenText, f.read)
43
- end
44
- end
45
-
46
- def test_extract_exists_overwrite
47
- writtenText = 'written text'
48
- ::File.open(EXTRACTED_FILENAME, 'w') { |f| f.write(writtenText) }
49
-
50
- gotCalledCorrectly = false
51
- ::Zip::File.open(TEST_ZIP.zip_name) do |zf|
52
- zf.extract(zf.entries.first, EXTRACTED_FILENAME) do |entry, extractLoc|
53
- gotCalledCorrectly = zf.entries.first == entry &&
54
- extractLoc == EXTRACTED_FILENAME
55
- true
56
- end
57
- end
58
-
59
- assert(gotCalledCorrectly)
60
- ::File.open(EXTRACTED_FILENAME, 'r') do |f|
61
- assert(writtenText != f.read)
62
- end
63
- end
64
-
65
- def test_extract_non_entry
66
- zf = ::Zip::File.new(TEST_ZIP.zip_name)
67
- assert_raises(Errno::ENOENT) { zf.extract('nonExistingEntry', 'nonExistingEntry') }
68
- ensure
69
- zf.close if zf
70
- end
71
-
72
- def test_extract_non_entry_2
73
- outFile = 'outfile'
74
- assert_raises(Errno::ENOENT) do
75
- zf = ::Zip::File.new(TEST_ZIP.zip_name)
76
- nonEntry = 'hotdog-diddelidoo'
77
- assert(!zf.entries.include?(nonEntry))
78
- zf.extract(nonEntry, outFile)
79
- zf.close
80
- end
81
- assert(!File.exist?(outFile))
82
- end
83
- end
@@ -1,69 +0,0 @@
1
- require 'test_helper'
2
-
3
- class FilePermissionsTest < MiniTest::Test
4
-
5
- ZIPNAME = File.join(File.dirname(__FILE__), "umask.zip")
6
- FILENAME = File.join(File.dirname(__FILE__), "umask.txt")
7
-
8
- def teardown
9
- ::File.unlink(ZIPNAME)
10
- ::File.unlink(FILENAME)
11
- end
12
-
13
- def test_current_umask
14
- create_files
15
- assert_matching_permissions FILENAME, ZIPNAME
16
- end
17
-
18
- def test_umask_000
19
- set_umask(0000) do
20
- create_files
21
- end
22
-
23
- assert_matching_permissions FILENAME, ZIPNAME
24
- end
25
-
26
- def test_umask_066
27
- set_umask(0066) do
28
- create_files
29
- end
30
-
31
- assert_matching_permissions FILENAME, ZIPNAME
32
- end
33
-
34
- def test_umask_027
35
- set_umask(0027) do
36
- create_files
37
- end
38
-
39
- assert_matching_permissions FILENAME, ZIPNAME
40
- end
41
-
42
- def assert_matching_permissions(expected_file, actual_file)
43
- assert_equal(
44
- ::File.stat(expected_file).mode.to_s(8).rjust(4, '0'),
45
- ::File.stat(actual_file).mode.to_s(8).rjust(4, '0')
46
- )
47
- end
48
-
49
- def create_files
50
- ::Zip::File.open(ZIPNAME, ::Zip::File::CREATE) do |zip|
51
- zip.comment = "test"
52
- end
53
-
54
- ::File.open(FILENAME, 'w') do |file|
55
- file << 'test'
56
- end
57
- end
58
-
59
- # If anything goes wrong, make sure the umask is restored.
60
- def set_umask(umask, &block)
61
- begin
62
- saved_umask = ::File.umask(umask)
63
- yield
64
- ensure
65
- ::File.umask(saved_umask)
66
- end
67
- end
68
-
69
- end
@@ -1,57 +0,0 @@
1
- require 'test_helper'
2
-
3
- class ZipFileSplitTest < MiniTest::Test
4
- TEST_ZIP = TestZipFile::TEST_ZIP2.clone
5
- TEST_ZIP.zip_name = 'large_zip_file.zip'
6
- EXTRACTED_FILENAME = 'test/data/generated/extEntrySplit'
7
- UNSPLITTED_FILENAME = 'test/data/generated/unsplitted.zip'
8
- ENTRY_TO_EXTRACT = TEST_ZIP.entry_names.first
9
-
10
- def setup
11
- FileUtils.cp(TestZipFile::TEST_ZIP2.zip_name, TEST_ZIP.zip_name)
12
- end
13
-
14
- def teardown
15
- File.delete(TEST_ZIP.zip_name)
16
- File.delete(UNSPLITTED_FILENAME) if File.exist?(UNSPLITTED_FILENAME)
17
-
18
- Dir["#{TEST_ZIP.zip_name}.*"].each do |zip_file_name|
19
- File.delete(zip_file_name) if File.exist?(zip_file_name)
20
- end
21
- end
22
-
23
- def test_split_method_respond
24
- assert_respond_to ::Zip::File, :split, 'Does not have split class method'
25
- end
26
-
27
- def test_split
28
- result = ::Zip::File.split(TEST_ZIP.zip_name, 65_536, false)
29
-
30
- return if result.nil?
31
- Dir["#{TEST_ZIP.zip_name}.*"].sort.each_with_index do |zip_file_name, index|
32
- File.open(zip_file_name, 'rb') do |zip_file|
33
- zip_file.read([::Zip::File::SPLIT_SIGNATURE].pack('V').size) if index == 0
34
- File.open(UNSPLITTED_FILENAME, 'ab') do |file|
35
- file << zip_file.read
36
- end
37
- end
38
- end
39
-
40
- ::Zip::File.open(UNSPLITTED_FILENAME) do |zf|
41
- zf.extract(ENTRY_TO_EXTRACT, EXTRACTED_FILENAME)
42
-
43
- assert(File.exist?(EXTRACTED_FILENAME))
44
- AssertEntry.assert_contents(EXTRACTED_FILENAME,
45
- zf.get_input_stream(ENTRY_TO_EXTRACT) { |is| is.read })
46
-
47
- File.unlink(EXTRACTED_FILENAME)
48
-
49
- entry = zf.get_entry(ENTRY_TO_EXTRACT)
50
- entry.extract(EXTRACTED_FILENAME)
51
-
52
- assert(File.exist?(EXTRACTED_FILENAME))
53
- AssertEntry.assert_contents(EXTRACTED_FILENAME,
54
- entry.get_input_stream { |is| is.read })
55
- end
56
- end
57
- end
@@ -1,583 +0,0 @@
1
- require 'test_helper'
2
-
3
- class ZipFileTest < MiniTest::Test
4
- include CommonZipFileFixture
5
- include ZipEntryData
6
-
7
- OK_DELETE_FILE = 'test/data/generated/okToDelete.txt'
8
- OK_DELETE_MOVED_FILE = 'test/data/generated/okToDeleteMoved.txt'
9
-
10
- def teardown
11
- ::Zip.write_zip64_support = false
12
- end
13
-
14
- def test_create_from_scratch_to_buffer
15
- comment = 'a short comment'
16
-
17
- buffer = ::Zip::File.add_buffer do |zf|
18
- zf.get_output_stream('myFile') { |os| os.write 'myFile contains just this' }
19
- zf.mkdir('dir1')
20
- zf.comment = comment
21
- end
22
-
23
- ::File.open(EMPTY_FILENAME, 'wb') { |file| file.write buffer.string }
24
-
25
- zfRead = ::Zip::File.new(EMPTY_FILENAME)
26
- assert_equal(comment, zfRead.comment)
27
- assert_equal(2, zfRead.entries.length)
28
- end
29
-
30
- def test_create_from_scratch
31
- comment = 'a short comment'
32
-
33
- zf = ::Zip::File.new(EMPTY_FILENAME, ::Zip::File::CREATE)
34
- zf.get_output_stream('myFile') { |os| os.write 'myFile contains just this' }
35
- zf.mkdir('dir1')
36
- zf.comment = comment
37
- zf.close
38
-
39
- zfRead = ::Zip::File.new(EMPTY_FILENAME)
40
- assert_equal(comment, zfRead.comment)
41
- assert_equal(2, zfRead.entries.length)
42
- end
43
-
44
- def test_create_from_scratch_with_old_create_parameter
45
- comment = 'a short comment'
46
-
47
- zf = ::Zip::File.new(EMPTY_FILENAME, 1)
48
- zf.get_output_stream('myFile') { |os| os.write 'myFile contains just this' }
49
- zf.mkdir('dir1')
50
- zf.comment = comment
51
- zf.close
52
-
53
- zfRead = ::Zip::File.new(EMPTY_FILENAME)
54
- assert_equal(comment, zfRead.comment)
55
- assert_equal(2, zfRead.entries.length)
56
- end
57
-
58
- def test_get_output_stream
59
- entryCount = nil
60
- ::Zip::File.open(TEST_ZIP.zip_name) do |zf|
61
- entryCount = zf.size
62
- zf.get_output_stream('newEntry.txt') do |os|
63
- os.write 'Putting stuff in newEntry.txt'
64
- end
65
- assert_equal(entryCount + 1, zf.size)
66
- assert_equal('Putting stuff in newEntry.txt', zf.read('newEntry.txt'))
67
-
68
- zf.get_output_stream(zf.get_entry('test/data/generated/empty.txt')) do |os|
69
- os.write 'Putting stuff in data/generated/empty.txt'
70
- end
71
- assert_equal(entryCount + 1, zf.size)
72
- assert_equal('Putting stuff in data/generated/empty.txt', zf.read('test/data/generated/empty.txt'))
73
-
74
- custom_entry_args = [TEST_COMMENT, TEST_EXTRA, TEST_COMPRESSED_SIZE, TEST_CRC, ::Zip::Entry::STORED, TEST_SIZE, TEST_TIME]
75
- zf.get_output_stream('entry_with_custom_args.txt', nil, *custom_entry_args) do |os|
76
- os.write 'Some data'
77
- end
78
- assert_equal(entryCount + 2, zf.size)
79
- entry = zf.get_entry('entry_with_custom_args.txt')
80
- assert_equal(custom_entry_args[0], entry.comment)
81
- assert_equal(custom_entry_args[2], entry.compressed_size)
82
- assert_equal(custom_entry_args[3], entry.crc)
83
- assert_equal(custom_entry_args[4], entry.compression_method)
84
- assert_equal(custom_entry_args[5], entry.size)
85
- assert_equal(custom_entry_args[6], entry.time)
86
-
87
- zf.get_output_stream('entry.bin') do |os|
88
- os.write(::File.open('test/data/generated/5entry.zip', 'rb').read)
89
- end
90
- end
91
-
92
- ::Zip::File.open(TEST_ZIP.zip_name) do |zf|
93
- assert_equal(entryCount + 3, zf.size)
94
- assert_equal('Putting stuff in newEntry.txt', zf.read('newEntry.txt'))
95
- assert_equal('Putting stuff in data/generated/empty.txt', zf.read('test/data/generated/empty.txt'))
96
- assert_equal(File.open('test/data/generated/5entry.zip', 'rb').read, zf.read('entry.bin'))
97
- end
98
- end
99
-
100
- def test_open_buffer_with_stringio
101
- string_io = StringIO.new File.read('test/data/rubycode.zip')
102
- ::Zip::File.open_buffer string_io do |zf|
103
- assert zf.entries.map { |e| e.name }.include?('zippedruby1.rb')
104
- end
105
- end
106
-
107
- def test_open_buffer_without_block
108
- string_io = StringIO.new File.read('test/data/rubycode.zip')
109
- zf = ::Zip::File.open_buffer string_io
110
- assert zf.entries.map { |e| e.name }.include?('zippedruby1.rb')
111
- end
112
-
113
- def test_cleans_up_tempfiles_after_close
114
- zf = ::Zip::File.new(EMPTY_FILENAME, ::Zip::File::CREATE)
115
- zf.get_output_stream('myFile') do |os|
116
- @tempfile_path = os.path
117
- os.write 'myFile contains just this'
118
- end
119
-
120
- assert_equal(true, File.exist?(@tempfile_path))
121
-
122
- zf.close
123
-
124
- assert_equal(false, File.exist?(@tempfile_path))
125
- end
126
-
127
- def test_add
128
- srcFile = 'test/data/file2.txt'
129
- entryName = 'newEntryName.rb'
130
- assert(::File.exist?(srcFile))
131
- zf = ::Zip::File.new(EMPTY_FILENAME, ::Zip::File::CREATE)
132
- zf.add(entryName, srcFile)
133
- zf.close
134
-
135
- zfRead = ::Zip::File.new(EMPTY_FILENAME)
136
- assert_equal('', zfRead.comment)
137
- assert_equal(1, zfRead.entries.length)
138
- assert_equal(entryName, zfRead.entries.first.name)
139
- AssertEntry.assert_contents(srcFile,
140
- zfRead.get_input_stream(entryName) { |zis| zis.read })
141
- end
142
-
143
- def test_recover_permissions_after_add_files_to_archive
144
- srcZip = TEST_ZIP.zip_name
145
- ::File.chmod(0664, srcZip)
146
- srcFile = 'test/data/file2.txt'
147
- entryName = 'newEntryName.rb'
148
- assert_equal(::File.stat(srcZip).mode, 0100664)
149
- assert(::File.exist?(srcZip))
150
- zf = ::Zip::File.new(srcZip, ::Zip::File::CREATE)
151
- zf.add(entryName, srcFile)
152
- zf.close
153
- assert_equal(::File.stat(srcZip).mode, 0100664)
154
- end
155
-
156
- def test_add_existing_entry_name
157
- assert_raises(::Zip::EntryExistsError) do
158
- ::Zip::File.open(TEST_ZIP.zip_name) do |zf|
159
- zf.add(zf.entries.first.name, 'test/data/file2.txt')
160
- end
161
- end
162
- end
163
-
164
- def test_add_existing_entry_name_replace
165
- gotCalled = false
166
- replacedEntry = nil
167
- ::Zip::File.open(TEST_ZIP.zip_name) do |zf|
168
- replacedEntry = zf.entries.first.name
169
- zf.add(replacedEntry, 'test/data/file2.txt') { gotCalled = true; true }
170
- end
171
- assert(gotCalled)
172
- ::Zip::File.open(TEST_ZIP.zip_name) do |zf|
173
- assert_contains(zf, replacedEntry, 'test/data/file2.txt')
174
- end
175
- end
176
-
177
- def test_add_directory
178
- ::Zip::File.open(TEST_ZIP.zip_name) do |zf|
179
- zf.add(TestFiles::EMPTY_TEST_DIR, TestFiles::EMPTY_TEST_DIR)
180
- end
181
- ::Zip::File.open(TEST_ZIP.zip_name) do |zf|
182
- dirEntry = zf.entries.detect { |e| e.name == TestFiles::EMPTY_TEST_DIR + '/' }
183
- assert(dirEntry.directory?)
184
- end
185
- end
186
-
187
- def test_remove
188
- entryToRemove, *remainingEntries = TEST_ZIP.entry_names
189
-
190
- FileUtils.cp(TestZipFile::TEST_ZIP2.zip_name, TEST_ZIP.zip_name)
191
-
192
- zf = ::Zip::File.new(TEST_ZIP.zip_name)
193
- assert(zf.entries.map { |e| e.name }.include?(entryToRemove))
194
- zf.remove(entryToRemove)
195
- assert(!zf.entries.map { |e| e.name }.include?(entryToRemove))
196
- assert_equal(zf.entries.map { |x| x.name }.sort, remainingEntries.sort)
197
- zf.close
198
-
199
- zfRead = ::Zip::File.new(TEST_ZIP.zip_name)
200
- assert(!zfRead.entries.map { |e| e.name }.include?(entryToRemove))
201
- assert_equal(zfRead.entries.map { |x| x.name }.sort, remainingEntries.sort)
202
- zfRead.close
203
- end
204
-
205
- def test_rename
206
- entryToRename, * = TEST_ZIP.entry_names
207
-
208
- zf = ::Zip::File.new(TEST_ZIP.zip_name)
209
- assert(zf.entries.map { |e| e.name }.include?(entryToRename))
210
-
211
- contents = zf.read(entryToRename)
212
- newName = 'changed entry name'
213
- assert(!zf.entries.map { |e| e.name }.include?(newName))
214
-
215
- zf.rename(entryToRename, newName)
216
- assert(zf.entries.map { |e| e.name }.include?(newName))
217
-
218
- assert_equal(contents, zf.read(newName))
219
-
220
- zf.close
221
-
222
- zfRead = ::Zip::File.new(TEST_ZIP.zip_name)
223
- assert(zfRead.entries.map { |e| e.name }.include?(newName))
224
- assert_equal(contents, zfRead.read(newName))
225
- zfRead.close
226
- end
227
-
228
- def test_rename_with_each
229
- zf_name = 'test_rename_zip.zip'
230
- ::File.unlink(zf_name) if ::File.exist?(zf_name)
231
- arr = []
232
- arr_renamed = []
233
- ::Zip::File.open(zf_name, ::Zip::File::CREATE) do |zf|
234
- zf.mkdir('test')
235
- arr << 'test/'
236
- arr_renamed << 'Ztest/'
237
- %w(a b c d).each do |f|
238
- zf.get_output_stream("test/#{f}") { |file| file.puts 'aaaa' }
239
- arr << "test/#{f}"
240
- arr_renamed << "Ztest/#{f}"
241
- end
242
- end
243
- zf = ::Zip::File.open(zf_name)
244
- assert_equal(zf.entries.map(&:name), arr)
245
- zf.close
246
- Zip::File.open(zf_name, 'wb') do |z|
247
- z.each do |f|
248
- z.rename(f, "Z#{f.name}")
249
- end
250
- end
251
- zf = ::Zip::File.open(zf_name)
252
- assert_equal(zf.entries.map(&:name), arr_renamed)
253
- zf.close
254
- ::File.unlink(zf_name) if ::File.exist?(zf_name)
255
- end
256
-
257
- def test_rename_to_existing_entry
258
- oldEntries = nil
259
- ::Zip::File.open(TEST_ZIP.zip_name) { |zf| oldEntries = zf.entries }
260
-
261
- assert_raises(::Zip::EntryExistsError) do
262
- ::Zip::File.open(TEST_ZIP.zip_name) do |zf|
263
- zf.rename(zf.entries[0], zf.entries[1].name)
264
- end
265
- end
266
-
267
- ::Zip::File.open(TEST_ZIP.zip_name) do |zf|
268
- assert_equal(oldEntries.sort.map { |e| e.name }, zf.entries.sort.map { |e| e.name })
269
- end
270
- end
271
-
272
- def test_rename_to_existing_entry_overwrite
273
- oldEntries = nil
274
- ::Zip::File.open(TEST_ZIP.zip_name) { |zf| oldEntries = zf.entries }
275
-
276
- gotCalled = false
277
- renamedEntryName = nil
278
- ::Zip::File.open(TEST_ZIP.zip_name) do |zf|
279
- renamedEntryName = zf.entries[0].name
280
- zf.rename(zf.entries[0], zf.entries[1].name) { gotCalled = true; true }
281
- end
282
-
283
- assert(gotCalled)
284
- oldEntries.delete_if { |e| e.name == renamedEntryName }
285
- ::Zip::File.open(TEST_ZIP.zip_name) do |zf|
286
- assert_equal(oldEntries.sort.map { |e| e.name },
287
- zf.entries.sort.map { |e| e.name })
288
- end
289
- end
290
-
291
- def test_rename_non_entry
292
- nonEntry = 'bogusEntry'
293
- target_entry = 'target_entryName'
294
- zf = ::Zip::File.new(TEST_ZIP.zip_name)
295
- assert(!zf.entries.include?(nonEntry))
296
- assert_raises(Errno::ENOENT) { zf.rename(nonEntry, target_entry) }
297
- zf.commit
298
- assert(!zf.entries.include?(target_entry))
299
- ensure
300
- zf.close
301
- end
302
-
303
- def test_rename_entry_to_existing_entry
304
- entry1, entry2, * = TEST_ZIP.entry_names
305
- zf = ::Zip::File.new(TEST_ZIP.zip_name)
306
- assert_raises(::Zip::EntryExistsError) { zf.rename(entry1, entry2) }
307
- ensure
308
- zf.close
309
- end
310
-
311
- def test_replace
312
- entryToReplace = TEST_ZIP.entry_names[2]
313
- newEntrySrcFilename = 'test/data/file2.txt'
314
- zf = ::Zip::File.new(TEST_ZIP.zip_name)
315
- zf.replace(entryToReplace, newEntrySrcFilename)
316
-
317
- zf.close
318
- zfRead = ::Zip::File.new(TEST_ZIP.zip_name)
319
- AssertEntry.assert_contents(newEntrySrcFilename,
320
- zfRead.get_input_stream(entryToReplace) { |is| is.read })
321
- AssertEntry.assert_contents(TEST_ZIP.entry_names[0],
322
- zfRead.get_input_stream(TEST_ZIP.entry_names[0]) { |is| is.read })
323
- AssertEntry.assert_contents(TEST_ZIP.entry_names[1],
324
- zfRead.get_input_stream(TEST_ZIP.entry_names[1]) { |is| is.read })
325
- AssertEntry.assert_contents(TEST_ZIP.entry_names[3],
326
- zfRead.get_input_stream(TEST_ZIP.entry_names[3]) { |is| is.read })
327
- zfRead.close
328
- end
329
-
330
- def test_replace_non_entry
331
- entryToReplace = 'nonExistingEntryname'
332
- ::Zip::File.open(TEST_ZIP.zip_name) do |zf|
333
- assert_raises(Errno::ENOENT) { zf.replace(entryToReplace, 'test/data/file2.txt') }
334
- end
335
- end
336
-
337
- def test_commit
338
- newName = 'renamedFirst'
339
- zf = ::Zip::File.new(TEST_ZIP.zip_name)
340
- oldName = zf.entries.first
341
- zf.rename(oldName, newName)
342
- zf.commit
343
-
344
- zfRead = ::Zip::File.new(TEST_ZIP.zip_name)
345
- assert(zfRead.entries.detect { |e| e.name == newName } != nil)
346
- assert(zfRead.entries.detect { |e| e.name == oldName }.nil?)
347
- zfRead.close
348
-
349
- zf.close
350
- res = system("unzip -t #{TEST_ZIP.zip_name}")
351
- assert_equal(res, true)
352
- end
353
-
354
- def test_double_commit(filename = 'test/data/generated/double_commit_test.zip')
355
- ::FileUtils.touch('test/data/generated/test_double_commit1.txt')
356
- ::FileUtils.touch('test/data/generated/test_double_commit2.txt')
357
- zf = ::Zip::File.open(filename, ::Zip::File::CREATE)
358
- zf.add('test1.txt', 'test/data/generated/test_double_commit1.txt')
359
- zf.commit
360
- zf.add('test2.txt', 'test/data/generated/test_double_commit2.txt')
361
- zf.commit
362
- zf.close
363
- zf2 = ::Zip::File.open(filename)
364
- assert(zf2.entries.detect { |e| e.name == 'test1.txt' } != nil)
365
- assert(zf2.entries.detect { |e| e.name == 'test2.txt' } != nil)
366
- res = system("unzip -t #{filename}")
367
- assert_equal(res, true)
368
- end
369
-
370
- def test_double_commit_zip64
371
- ::Zip.write_zip64_support = true
372
- test_double_commit('test/data/generated/double_commit_test64.zip')
373
- end
374
-
375
- def test_write_buffer
376
- newName = 'renamedFirst'
377
- zf = ::Zip::File.new(TEST_ZIP.zip_name)
378
- oldName = zf.entries.first
379
- zf.rename(oldName, newName)
380
- io = ::StringIO.new('')
381
- buffer = zf.write_buffer(io)
382
- File.open(TEST_ZIP.zip_name, 'wb') { |f| f.write buffer.string }
383
- zfRead = ::Zip::File.new(TEST_ZIP.zip_name)
384
- assert(zfRead.entries.detect { |e| e.name == newName } != nil)
385
- assert(zfRead.entries.detect { |e| e.name == oldName }.nil?)
386
- zfRead.close
387
-
388
- zf.close
389
- end
390
-
391
- # This test tests that after commit, you
392
- # can delete the file you used to add the entry to the zip file
393
- # with
394
- def test_commit_use_zip_entry
395
- FileUtils.cp(TestFiles::RANDOM_ASCII_FILE1, OK_DELETE_FILE)
396
- zf = ::Zip::File.open(TEST_ZIP.zip_name)
397
- zf.add('okToDelete.txt', OK_DELETE_FILE)
398
- assert_contains(zf, 'okToDelete.txt')
399
- zf.commit
400
- File.rename(OK_DELETE_FILE, OK_DELETE_MOVED_FILE)
401
- assert_contains(zf, 'okToDelete.txt', OK_DELETE_MOVED_FILE)
402
- end
403
-
404
- # def test_close
405
- # zf = ZipFile.new(TEST_ZIP.zip_name)
406
- # zf.close
407
- # assert_raises(IOError) {
408
- # zf.extract(TEST_ZIP.entry_names.first, "hullubullu")
409
- # }
410
- # end
411
-
412
- def test_compound1
413
- renamedName = 'renamedName'
414
- filename_to_remove = ''
415
- begin
416
- zf = ::Zip::File.new(TEST_ZIP.zip_name)
417
- originalEntries = zf.entries.dup
418
-
419
- assert_not_contains(zf, TestFiles::RANDOM_ASCII_FILE1)
420
- zf.add(TestFiles::RANDOM_ASCII_FILE1,
421
- TestFiles::RANDOM_ASCII_FILE1)
422
- assert_contains(zf, TestFiles::RANDOM_ASCII_FILE1)
423
-
424
- entry_to_rename = zf.entries.find { |entry| entry.name.match('longAscii') }
425
- zf.rename(entry_to_rename, renamedName)
426
- assert_contains(zf, renamedName)
427
-
428
- TestFiles::BINARY_TEST_FILES.each do |filename|
429
- zf.add(filename, filename)
430
- assert_contains(zf, filename)
431
- end
432
-
433
- assert_contains(zf, originalEntries.last.to_s)
434
- filename_to_remove = originalEntries.map(&:to_s).find { |name| name.match('longBinary') }
435
- zf.remove(filename_to_remove)
436
- assert_not_contains(zf, filename_to_remove)
437
-
438
- ensure
439
- zf.close
440
- end
441
- begin
442
- zfRead = ::Zip::File.new(TEST_ZIP.zip_name)
443
- assert_contains(zfRead, TestFiles::RANDOM_ASCII_FILE1)
444
- assert_contains(zfRead, renamedName)
445
- TestFiles::BINARY_TEST_FILES.each do |filename|
446
- assert_contains(zfRead, filename)
447
- end
448
- assert_not_contains(zfRead, filename_to_remove)
449
- ensure
450
- zfRead.close
451
- end
452
- end
453
-
454
- def test_compound2
455
- begin
456
- zf = ::Zip::File.new(TEST_ZIP.zip_name)
457
- originalEntries = zf.entries.dup
458
-
459
- originalEntries.each do |entry|
460
- zf.remove(entry)
461
- assert_not_contains(zf, entry)
462
- end
463
- assert(zf.entries.empty?)
464
-
465
- TestFiles::ASCII_TEST_FILES.each do |filename|
466
- zf.add(filename, filename)
467
- assert_contains(zf, filename)
468
- end
469
- assert_equal(zf.entries.sort.map { |e| e.name }, TestFiles::ASCII_TEST_FILES)
470
-
471
- zf.rename(TestFiles::ASCII_TEST_FILES[0], 'newName')
472
- assert_not_contains(zf, TestFiles::ASCII_TEST_FILES[0])
473
- assert_contains(zf, 'newName')
474
- ensure
475
- zf.close
476
- end
477
- begin
478
- zfRead = ::Zip::File.new(TEST_ZIP.zip_name)
479
- asciiTestFiles = TestFiles::ASCII_TEST_FILES.dup
480
- asciiTestFiles.shift
481
- asciiTestFiles.each do |filename|
482
- assert_contains(zf, filename)
483
- end
484
-
485
- assert_contains(zf, 'newName')
486
- ensure
487
- zfRead.close
488
- end
489
- end
490
-
491
- def test_change_comment
492
- ::Zip::File.open(TEST_ZIP.zip_name) do |zf|
493
- zf.comment = 'my changed comment'
494
- end
495
- zfRead = ::Zip::File.open(TEST_ZIP.zip_name)
496
- assert_equal('my changed comment', zfRead.comment)
497
- end
498
-
499
- def test_preserve_file_order
500
- entryNames = nil
501
- ::Zip::File.open(TEST_ZIP.zip_name) do |zf|
502
- entryNames = zf.entries.map { |e| e.to_s }
503
- zf.get_output_stream('a.txt') { |os| os.write 'this is a.txt' }
504
- zf.get_output_stream('z.txt') { |os| os.write 'this is z.txt' }
505
- zf.get_output_stream('k.txt') { |os| os.write 'this is k.txt' }
506
- entryNames << 'a.txt' << 'z.txt' << 'k.txt'
507
- end
508
-
509
- ::Zip::File.open(TEST_ZIP.zip_name) do |zf|
510
- assert_equal(entryNames, zf.entries.map { |e| e.to_s })
511
- entries = zf.entries.sort_by { |e| e.name }.reverse
512
- entries.each do |e|
513
- zf.remove e
514
- zf.get_output_stream(e) { |os| os.write 'foo' }
515
- end
516
- entryNames = entries.map { |e| e.to_s }
517
- end
518
- ::Zip::File.open(TEST_ZIP.zip_name) do |zf|
519
- assert_equal(entryNames, zf.entries.map { |e| e.to_s })
520
- end
521
- end
522
-
523
- def test_streaming
524
- fname = ::File.join(::File.expand_path(::File.dirname(__FILE__)), '../README.md')
525
- zname = 'test/data/generated/README.zip'
526
- Zip::File.open(zname, Zip::File::CREATE) do |zipfile|
527
- zipfile.get_output_stream(File.basename(fname)) do |f|
528
- f.puts File.read(fname)
529
- end
530
- end
531
-
532
- data = nil
533
- File.open(zname, 'rb') do |f|
534
- Zip::File.open_buffer(f) do |zipfile|
535
- zipfile.each do |entry|
536
- next unless entry.name =~ /README.md/
537
- data = zipfile.read(entry)
538
- end
539
- end
540
- end
541
- assert data
542
- assert data =~ /Simonov/
543
- end
544
-
545
- def test_nonexistant_zip
546
- assert_raises(::Zip::Error) do
547
- ::Zip::File.open('fake.zip')
548
- end
549
- end
550
-
551
- def test_empty_zip
552
- assert_raises(::Zip::Error) do
553
- ::Zip::File.open(TestFiles::NULL_FILE)
554
- end
555
- end
556
-
557
- def test_odd_extra_field
558
- entry_count = 0
559
- File.open 'test/data/oddExtraField.zip', 'rb' do |zip_io|
560
- Zip::File.open_buffer zip_io.read do |zip|
561
- zip.each do |zip_entry|
562
- entry_count += 1
563
- end
564
- end
565
- end
566
- assert_equal 13, entry_count
567
- end
568
-
569
- def test_open_xls_does_not_raise_type_error
570
- ::Zip::File.open('test/data/test.xls')
571
- end
572
-
573
- private
574
-
575
- def assert_contains(zf, entryName, filename = entryName)
576
- assert(zf.entries.detect { |e| e.name == entryName } != nil, "entry #{entryName} not in #{zf.entries.join(', ')} in zip file #{zf}")
577
- assert_entry_contents(zf, entryName, filename) if File.exist?(filename)
578
- end
579
-
580
- def assert_not_contains(zf, entryName)
581
- assert(zf.entries.detect { |e| e.name == entryName }.nil?, "entry #{entryName} in #{zf.entries.join(', ')} in zip file #{zf}")
582
- end
583
- end