rubyzip 1.1.7 → 1.2.1
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of rubyzip might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/README.md +75 -40
- data/Rakefile +3 -4
- data/lib/zip.rb +3 -5
- data/lib/zip/central_directory.rb +7 -7
- data/lib/zip/compressor.rb +0 -0
- data/lib/zip/constants.rb +2 -2
- data/lib/zip/crypto/null_encryption.rb +3 -3
- data/lib/zip/crypto/traditional_encryption.rb +5 -5
- data/lib/zip/decompressor.rb +2 -2
- data/lib/zip/deflater.rb +8 -6
- data/lib/zip/dos_time.rb +4 -5
- data/lib/zip/entry.rb +75 -81
- data/lib/zip/entry_set.rb +11 -11
- data/lib/zip/errors.rb +1 -0
- data/lib/zip/extra_field.rb +6 -6
- data/lib/zip/extra_field/generic.rb +7 -7
- data/lib/zip/extra_field/ntfs.rb +14 -16
- data/lib/zip/extra_field/old_unix.rb +9 -10
- data/lib/zip/extra_field/universal_time.rb +14 -14
- data/lib/zip/extra_field/unix.rb +8 -9
- data/lib/zip/extra_field/zip64.rb +12 -11
- data/lib/zip/extra_field/zip64_placeholder.rb +1 -1
- data/lib/zip/file.rb +47 -60
- data/lib/zip/filesystem.rb +132 -135
- data/lib/zip/inflater.rb +3 -3
- data/lib/zip/input_stream.rb +13 -7
- data/lib/zip/ioextras.rb +1 -3
- data/lib/zip/ioextras/abstract_input_stream.rb +5 -9
- data/lib/zip/ioextras/abstract_output_stream.rb +2 -4
- data/lib/zip/null_compressor.rb +2 -2
- data/lib/zip/null_decompressor.rb +3 -3
- data/lib/zip/null_input_stream.rb +0 -0
- data/lib/zip/output_stream.rb +8 -9
- data/lib/zip/pass_thru_compressor.rb +4 -4
- data/lib/zip/pass_thru_decompressor.rb +2 -3
- data/lib/zip/streamable_directory.rb +2 -2
- data/lib/zip/streamable_stream.rb +2 -2
- data/lib/zip/version.rb +1 -1
- data/samples/example.rb +29 -39
- data/samples/example_filesystem.rb +16 -18
- data/samples/example_recursive.rb +32 -25
- data/samples/{gtkRubyzip.rb → gtk_ruby_zip.rb} +23 -25
- data/samples/qtzip.rb +17 -26
- data/samples/write_simple.rb +12 -13
- data/samples/zipfind.rb +24 -32
- data/test/basic_zip_file_test.rb +11 -15
- data/test/case_sensitivity_test.rb +69 -0
- data/test/central_directory_entry_test.rb +32 -36
- data/test/central_directory_test.rb +46 -50
- data/test/crypto/null_encryption_test.rb +8 -4
- data/test/crypto/traditional_encryption_test.rb +5 -5
- data/test/data/notzippedruby.rb +1 -1
- data/test/data/oddExtraField.zip +0 -0
- data/test/data/test.xls +0 -0
- data/test/deflater_test.rb +10 -12
- data/test/encryption_test.rb +2 -2
- data/test/entry_set_test.rb +50 -25
- data/test/entry_test.rb +76 -87
- data/test/errors_test.rb +0 -2
- data/test/extra_field_test.rb +19 -21
- data/test/file_extract_directory_test.rb +12 -14
- data/test/file_extract_test.rb +33 -40
- data/test/file_permissions_test.rb +69 -0
- data/test/file_split_test.rb +24 -27
- data/test/file_test.rb +196 -172
- data/test/filesystem/dir_iterator_test.rb +13 -17
- data/test/filesystem/directory_test.rb +80 -90
- data/test/filesystem/file_mutating_test.rb +51 -63
- data/test/filesystem/file_nonmutating_test.rb +222 -228
- data/test/filesystem/file_stat_test.rb +17 -19
- data/test/gentestfiles.rb +47 -55
- data/test/inflater_test.rb +1 -1
- data/test/input_stream_test.rb +46 -34
- data/test/ioextras/abstract_input_stream_test.rb +22 -23
- data/test/ioextras/abstract_output_stream_test.rb +32 -32
- data/test/ioextras/fake_io_test.rb +1 -1
- data/test/local_entry_test.rb +36 -38
- data/test/output_stream_test.rb +20 -21
- data/test/pass_thru_compressor_test.rb +5 -6
- data/test/pass_thru_decompressor_test.rb +0 -1
- data/test/samples/example_recursive_test.rb +37 -0
- data/test/settings_test.rb +18 -15
- data/test/test_helper.rb +50 -44
- data/test/unicode_file_names_and_comments_test.rb +5 -7
- data/test/zip64_full_test.rb +9 -11
- data/test/zip64_support_test.rb +0 -1
- metadata +14 -32
@@ -3,18 +3,17 @@ require 'test_helper'
|
|
3
3
|
class ZipFileExtractDirectoryTest < MiniTest::Test
|
4
4
|
include CommonZipFileFixture
|
5
5
|
|
6
|
-
TEST_OUT_NAME =
|
6
|
+
TEST_OUT_NAME = 'test/data/generated/emptyOutDir'
|
7
7
|
|
8
8
|
def open_zip(&aProc)
|
9
|
-
assert(aProc
|
9
|
+
assert(!aProc.nil?)
|
10
10
|
::Zip::File.open(TestZipFile::TEST_ZIP4.zip_name, &aProc)
|
11
11
|
end
|
12
12
|
|
13
13
|
def extract_test_dir(&aProc)
|
14
|
-
open_zip
|
15
|
-
|zf|
|
14
|
+
open_zip do |zf|
|
16
15
|
zf.extract(TestFiles::EMPTY_TEST_DIR, TEST_OUT_NAME, &aProc)
|
17
|
-
|
16
|
+
end
|
18
17
|
end
|
19
18
|
|
20
19
|
def setup
|
@@ -24,32 +23,31 @@ class ZipFileExtractDirectoryTest < MiniTest::Test
|
|
24
23
|
File.delete(TEST_OUT_NAME) if File.exist? TEST_OUT_NAME
|
25
24
|
end
|
26
25
|
|
27
|
-
def
|
26
|
+
def test_extract_directory
|
28
27
|
extract_test_dir
|
29
28
|
assert(File.directory?(TEST_OUT_NAME))
|
30
29
|
end
|
31
30
|
|
32
|
-
def
|
31
|
+
def test_extract_directory_exists_as_dir
|
33
32
|
Dir.mkdir TEST_OUT_NAME
|
34
33
|
extract_test_dir
|
35
34
|
assert(File.directory?(TEST_OUT_NAME))
|
36
35
|
end
|
37
36
|
|
38
|
-
def
|
39
|
-
File.open(TEST_OUT_NAME,
|
37
|
+
def test_extract_directory_exists_as_file
|
38
|
+
File.open(TEST_OUT_NAME, 'w') { |f| f.puts 'something' }
|
40
39
|
assert_raises(::Zip::DestinationFileExistsError) { extract_test_dir }
|
41
40
|
end
|
42
41
|
|
43
|
-
def
|
44
|
-
File.open(TEST_OUT_NAME,
|
42
|
+
def test_extract_directory_exists_as_file_overwrite
|
43
|
+
File.open(TEST_OUT_NAME, 'w') { |f| f.puts 'something' }
|
45
44
|
gotCalled = false
|
46
|
-
extract_test_dir
|
47
|
-
|entry, destPath|
|
45
|
+
extract_test_dir do |entry, destPath|
|
48
46
|
gotCalled = true
|
49
47
|
assert_equal(TEST_OUT_NAME, destPath)
|
50
48
|
assert(entry.directory?)
|
51
49
|
true
|
52
|
-
|
50
|
+
end
|
53
51
|
assert(gotCalled)
|
54
52
|
assert(File.directory?(TEST_OUT_NAME))
|
55
53
|
end
|
data/test/file_extract_test.rb
CHANGED
@@ -2,7 +2,7 @@ require 'test_helper'
|
|
2
2
|
|
3
3
|
class ZipFileExtractTest < MiniTest::Test
|
4
4
|
include CommonZipFileFixture
|
5
|
-
EXTRACTED_FILENAME =
|
5
|
+
EXTRACTED_FILENAME = 'test/data/generated/extEntry'
|
6
6
|
ENTRY_TO_EXTRACT, *REMAINING_ENTRIES = TEST_ZIP.entry_names.reverse
|
7
7
|
|
8
8
|
def setup
|
@@ -11,14 +11,12 @@ class ZipFileExtractTest < MiniTest::Test
|
|
11
11
|
end
|
12
12
|
|
13
13
|
def test_extract
|
14
|
-
::Zip::File.open(TEST_ZIP.zip_name)
|
15
|
-
|zf|
|
14
|
+
::Zip::File.open(TEST_ZIP.zip_name) do |zf|
|
16
15
|
zf.extract(ENTRY_TO_EXTRACT, EXTRACTED_FILENAME)
|
17
16
|
|
18
17
|
assert(File.exist?(EXTRACTED_FILENAME))
|
19
|
-
AssertEntry
|
20
|
-
|
21
|
-
|
18
|
+
AssertEntry.assert_contents(EXTRACTED_FILENAME,
|
19
|
+
zf.get_input_stream(ENTRY_TO_EXTRACT) { |is| is.read })
|
22
20
|
|
23
21
|
::File.unlink(EXTRACTED_FILENAME)
|
24
22
|
|
@@ -26,65 +24,60 @@ class ZipFileExtractTest < MiniTest::Test
|
|
26
24
|
entry.extract(EXTRACTED_FILENAME)
|
27
25
|
|
28
26
|
assert(File.exist?(EXTRACTED_FILENAME))
|
29
|
-
AssertEntry
|
30
|
-
|
31
|
-
|
32
|
-
}
|
27
|
+
AssertEntry.assert_contents(EXTRACTED_FILENAME,
|
28
|
+
entry.get_input_stream { |is| is.read })
|
29
|
+
end
|
33
30
|
end
|
34
31
|
|
35
|
-
def
|
36
|
-
writtenText =
|
37
|
-
::File.open(EXTRACTED_FILENAME,
|
32
|
+
def test_extract_exists
|
33
|
+
writtenText = 'written text'
|
34
|
+
::File.open(EXTRACTED_FILENAME, 'w') { |f| f.write(writtenText) }
|
38
35
|
|
39
|
-
assert_raises(::Zip::DestinationFileExistsError)
|
40
|
-
::Zip::File.open(TEST_ZIP.zip_name)
|
36
|
+
assert_raises(::Zip::DestinationFileExistsError) do
|
37
|
+
::Zip::File.open(TEST_ZIP.zip_name) do |zf|
|
41
38
|
zf.extract(zf.entries.first, EXTRACTED_FILENAME)
|
42
|
-
|
43
|
-
|
44
|
-
File.open(EXTRACTED_FILENAME,
|
39
|
+
end
|
40
|
+
end
|
41
|
+
File.open(EXTRACTED_FILENAME, 'r') do |f|
|
45
42
|
assert_equal(writtenText, f.read)
|
46
|
-
|
43
|
+
end
|
47
44
|
end
|
48
45
|
|
49
|
-
def
|
50
|
-
writtenText =
|
51
|
-
::File.open(EXTRACTED_FILENAME,
|
46
|
+
def test_extract_exists_overwrite
|
47
|
+
writtenText = 'written text'
|
48
|
+
::File.open(EXTRACTED_FILENAME, 'w') { |f| f.write(writtenText) }
|
52
49
|
|
53
50
|
gotCalledCorrectly = false
|
54
|
-
::Zip::File.open(TEST_ZIP.zip_name)
|
55
|
-
|
56
|
-
zf.extract(zf.entries.first, EXTRACTED_FILENAME) {
|
57
|
-
|entry, extractLoc|
|
51
|
+
::Zip::File.open(TEST_ZIP.zip_name) do |zf|
|
52
|
+
zf.extract(zf.entries.first, EXTRACTED_FILENAME) do |entry, extractLoc|
|
58
53
|
gotCalledCorrectly = zf.entries.first == entry &&
|
59
|
-
|
54
|
+
extractLoc == EXTRACTED_FILENAME
|
60
55
|
true
|
61
|
-
|
62
|
-
|
56
|
+
end
|
57
|
+
end
|
63
58
|
|
64
59
|
assert(gotCalledCorrectly)
|
65
|
-
::File.open(EXTRACTED_FILENAME,
|
66
|
-
|f|
|
60
|
+
::File.open(EXTRACTED_FILENAME, 'r') do |f|
|
67
61
|
assert(writtenText != f.read)
|
68
|
-
|
62
|
+
end
|
69
63
|
end
|
70
64
|
|
71
|
-
def
|
65
|
+
def test_extract_non_entry
|
72
66
|
zf = ::Zip::File.new(TEST_ZIP.zip_name)
|
73
|
-
assert_raises(Errno::ENOENT) { zf.extract(
|
67
|
+
assert_raises(Errno::ENOENT) { zf.extract('nonExistingEntry', 'nonExistingEntry') }
|
74
68
|
ensure
|
75
69
|
zf.close if zf
|
76
70
|
end
|
77
71
|
|
78
|
-
def
|
79
|
-
outFile =
|
80
|
-
assert_raises(Errno::ENOENT)
|
72
|
+
def test_extract_non_entry_2
|
73
|
+
outFile = 'outfile'
|
74
|
+
assert_raises(Errno::ENOENT) do
|
81
75
|
zf = ::Zip::File.new(TEST_ZIP.zip_name)
|
82
|
-
nonEntry =
|
76
|
+
nonEntry = 'hotdog-diddelidoo'
|
83
77
|
assert(!zf.entries.include?(nonEntry))
|
84
78
|
zf.extract(nonEntry, outFile)
|
85
79
|
zf.close
|
86
|
-
|
80
|
+
end
|
87
81
|
assert(!File.exist?(outFile))
|
88
82
|
end
|
89
|
-
|
90
83
|
end
|
@@ -0,0 +1,69 @@
|
|
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
|
data/test/file_split_test.rb
CHANGED
@@ -2,9 +2,9 @@ require 'test_helper'
|
|
2
2
|
|
3
3
|
class ZipFileSplitTest < MiniTest::Test
|
4
4
|
TEST_ZIP = TestZipFile::TEST_ZIP2.clone
|
5
|
-
TEST_ZIP.zip_name =
|
6
|
-
EXTRACTED_FILENAME =
|
7
|
-
UNSPLITTED_FILENAME =
|
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
8
|
ENTRY_TO_EXTRACT = TEST_ZIP.entry_names.first
|
9
9
|
|
10
10
|
def setup
|
@@ -21,40 +21,37 @@ class ZipFileSplitTest < MiniTest::Test
|
|
21
21
|
end
|
22
22
|
|
23
23
|
def test_split_method_respond
|
24
|
-
assert_respond_to ::Zip::File, :split,
|
24
|
+
assert_respond_to ::Zip::File, :split, 'Does not have split class method'
|
25
25
|
end
|
26
26
|
|
27
27
|
def test_split
|
28
|
-
result = ::Zip::File.split(TEST_ZIP.zip_name,
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
end
|
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
|
37
36
|
end
|
38
37
|
end
|
38
|
+
end
|
39
39
|
|
40
|
-
|
41
|
-
|
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
|
-
|
40
|
+
::Zip::File.open(UNSPLITTED_FILENAME) do |zf|
|
41
|
+
zf.extract(ENTRY_TO_EXTRACT, EXTRACTED_FILENAME)
|
47
42
|
|
48
|
-
|
43
|
+
assert(File.exist?(EXTRACTED_FILENAME))
|
44
|
+
AssertEntry.assert_contents(EXTRACTED_FILENAME,
|
45
|
+
zf.get_input_stream(ENTRY_TO_EXTRACT) { |is| is.read })
|
49
46
|
|
50
|
-
|
51
|
-
entry.extract(EXTRACTED_FILENAME)
|
47
|
+
File.unlink(EXTRACTED_FILENAME)
|
52
48
|
|
53
|
-
|
54
|
-
|
55
|
-
entry.get_input_stream() { |is| is.read })
|
49
|
+
entry = zf.get_entry(ENTRY_TO_EXTRACT)
|
50
|
+
entry.extract(EXTRACTED_FILENAME)
|
56
51
|
|
57
|
-
|
52
|
+
assert(File.exist?(EXTRACTED_FILENAME))
|
53
|
+
AssertEntry.assert_contents(EXTRACTED_FILENAME,
|
54
|
+
entry.get_input_stream { |is| is.read })
|
58
55
|
end
|
59
56
|
end
|
60
57
|
end
|
data/test/file_test.rb
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
|
-
|
4
3
|
class ZipFileTest < MiniTest::Test
|
5
4
|
include CommonZipFileFixture
|
5
|
+
include ZipEntryData
|
6
6
|
|
7
7
|
OK_DELETE_FILE = 'test/data/generated/okToDelete.txt'
|
8
8
|
OK_DELETE_MOVED_FILE = 'test/data/generated/okToDeleteMoved.txt'
|
@@ -11,12 +11,12 @@ class ZipFileTest < MiniTest::Test
|
|
11
11
|
::Zip.write_zip64_support = false
|
12
12
|
end
|
13
13
|
|
14
|
-
def
|
15
|
-
comment =
|
14
|
+
def test_create_from_scratch_to_buffer
|
15
|
+
comment = 'a short comment'
|
16
16
|
|
17
17
|
buffer = ::Zip::File.add_buffer do |zf|
|
18
|
-
zf.get_output_stream(
|
19
|
-
zf.mkdir(
|
18
|
+
zf.get_output_stream('myFile') { |os| os.write 'myFile contains just this' }
|
19
|
+
zf.mkdir('dir1')
|
20
20
|
zf.comment = comment
|
21
21
|
end
|
22
22
|
|
@@ -27,12 +27,26 @@ class ZipFileTest < MiniTest::Test
|
|
27
27
|
assert_equal(2, zfRead.entries.length)
|
28
28
|
end
|
29
29
|
|
30
|
-
def
|
31
|
-
comment =
|
30
|
+
def test_create_from_scratch
|
31
|
+
comment = 'a short comment'
|
32
32
|
|
33
33
|
zf = ::Zip::File.new(EMPTY_FILENAME, ::Zip::File::CREATE)
|
34
|
-
zf.get_output_stream(
|
35
|
-
zf.mkdir(
|
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')
|
36
50
|
zf.comment = comment
|
37
51
|
zf.close
|
38
52
|
|
@@ -43,29 +57,25 @@ class ZipFileTest < MiniTest::Test
|
|
43
57
|
|
44
58
|
def test_get_output_stream
|
45
59
|
entryCount = nil
|
46
|
-
::Zip::File.open(TEST_ZIP.zip_name)
|
47
|
-
|zf|
|
60
|
+
::Zip::File.open(TEST_ZIP.zip_name) do |zf|
|
48
61
|
entryCount = zf.size
|
49
|
-
zf.get_output_stream('newEntry.txt')
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
assert_equal(
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
os.write "Some data"
|
67
|
-
}
|
68
|
-
assert_equal(entryCount+2, 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)
|
69
79
|
entry = zf.get_entry('entry_with_custom_args.txt')
|
70
80
|
assert_equal(custom_entry_args[0], entry.comment)
|
71
81
|
assert_equal(custom_entry_args[2], entry.compressed_size)
|
@@ -74,26 +84,37 @@ class ZipFileTest < MiniTest::Test
|
|
74
84
|
assert_equal(custom_entry_args[5], entry.size)
|
75
85
|
assert_equal(custom_entry_args[6], entry.time)
|
76
86
|
|
77
|
-
zf.get_output_stream('entry.bin')
|
78
|
-
|os|
|
87
|
+
zf.get_output_stream('entry.bin') do |os|
|
79
88
|
os.write(::File.open('test/data/generated/5entry.zip', 'rb').read)
|
80
|
-
|
81
|
-
|
89
|
+
end
|
90
|
+
end
|
82
91
|
|
83
|
-
::Zip::File.open(TEST_ZIP.zip_name)
|
84
|
-
|
85
|
-
assert_equal(
|
86
|
-
assert_equal(
|
87
|
-
assert_equal(
|
88
|
-
|
89
|
-
|
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')
|
90
111
|
end
|
91
112
|
|
92
113
|
def test_cleans_up_tempfiles_after_close
|
93
114
|
zf = ::Zip::File.new(EMPTY_FILENAME, ::Zip::File::CREATE)
|
94
|
-
zf.get_output_stream(
|
115
|
+
zf.get_output_stream('myFile') do |os|
|
95
116
|
@tempfile_path = os.path
|
96
|
-
os.write
|
117
|
+
os.write 'myFile contains just this'
|
97
118
|
end
|
98
119
|
|
99
120
|
assert_equal(true, File.exist?(@tempfile_path))
|
@@ -104,15 +125,15 @@ class ZipFileTest < MiniTest::Test
|
|
104
125
|
end
|
105
126
|
|
106
127
|
def test_add
|
107
|
-
srcFile =
|
108
|
-
entryName =
|
128
|
+
srcFile = 'test/data/file2.txt'
|
129
|
+
entryName = 'newEntryName.rb'
|
109
130
|
assert(::File.exist?(srcFile))
|
110
131
|
zf = ::Zip::File.new(EMPTY_FILENAME, ::Zip::File::CREATE)
|
111
132
|
zf.add(entryName, srcFile)
|
112
133
|
zf.close
|
113
134
|
|
114
135
|
zfRead = ::Zip::File.new(EMPTY_FILENAME)
|
115
|
-
assert_equal(
|
136
|
+
assert_equal('', zfRead.comment)
|
116
137
|
assert_equal(1, zfRead.entries.length)
|
117
138
|
assert_equal(entryName, zfRead.entries.first.name)
|
118
139
|
AssertEntry.assert_contents(srcFile,
|
@@ -122,8 +143,8 @@ class ZipFileTest < MiniTest::Test
|
|
122
143
|
def test_recover_permissions_after_add_files_to_archive
|
123
144
|
srcZip = TEST_ZIP.zip_name
|
124
145
|
::File.chmod(0664, srcZip)
|
125
|
-
srcFile =
|
126
|
-
entryName =
|
146
|
+
srcFile = 'test/data/file2.txt'
|
147
|
+
entryName = 'newEntryName.rb'
|
127
148
|
assert_equal(::File.stat(srcZip).mode, 0100664)
|
128
149
|
assert(::File.exist?(srcZip))
|
129
150
|
zf = ::Zip::File.new(srcZip, ::Zip::File::CREATE)
|
@@ -132,40 +153,35 @@ class ZipFileTest < MiniTest::Test
|
|
132
153
|
assert_equal(::File.stat(srcZip).mode, 0100664)
|
133
154
|
end
|
134
155
|
|
135
|
-
def
|
136
|
-
assert_raises(::Zip::EntryExistsError)
|
137
|
-
::Zip::File.open(TEST_ZIP.zip_name)
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
}
|
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
|
142
162
|
end
|
143
163
|
|
144
|
-
def
|
164
|
+
def test_add_existing_entry_name_replace
|
145
165
|
gotCalled = false
|
146
166
|
replacedEntry = nil
|
147
|
-
::Zip::File.open(TEST_ZIP.zip_name)
|
148
|
-
|zf|
|
167
|
+
::Zip::File.open(TEST_ZIP.zip_name) do |zf|
|
149
168
|
replacedEntry = zf.entries.first.name
|
150
|
-
zf.add(replacedEntry,
|
151
|
-
|
169
|
+
zf.add(replacedEntry, 'test/data/file2.txt') { gotCalled = true; true }
|
170
|
+
end
|
152
171
|
assert(gotCalled)
|
153
|
-
::Zip::File.open(TEST_ZIP.zip_name)
|
154
|
-
|
155
|
-
|
156
|
-
}
|
172
|
+
::Zip::File.open(TEST_ZIP.zip_name) do |zf|
|
173
|
+
assert_contains(zf, replacedEntry, 'test/data/file2.txt')
|
174
|
+
end
|
157
175
|
end
|
158
176
|
|
159
|
-
def
|
160
|
-
::Zip::File.open(TEST_ZIP.zip_name)
|
161
|
-
|zf|
|
177
|
+
def test_add_directory
|
178
|
+
::Zip::File.open(TEST_ZIP.zip_name) do |zf|
|
162
179
|
zf.add(TestFiles::EMPTY_TEST_DIR, TestFiles::EMPTY_TEST_DIR)
|
163
|
-
|
164
|
-
::Zip::File.open(TEST_ZIP.zip_name)
|
165
|
-
|
166
|
-
dirEntry = zf.entries.detect { |e| e.name == 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 + '/' }
|
167
183
|
assert(dirEntry.directory?)
|
168
|
-
|
184
|
+
end
|
169
185
|
end
|
170
186
|
|
171
187
|
def test_remove
|
@@ -193,7 +209,7 @@ class ZipFileTest < MiniTest::Test
|
|
193
209
|
assert(zf.entries.map { |e| e.name }.include?(entryToRename))
|
194
210
|
|
195
211
|
contents = zf.read(entryToRename)
|
196
|
-
newName =
|
212
|
+
newName = 'changed entry name'
|
197
213
|
assert(!zf.entries.map { |e| e.name }.include?(newName))
|
198
214
|
|
199
215
|
zf.rename(entryToRename, newName)
|
@@ -211,9 +227,7 @@ class ZipFileTest < MiniTest::Test
|
|
211
227
|
|
212
228
|
def test_rename_with_each
|
213
229
|
zf_name = 'test_rename_zip.zip'
|
214
|
-
if ::File.exist?(zf_name)
|
215
|
-
::File.unlink(zf_name)
|
216
|
-
end
|
230
|
+
::File.unlink(zf_name) if ::File.exist?(zf_name)
|
217
231
|
arr = []
|
218
232
|
arr_renamed = []
|
219
233
|
::Zip::File.open(zf_name, ::Zip::File::CREATE) do |zf|
|
@@ -229,7 +243,7 @@ class ZipFileTest < MiniTest::Test
|
|
229
243
|
zf = ::Zip::File.open(zf_name)
|
230
244
|
assert_equal(zf.entries.map(&:name), arr)
|
231
245
|
zf.close
|
232
|
-
Zip::File.open(zf_name,
|
246
|
+
Zip::File.open(zf_name, 'wb') do |z|
|
233
247
|
z.each do |f|
|
234
248
|
z.rename(f, "Z#{f.name}")
|
235
249
|
end
|
@@ -237,12 +251,10 @@ class ZipFileTest < MiniTest::Test
|
|
237
251
|
zf = ::Zip::File.open(zf_name)
|
238
252
|
assert_equal(zf.entries.map(&:name), arr_renamed)
|
239
253
|
zf.close
|
240
|
-
if ::File.exist?(zf_name)
|
241
|
-
::File.unlink(zf_name)
|
242
|
-
end
|
254
|
+
::File.unlink(zf_name) if ::File.exist?(zf_name)
|
243
255
|
end
|
244
256
|
|
245
|
-
def
|
257
|
+
def test_rename_to_existing_entry
|
246
258
|
oldEntries = nil
|
247
259
|
::Zip::File.open(TEST_ZIP.zip_name) { |zf| oldEntries = zf.entries }
|
248
260
|
|
@@ -257,7 +269,7 @@ class ZipFileTest < MiniTest::Test
|
|
257
269
|
end
|
258
270
|
end
|
259
271
|
|
260
|
-
def
|
272
|
+
def test_rename_to_existing_entry_overwrite
|
261
273
|
oldEntries = nil
|
262
274
|
::Zip::File.open(TEST_ZIP.zip_name) { |zf| oldEntries = zf.entries }
|
263
275
|
|
@@ -276,61 +288,54 @@ class ZipFileTest < MiniTest::Test
|
|
276
288
|
end
|
277
289
|
end
|
278
290
|
|
279
|
-
def
|
280
|
-
nonEntry =
|
281
|
-
target_entry =
|
291
|
+
def test_rename_non_entry
|
292
|
+
nonEntry = 'bogusEntry'
|
293
|
+
target_entry = 'target_entryName'
|
282
294
|
zf = ::Zip::File.new(TEST_ZIP.zip_name)
|
283
295
|
assert(!zf.entries.include?(nonEntry))
|
284
|
-
assert_raises(Errno::ENOENT) {
|
285
|
-
zf.rename(nonEntry, target_entry)
|
286
|
-
}
|
296
|
+
assert_raises(Errno::ENOENT) { zf.rename(nonEntry, target_entry) }
|
287
297
|
zf.commit
|
288
298
|
assert(!zf.entries.include?(target_entry))
|
289
299
|
ensure
|
290
300
|
zf.close
|
291
301
|
end
|
292
302
|
|
293
|
-
def
|
303
|
+
def test_rename_entry_to_existing_entry
|
294
304
|
entry1, entry2, * = TEST_ZIP.entry_names
|
295
305
|
zf = ::Zip::File.new(TEST_ZIP.zip_name)
|
296
|
-
assert_raises(::Zip::EntryExistsError) {
|
297
|
-
zf.rename(entry1, entry2)
|
298
|
-
}
|
306
|
+
assert_raises(::Zip::EntryExistsError) { zf.rename(entry1, entry2) }
|
299
307
|
ensure
|
300
308
|
zf.close
|
301
309
|
end
|
302
310
|
|
303
311
|
def test_replace
|
304
312
|
entryToReplace = TEST_ZIP.entry_names[2]
|
305
|
-
newEntrySrcFilename =
|
313
|
+
newEntrySrcFilename = 'test/data/file2.txt'
|
306
314
|
zf = ::Zip::File.new(TEST_ZIP.zip_name)
|
307
315
|
zf.replace(entryToReplace, newEntrySrcFilename)
|
308
316
|
|
309
317
|
zf.close
|
310
318
|
zfRead = ::Zip::File.new(TEST_ZIP.zip_name)
|
311
|
-
AssertEntry
|
312
|
-
|
313
|
-
AssertEntry
|
314
|
-
|
315
|
-
AssertEntry
|
316
|
-
|
317
|
-
AssertEntry
|
318
|
-
|
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 })
|
319
327
|
zfRead.close
|
320
328
|
end
|
321
329
|
|
322
|
-
def
|
323
|
-
entryToReplace =
|
324
|
-
::Zip::File.open(TEST_ZIP.zip_name)
|
325
|
-
|
326
|
-
|
327
|
-
zf.replace(entryToReplace, "test/data/file2.txt")
|
328
|
-
}
|
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
|
330
335
|
end
|
331
336
|
|
332
337
|
def test_commit
|
333
|
-
newName =
|
338
|
+
newName = 'renamedFirst'
|
334
339
|
zf = ::Zip::File.new(TEST_ZIP.zip_name)
|
335
340
|
oldName = zf.entries.first
|
336
341
|
zf.rename(oldName, newName)
|
@@ -338,7 +343,7 @@ class ZipFileTest < MiniTest::Test
|
|
338
343
|
|
339
344
|
zfRead = ::Zip::File.new(TEST_ZIP.zip_name)
|
340
345
|
assert(zfRead.entries.detect { |e| e.name == newName } != nil)
|
341
|
-
assert(zfRead.entries.detect { |e| e.name == oldName }
|
346
|
+
assert(zfRead.entries.detect { |e| e.name == oldName }.nil?)
|
342
347
|
zfRead.close
|
343
348
|
|
344
349
|
zf.close
|
@@ -356,8 +361,8 @@ class ZipFileTest < MiniTest::Test
|
|
356
361
|
zf.commit
|
357
362
|
zf.close
|
358
363
|
zf2 = ::Zip::File.open(filename)
|
359
|
-
assert(zf2.entries.detect {|e| e.name == 'test1.txt'} != nil
|
360
|
-
assert(zf2.entries.detect {|e| e.name == 'test2.txt'} != nil
|
364
|
+
assert(zf2.entries.detect { |e| e.name == 'test1.txt' } != nil)
|
365
|
+
assert(zf2.entries.detect { |e| e.name == 'test2.txt' } != nil)
|
361
366
|
res = system("unzip -t #{filename}")
|
362
367
|
assert_equal(res, true)
|
363
368
|
end
|
@@ -368,7 +373,7 @@ class ZipFileTest < MiniTest::Test
|
|
368
373
|
end
|
369
374
|
|
370
375
|
def test_write_buffer
|
371
|
-
newName =
|
376
|
+
newName = 'renamedFirst'
|
372
377
|
zf = ::Zip::File.new(TEST_ZIP.zip_name)
|
373
378
|
oldName = zf.entries.first
|
374
379
|
zf.rename(oldName, newName)
|
@@ -377,7 +382,7 @@ class ZipFileTest < MiniTest::Test
|
|
377
382
|
File.open(TEST_ZIP.zip_name, 'wb') { |f| f.write buffer.string }
|
378
383
|
zfRead = ::Zip::File.new(TEST_ZIP.zip_name)
|
379
384
|
assert(zfRead.entries.detect { |e| e.name == newName } != nil)
|
380
|
-
assert(zfRead.entries.detect { |e| e.name == oldName }
|
385
|
+
assert(zfRead.entries.detect { |e| e.name == oldName }.nil?)
|
381
386
|
zfRead.close
|
382
387
|
|
383
388
|
zf.close
|
@@ -386,27 +391,26 @@ class ZipFileTest < MiniTest::Test
|
|
386
391
|
# This test tests that after commit, you
|
387
392
|
# can delete the file you used to add the entry to the zip file
|
388
393
|
# with
|
389
|
-
def
|
394
|
+
def test_commit_use_zip_entry
|
390
395
|
FileUtils.cp(TestFiles::RANDOM_ASCII_FILE1, OK_DELETE_FILE)
|
391
396
|
zf = ::Zip::File.open(TEST_ZIP.zip_name)
|
392
|
-
zf.add(
|
393
|
-
assert_contains(zf,
|
397
|
+
zf.add('okToDelete.txt', OK_DELETE_FILE)
|
398
|
+
assert_contains(zf, 'okToDelete.txt')
|
394
399
|
zf.commit
|
395
400
|
File.rename(OK_DELETE_FILE, OK_DELETE_MOVED_FILE)
|
396
|
-
assert_contains(zf,
|
401
|
+
assert_contains(zf, 'okToDelete.txt', OK_DELETE_MOVED_FILE)
|
397
402
|
end
|
398
403
|
|
399
|
-
# def test_close
|
400
|
-
# zf = ZipFile.new(TEST_ZIP.zip_name)
|
401
|
-
# zf.close
|
402
|
-
# assert_raises(IOError) {
|
403
|
-
# zf.extract(TEST_ZIP.entry_names.first, "hullubullu")
|
404
|
-
# }
|
405
|
-
# end
|
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
|
406
411
|
|
407
412
|
def test_compound1
|
408
|
-
renamedName =
|
409
|
-
originalEntries = []
|
413
|
+
renamedName = 'renamedName'
|
410
414
|
filename_to_remove = ''
|
411
415
|
begin
|
412
416
|
zf = ::Zip::File.new(TEST_ZIP.zip_name)
|
@@ -421,11 +425,10 @@ class ZipFileTest < MiniTest::Test
|
|
421
425
|
zf.rename(entry_to_rename, renamedName)
|
422
426
|
assert_contains(zf, renamedName)
|
423
427
|
|
424
|
-
TestFiles::BINARY_TEST_FILES.each
|
425
|
-
|filename|
|
428
|
+
TestFiles::BINARY_TEST_FILES.each do |filename|
|
426
429
|
zf.add(filename, filename)
|
427
430
|
assert_contains(zf, filename)
|
428
|
-
|
431
|
+
end
|
429
432
|
|
430
433
|
assert_contains(zf, originalEntries.last.to_s)
|
431
434
|
filename_to_remove = originalEntries.map(&:to_s).find { |name| name.match('longBinary') }
|
@@ -439,10 +442,9 @@ class ZipFileTest < MiniTest::Test
|
|
439
442
|
zfRead = ::Zip::File.new(TEST_ZIP.zip_name)
|
440
443
|
assert_contains(zfRead, TestFiles::RANDOM_ASCII_FILE1)
|
441
444
|
assert_contains(zfRead, renamedName)
|
442
|
-
TestFiles::BINARY_TEST_FILES.each
|
443
|
-
|filename|
|
445
|
+
TestFiles::BINARY_TEST_FILES.each do |filename|
|
444
446
|
assert_contains(zfRead, filename)
|
445
|
-
|
447
|
+
end
|
446
448
|
assert_not_contains(zfRead, filename_to_remove)
|
447
449
|
ensure
|
448
450
|
zfRead.close
|
@@ -454,23 +456,21 @@ class ZipFileTest < MiniTest::Test
|
|
454
456
|
zf = ::Zip::File.new(TEST_ZIP.zip_name)
|
455
457
|
originalEntries = zf.entries.dup
|
456
458
|
|
457
|
-
originalEntries.each
|
458
|
-
|entry|
|
459
|
+
originalEntries.each do |entry|
|
459
460
|
zf.remove(entry)
|
460
461
|
assert_not_contains(zf, entry)
|
461
|
-
|
462
|
+
end
|
462
463
|
assert(zf.entries.empty?)
|
463
464
|
|
464
|
-
TestFiles::ASCII_TEST_FILES.each
|
465
|
-
|filename|
|
465
|
+
TestFiles::ASCII_TEST_FILES.each do |filename|
|
466
466
|
zf.add(filename, filename)
|
467
467
|
assert_contains(zf, filename)
|
468
|
-
|
468
|
+
end
|
469
469
|
assert_equal(zf.entries.sort.map { |e| e.name }, TestFiles::ASCII_TEST_FILES)
|
470
470
|
|
471
|
-
zf.rename(TestFiles::ASCII_TEST_FILES[0],
|
471
|
+
zf.rename(TestFiles::ASCII_TEST_FILES[0], 'newName')
|
472
472
|
assert_not_contains(zf, TestFiles::ASCII_TEST_FILES[0])
|
473
|
-
assert_contains(zf,
|
473
|
+
assert_contains(zf, 'newName')
|
474
474
|
ensure
|
475
475
|
zf.close
|
476
476
|
end
|
@@ -478,56 +478,51 @@ class ZipFileTest < MiniTest::Test
|
|
478
478
|
zfRead = ::Zip::File.new(TEST_ZIP.zip_name)
|
479
479
|
asciiTestFiles = TestFiles::ASCII_TEST_FILES.dup
|
480
480
|
asciiTestFiles.shift
|
481
|
-
asciiTestFiles.each
|
482
|
-
|filename|
|
481
|
+
asciiTestFiles.each do |filename|
|
483
482
|
assert_contains(zf, filename)
|
484
|
-
|
483
|
+
end
|
485
484
|
|
486
|
-
assert_contains(zf,
|
485
|
+
assert_contains(zf, 'newName')
|
487
486
|
ensure
|
488
487
|
zfRead.close
|
489
488
|
end
|
490
489
|
end
|
491
490
|
|
492
|
-
def
|
491
|
+
def test_change_comment
|
493
492
|
::Zip::File.open(TEST_ZIP.zip_name) do |zf|
|
494
|
-
zf.comment =
|
493
|
+
zf.comment = 'my changed comment'
|
495
494
|
end
|
496
495
|
zfRead = ::Zip::File.open(TEST_ZIP.zip_name)
|
497
|
-
assert_equal(
|
496
|
+
assert_equal('my changed comment', zfRead.comment)
|
498
497
|
end
|
499
498
|
|
500
499
|
def test_preserve_file_order
|
501
500
|
entryNames = nil
|
502
|
-
::Zip::File.open(TEST_ZIP.zip_name)
|
503
|
-
|zf|
|
501
|
+
::Zip::File.open(TEST_ZIP.zip_name) do |zf|
|
504
502
|
entryNames = zf.entries.map { |e| e.to_s }
|
505
|
-
zf.get_output_stream(
|
506
|
-
zf.get_output_stream(
|
507
|
-
zf.get_output_stream(
|
508
|
-
entryNames <<
|
509
|
-
|
510
|
-
|
511
|
-
::Zip::File.open(TEST_ZIP.zip_name)
|
512
|
-
|zf|
|
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|
|
513
510
|
assert_equal(entryNames, zf.entries.map { |e| e.to_s })
|
514
511
|
entries = zf.entries.sort_by { |e| e.name }.reverse
|
515
|
-
entries.each
|
516
|
-
|e|
|
512
|
+
entries.each do |e|
|
517
513
|
zf.remove e
|
518
|
-
zf.get_output_stream(e) { |os| os.write
|
519
|
-
|
514
|
+
zf.get_output_stream(e) { |os| os.write 'foo' }
|
515
|
+
end
|
520
516
|
entryNames = entries.map { |e| e.to_s }
|
521
|
-
|
522
|
-
::Zip::File.open(TEST_ZIP.zip_name)
|
523
|
-
|zf|
|
517
|
+
end
|
518
|
+
::Zip::File.open(TEST_ZIP.zip_name) do |zf|
|
524
519
|
assert_equal(entryNames, zf.entries.map { |e| e.to_s })
|
525
|
-
|
520
|
+
end
|
526
521
|
end
|
527
522
|
|
528
523
|
def test_streaming
|
529
|
-
fname = ::File.join(::File.expand_path(::File.dirname(__FILE__)),
|
530
|
-
zname =
|
524
|
+
fname = ::File.join(::File.expand_path(::File.dirname(__FILE__)), '../README.md')
|
525
|
+
zname = 'test/data/generated/README.zip'
|
531
526
|
Zip::File.open(zname, Zip::File::CREATE) do |zipfile|
|
532
527
|
zipfile.get_output_stream(File.basename(fname)) do |f|
|
533
528
|
f.puts File.read(fname)
|
@@ -547,13 +542,42 @@ class ZipFileTest < MiniTest::Test
|
|
547
542
|
assert data =~ /Simonov/
|
548
543
|
end
|
549
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
|
+
|
550
573
|
private
|
574
|
+
|
551
575
|
def assert_contains(zf, entryName, filename = entryName)
|
552
576
|
assert(zf.entries.detect { |e| e.name == entryName } != nil, "entry #{entryName} not in #{zf.entries.join(', ')} in zip file #{zf}")
|
553
|
-
|
577
|
+
assert_entry_contents(zf, entryName, filename) if File.exist?(filename)
|
554
578
|
end
|
555
579
|
|
556
580
|
def assert_not_contains(zf, entryName)
|
557
|
-
assert(zf.entries.detect { |e| e.name == entryName }
|
581
|
+
assert(zf.entries.detect { |e| e.name == entryName }.nil?, "entry #{entryName} in #{zf.entries.join(', ')} in zip file #{zf}")
|
558
582
|
end
|
559
583
|
end
|