rubyzip 1.1.7 → 1.2.4

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.

Files changed (106) hide show
  1. checksums.yaml +5 -5
  2. data/README.md +87 -45
  3. data/Rakefile +3 -4
  4. data/lib/zip.rb +11 -5
  5. data/lib/zip/central_directory.rb +8 -8
  6. data/lib/zip/compressor.rb +1 -2
  7. data/lib/zip/constants.rb +5 -5
  8. data/lib/zip/crypto/null_encryption.rb +4 -6
  9. data/lib/zip/crypto/traditional_encryption.rb +5 -5
  10. data/lib/zip/decompressor.rb +3 -3
  11. data/lib/zip/deflater.rb +8 -6
  12. data/lib/zip/dos_time.rb +5 -6
  13. data/lib/zip/entry.rb +120 -128
  14. data/lib/zip/entry_set.rb +14 -14
  15. data/lib/zip/errors.rb +1 -0
  16. data/lib/zip/extra_field.rb +8 -8
  17. data/lib/zip/extra_field/generic.rb +8 -8
  18. data/lib/zip/extra_field/ntfs.rb +14 -16
  19. data/lib/zip/extra_field/old_unix.rb +9 -10
  20. data/lib/zip/extra_field/universal_time.rb +14 -14
  21. data/lib/zip/extra_field/unix.rb +8 -9
  22. data/lib/zip/extra_field/zip64.rb +12 -11
  23. data/lib/zip/extra_field/zip64_placeholder.rb +1 -2
  24. data/lib/zip/file.rb +81 -81
  25. data/lib/zip/filesystem.rb +144 -143
  26. data/lib/zip/inflater.rb +5 -5
  27. data/lib/zip/input_stream.rb +22 -13
  28. data/lib/zip/ioextras.rb +1 -3
  29. data/lib/zip/ioextras/abstract_input_stream.rb +6 -10
  30. data/lib/zip/ioextras/abstract_output_stream.rb +3 -5
  31. data/lib/zip/null_compressor.rb +2 -2
  32. data/lib/zip/null_decompressor.rb +3 -3
  33. data/lib/zip/null_input_stream.rb +0 -0
  34. data/lib/zip/output_stream.rb +13 -14
  35. data/lib/zip/pass_thru_compressor.rb +4 -4
  36. data/lib/zip/pass_thru_decompressor.rb +3 -4
  37. data/lib/zip/streamable_directory.rb +2 -2
  38. data/lib/zip/streamable_stream.rb +3 -3
  39. data/lib/zip/version.rb +1 -1
  40. data/samples/example.rb +29 -39
  41. data/samples/example_filesystem.rb +16 -18
  42. data/samples/example_recursive.rb +31 -25
  43. data/samples/{gtkRubyzip.rb → gtk_ruby_zip.rb} +23 -25
  44. data/samples/qtzip.rb +18 -27
  45. data/samples/write_simple.rb +12 -13
  46. data/samples/zipfind.rb +26 -34
  47. data/test/basic_zip_file_test.rb +11 -15
  48. data/test/case_sensitivity_test.rb +69 -0
  49. data/test/central_directory_entry_test.rb +32 -36
  50. data/test/central_directory_test.rb +46 -50
  51. data/test/crypto/null_encryption_test.rb +8 -4
  52. data/test/crypto/traditional_encryption_test.rb +5 -5
  53. data/test/data/gpbit3stored.zip +0 -0
  54. data/test/data/notzippedruby.rb +1 -1
  55. data/test/data/oddExtraField.zip +0 -0
  56. data/test/data/path_traversal/Makefile +10 -0
  57. data/test/data/path_traversal/jwilk/README.md +5 -0
  58. data/test/data/path_traversal/jwilk/absolute1.zip +0 -0
  59. data/test/data/path_traversal/jwilk/absolute2.zip +0 -0
  60. data/test/data/path_traversal/jwilk/dirsymlink.zip +0 -0
  61. data/test/data/path_traversal/jwilk/dirsymlink2a.zip +0 -0
  62. data/test/data/path_traversal/jwilk/dirsymlink2b.zip +0 -0
  63. data/test/data/path_traversal/jwilk/relative0.zip +0 -0
  64. data/test/data/path_traversal/jwilk/relative2.zip +0 -0
  65. data/test/data/path_traversal/jwilk/symlink.zip +0 -0
  66. data/test/data/path_traversal/relative1.zip +0 -0
  67. data/test/data/path_traversal/tilde.zip +0 -0
  68. data/test/data/path_traversal/tuzovakaoff/README.md +3 -0
  69. data/test/data/path_traversal/tuzovakaoff/absolutepath.zip +0 -0
  70. data/test/data/path_traversal/tuzovakaoff/symlink.zip +0 -0
  71. data/test/data/rubycode.zip +0 -0
  72. data/test/data/test.xls +0 -0
  73. data/test/deflater_test.rb +10 -12
  74. data/test/encryption_test.rb +2 -2
  75. data/test/entry_set_test.rb +50 -25
  76. data/test/entry_test.rb +76 -87
  77. data/test/errors_test.rb +1 -2
  78. data/test/extra_field_test.rb +19 -21
  79. data/test/file_extract_directory_test.rb +12 -14
  80. data/test/file_extract_test.rb +33 -40
  81. data/test/file_permissions_test.rb +65 -0
  82. data/test/file_split_test.rb +24 -27
  83. data/test/file_test.rb +266 -179
  84. data/test/filesystem/dir_iterator_test.rb +13 -17
  85. data/test/filesystem/directory_test.rb +101 -93
  86. data/test/filesystem/file_mutating_test.rb +52 -65
  87. data/test/filesystem/file_nonmutating_test.rb +223 -229
  88. data/test/filesystem/file_stat_test.rb +17 -19
  89. data/test/gentestfiles.rb +54 -62
  90. data/test/inflater_test.rb +1 -1
  91. data/test/input_stream_test.rb +52 -40
  92. data/test/ioextras/abstract_input_stream_test.rb +22 -23
  93. data/test/ioextras/abstract_output_stream_test.rb +33 -33
  94. data/test/ioextras/fake_io_test.rb +1 -1
  95. data/test/local_entry_test.rb +36 -38
  96. data/test/output_stream_test.rb +20 -21
  97. data/test/pass_thru_compressor_test.rb +5 -6
  98. data/test/pass_thru_decompressor_test.rb +0 -1
  99. data/test/path_traversal_test.rb +141 -0
  100. data/test/samples/example_recursive_test.rb +37 -0
  101. data/test/settings_test.rb +18 -15
  102. data/test/test_helper.rb +52 -46
  103. data/test/unicode_file_names_and_comments_test.rb +17 -7
  104. data/test/zip64_full_test.rb +10 -12
  105. data/test/zip64_support_test.rb +0 -1
  106. metadata +94 -65
@@ -1,30 +1,28 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- $: << "../lib"
3
+ $: << '../lib'
4
4
 
5
- require 'zip/zipfilesystem'
5
+ require 'zip/filesystem'
6
6
 
7
- EXAMPLE_ZIP = "filesystem.zip"
7
+ EXAMPLE_ZIP = 'filesystem.zip'
8
8
 
9
- File.delete(EXAMPLE_ZIP) if File.exists?(EXAMPLE_ZIP)
9
+ File.delete(EXAMPLE_ZIP) if File.exist?(EXAMPLE_ZIP)
10
10
 
11
- Zip::File.open(EXAMPLE_ZIP, Zip::File::CREATE) {
12
- |zf|
13
- zf.file.open("file1.txt", "w") { |os| os.write "first file1.txt" }
14
- zf.dir.mkdir("dir1")
15
- zf.dir.chdir("dir1")
16
- zf.file.open("file1.txt", "w") { |os| os.write "second file1.txt" }
17
- puts zf.file.read("file1.txt")
18
- puts zf.file.read("../file1.txt")
19
- zf.dir.chdir("..")
20
- zf.file.open("file2.txt", "w") { |os| os.write "first file2.txt" }
11
+ Zip::File.open(EXAMPLE_ZIP, Zip::File::CREATE) do |zf|
12
+ zf.file.open('file1.txt', 'w') { |os| os.write 'first file1.txt' }
13
+ zf.dir.mkdir('dir1')
14
+ zf.dir.chdir('dir1')
15
+ zf.file.open('file1.txt', 'w') { |os| os.write 'second file1.txt' }
16
+ puts zf.file.read('file1.txt')
17
+ puts zf.file.read('../file1.txt')
18
+ zf.dir.chdir('..')
19
+ zf.file.open('file2.txt', 'w') { |os| os.write 'first file2.txt' }
21
20
  puts "Entries: #{zf.entries.join(', ')}"
22
- }
21
+ end
23
22
 
24
- Zip::File.open(EXAMPLE_ZIP) {
25
- |zf|
23
+ Zip::File.open(EXAMPLE_ZIP) do |zf|
26
24
  puts "Entries from reloaded zip: #{zf.entries.join(', ')}"
27
- }
25
+ end
28
26
 
29
27
  # For other examples, look at zip.rb and ziptest.rb
30
28
 
@@ -6,43 +6,49 @@ require 'zip'
6
6
  # included in the archive, rather just its contents.
7
7
  #
8
8
  # Usage:
9
- # directoryToZip = "/tmp/input"
10
- # outputFile = "/tmp/out.zip"
11
- # zf = ZipFileGenerator.new(directoryToZip, outputFile)
9
+ # directory_to_zip = "/tmp/input"
10
+ # output_file = "/tmp/out.zip"
11
+ # zf = ZipFileGenerator.new(directory_to_zip, output_file)
12
12
  # zf.write()
13
13
  class ZipFileGenerator
14
-
15
14
  # Initialize with the directory to zip and the location of the output archive.
16
- def initialize(inputDir, outputFile)
17
- @inputDir = inputDir
18
- @outputFile = outputFile
15
+ def initialize(input_dir, output_file)
16
+ @input_dir = input_dir
17
+ @output_file = output_file
19
18
  end
20
19
 
21
20
  # Zip the input directory.
22
- def write()
23
- entries = Dir.entries(@inputDir); entries.delete("."); entries.delete("..")
24
- io = Zip::File.open(@outputFile, Zip::File::CREATE);
21
+ def write
22
+ entries = Dir.entries(@input_dir) - %w[. ..]
25
23
 
26
- writeEntries(entries, "", io)
27
- io.close();
24
+ ::Zip::File.open(@output_file, ::Zip::File::CREATE) do |zipfile|
25
+ write_entries entries, '', zipfile
26
+ end
28
27
  end
29
28
 
30
- # A helper method to make the recursion work.
31
29
  private
32
- def writeEntries(entries, path, io)
33
-
34
- entries.each { |e|
35
- zipFilePath = path == "" ? e : File.join(path, e)
36
- diskFilePath = File.join(@inputDir, zipFilePath)
37
- puts "Deflating " + diskFilePath
38
- if File.directory?(diskFilePath)
39
- io.mkdir(zipFilePath)
40
- subdir =Dir.entries(diskFilePath); subdir.delete("."); subdir.delete("..")
41
- writeEntries(subdir, zipFilePath, io)
30
+
31
+ # A helper method to make the recursion work.
32
+ def write_entries(entries, path, zipfile)
33
+ entries.each do |e|
34
+ zipfile_path = path == '' ? e : File.join(path, e)
35
+ disk_file_path = File.join(@input_dir, zipfile_path)
36
+
37
+ if File.directory? disk_file_path
38
+ recursively_deflate_directory(disk_file_path, zipfile, zipfile_path)
42
39
  else
43
- io.get_output_stream(zipFilePath) { |f| f.puts(File.open(diskFilePath, "rb").read())}
40
+ put_into_archive(disk_file_path, zipfile, zipfile_path)
44
41
  end
45
- }
42
+ end
43
+ end
44
+
45
+ def recursively_deflate_directory(disk_file_path, zipfile, zipfile_path)
46
+ zipfile.mkdir zipfile_path
47
+ subdir = Dir.entries(disk_file_path) - %w[. ..]
48
+ write_entries subdir, zipfile_path, zipfile
46
49
  end
47
50
 
51
+ def put_into_archive(disk_file_path, zipfile, zipfile_path)
52
+ zipfile.add(zipfile_path, disk_file_path)
53
+ end
48
54
  end
@@ -1,6 +1,6 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- $: << "../lib"
3
+ $: << '../lib'
4
4
 
5
5
  $VERBOSE = true
6
6
 
@@ -11,7 +11,7 @@ class MainApp < Gtk::Window
11
11
  def initialize
12
12
  super()
13
13
  set_usize(400, 256)
14
- set_title("rubyzip")
14
+ set_title('rubyzip')
15
15
  signal_connect(Gtk::Window::SIGNAL_DESTROY) { Gtk.main_quit }
16
16
 
17
17
  box = Gtk::VBox.new(false, 0)
@@ -19,26 +19,25 @@ class MainApp < Gtk::Window
19
19
 
20
20
  @zipfile = nil
21
21
  @buttonPanel = ButtonPanel.new
22
- @buttonPanel.openButton.signal_connect(Gtk::Button::SIGNAL_CLICKED) {
22
+ @buttonPanel.openButton.signal_connect(Gtk::Button::SIGNAL_CLICKED) do
23
23
  show_file_selector
24
- }
25
- @buttonPanel.extractButton.signal_connect(Gtk::Button::SIGNAL_CLICKED) {
26
- puts "Not implemented!"
27
- }
24
+ end
25
+ @buttonPanel.extractButton.signal_connect(Gtk::Button::SIGNAL_CLICKED) do
26
+ puts 'Not implemented!'
27
+ end
28
28
  box.pack_start(@buttonPanel, false, false, 0)
29
29
 
30
30
  sw = Gtk::ScrolledWindow.new
31
31
  sw.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC)
32
32
  box.pack_start(sw, true, true, 0)
33
33
 
34
- @clist = Gtk::CList.new(["Name", "Size", "Compression"])
34
+ @clist = Gtk::CList.new(%w[Name Size Compression])
35
35
  @clist.set_selection_mode(Gtk::SELECTION_BROWSE)
36
36
  @clist.set_column_width(0, 120)
37
37
  @clist.set_column_width(1, 120)
38
- @clist.signal_connect(Gtk::CList::SIGNAL_SELECT_ROW) {
39
- |w, row, column, event|
38
+ @clist.signal_connect(Gtk::CList::SIGNAL_SELECT_ROW) do |_w, row, _column, _event|
40
39
  @selected_row = row
41
- }
40
+ end
42
41
  sw.add(@clist)
43
42
  end
44
43
 
@@ -48,38 +47,37 @@ class MainApp < Gtk::Window
48
47
  super
49
48
  set_layout(Gtk::BUTTONBOX_START)
50
49
  set_spacing(0)
51
- @openButton = Gtk::Button.new("Open archive")
52
- @extractButton = Gtk::Button.new("Extract entry")
50
+ @openButton = Gtk::Button.new('Open archive')
51
+ @extractButton = Gtk::Button.new('Extract entry')
53
52
  pack_start(@openButton)
54
53
  pack_start(@extractButton)
55
54
  end
56
55
  end
57
56
 
58
57
  def show_file_selector
59
- @fileSelector = Gtk::FileSelection.new("Open zip file")
58
+ @fileSelector = Gtk::FileSelection.new('Open zip file')
60
59
  @fileSelector.show
61
- @fileSelector.ok_button.signal_connect(Gtk::Button::SIGNAL_CLICKED) {
60
+ @fileSelector.ok_button.signal_connect(Gtk::Button::SIGNAL_CLICKED) do
62
61
  open_zip(@fileSelector.filename)
63
62
  @fileSelector.destroy
64
- }
65
- @fileSelector.cancel_button.signal_connect(Gtk::Button::SIGNAL_CLICKED) {
63
+ end
64
+ @fileSelector.cancel_button.signal_connect(Gtk::Button::SIGNAL_CLICKED) do
66
65
  @fileSelector.destroy
67
- }
66
+ end
68
67
  end
69
68
 
70
69
  def open_zip(filename)
71
70
  @zipfile = Zip::File.open(filename)
72
71
  @clist.clear
73
- @zipfile.each {
74
- |entry|
75
- @clist.append([ entry.name,
76
- entry.size.to_s,
77
- (100.0*entry.compressedSize/entry.size).to_s+"%" ])
78
- }
72
+ @zipfile.each do |entry|
73
+ @clist.append([entry.name,
74
+ entry.size.to_s,
75
+ (100.0 * entry.compressedSize / entry.size).to_s + '%'])
76
+ end
79
77
  end
80
78
  end
81
79
 
82
- mainApp = MainApp.new()
80
+ mainApp = MainApp.new
83
81
 
84
82
  mainApp.show_all
85
83
 
@@ -1,22 +1,18 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- $VERBOSE=true
3
+ $VERBOSE = true
4
4
 
5
- $: << "../lib"
5
+ $: << '../lib'
6
6
 
7
7
  require 'Qt'
8
8
  system('rbuic -o zipdialogui.rb zipdialogui.ui')
9
9
  require 'zipdialogui.rb'
10
10
  require 'zip'
11
11
 
12
-
13
-
14
12
  a = Qt::Application.new(ARGV)
15
13
 
16
14
  class ZipDialog < ZipDialogUI
17
-
18
-
19
- def initialize()
15
+ def initialize
20
16
  super()
21
17
  connect(child('add_button'), SIGNAL('clicked()'),
22
18
  self, SLOT('add_files()'))
@@ -32,16 +28,14 @@ class ZipDialog < ZipDialogUI
32
28
  Zip::File.foreach(@zip_filename, &proc)
33
29
  end
34
30
 
35
- def refresh()
36
- lv = child("entry_list_view")
31
+ def refresh
32
+ lv = child('entry_list_view')
37
33
  lv.clear
38
- each {
39
- |e|
34
+ each do |e|
40
35
  lv.insert_item(Qt::ListViewItem.new(lv, e.name, e.size.to_s))
41
- }
36
+ end
42
37
  end
43
38
 
44
-
45
39
  def load(zipfile)
46
40
  @zip_filename = zipfile
47
41
  refresh
@@ -49,13 +43,11 @@ class ZipDialog < ZipDialogUI
49
43
 
50
44
  def add_files
51
45
  l = Qt::FileDialog.getOpenFileNames(nil, nil, self)
52
- zipfile {
53
- |zf|
54
- l.each {
55
- |path|
46
+ zipfile do |zf|
47
+ l.each do |path|
56
48
  zf.add(File.basename(path), path)
57
- }
58
- }
49
+ end
50
+ end
59
51
  refresh
60
52
  end
61
53
 
@@ -63,7 +55,7 @@ class ZipDialog < ZipDialogUI
63
55
  selected_items = []
64
56
  unselected_items = []
65
57
  lv_item = entry_list_view.first_child
66
- while (lv_item)
58
+ while lv_item
67
59
  if entry_list_view.is_selected(lv_item)
68
60
  selected_items << lv_item.text(0)
69
61
  else
@@ -73,22 +65,21 @@ class ZipDialog < ZipDialogUI
73
65
  end
74
66
  puts "selected_items.size = #{selected_items.size}"
75
67
  puts "unselected_items.size = #{unselected_items.size}"
76
- items = selected_items.size > 0 ? selected_items : unselected_items
68
+ items = !selected_items.empty? ? selected_items : unselected_items
77
69
  puts "items.size = #{items.size}"
78
70
 
79
71
  d = Qt::FileDialog.get_existing_directory(nil, self)
80
- if (!d)
81
- puts "No directory chosen"
72
+ if !d
73
+ puts 'No directory chosen'
82
74
  else
83
75
  zipfile { |zf| items.each { |e| zf.extract(e, File.join(d, e)) } }
84
76
  end
85
-
86
77
  end
87
78
 
88
79
  slots 'add_files()', 'extract_files()'
89
80
  end
90
81
 
91
- if !ARGV[0]
82
+ unless ARGV[0]
92
83
  puts "usage: #{$0} zipname"
93
84
  exit
94
85
  end
@@ -97,5 +88,5 @@ zd = ZipDialog.new
97
88
  zd.load(ARGV[0])
98
89
 
99
90
  a.mainWidget = zd
100
- zd.show()
101
- a.exec()
91
+ zd.show
92
+ a.exec
@@ -1,13 +1,12 @@
1
- #!/usr/bin/env ruby
2
-
3
- $: << "../lib"
4
-
5
- require 'zip'
6
-
7
- include Zip
8
-
9
- OutputStream.open('simple.zip') {
10
- |zos|
11
- ze = zos.put_next_entry 'entry.txt'
12
- zos.puts "Hello world"
13
- }
1
+ #!/usr/bin/env ruby
2
+
3
+ $: << '../lib'
4
+
5
+ require 'zip'
6
+
7
+ include Zip
8
+
9
+ OutputStream.open('simple.zip') do |zos|
10
+ zos.put_next_entry 'entry.txt'
11
+ zos.puts 'Hello world'
12
+ end
@@ -2,7 +2,7 @@
2
2
 
3
3
  $VERBOSE = true
4
4
 
5
- $: << "../lib"
5
+ $: << '../lib'
6
6
 
7
7
  require 'zip'
8
8
  require 'find'
@@ -10,53 +10,46 @@ require 'find'
10
10
  module Zip
11
11
  module ZipFind
12
12
  def self.find(path, zipFilePattern = /\.zip$/i)
13
- Find.find(path) {
14
- |fileName|
15
- yield(fileName)
16
- if zipFilePattern.match(fileName) && File.file?(fileName)
17
- begin
18
- Zip::File.foreach(fileName) {
19
- |zipEntry|
20
- yield(fileName + File::SEPARATOR + zipEntry.to_s)
21
- }
22
- rescue Errno::EACCES => ex
23
- puts ex
24
- end
25
- end
26
- }
13
+ Find.find(path) do |fileName|
14
+ yield(fileName)
15
+ next unless zipFilePattern.match(fileName) && File.file?(fileName)
16
+ begin
17
+ Zip::File.foreach(fileName) do |zipEntry|
18
+ yield(fileName + File::SEPARATOR + zipEntry.to_s)
19
+ end
20
+ rescue Errno::EACCES => ex
21
+ puts ex
22
+ end
23
+ end
27
24
  end
28
25
 
29
26
  def self.find_file(path, fileNamePattern, zipFilePattern = /\.zip$/i)
30
- self.find(path, zipFilePattern) {
31
- |fileName|
32
- yield(fileName) if fileNamePattern.match(fileName)
33
- }
27
+ find(path, zipFilePattern) do |fileName|
28
+ yield(fileName) if fileNamePattern.match(fileName)
29
+ end
34
30
  end
35
-
36
31
  end
37
32
  end
38
33
 
39
- if __FILE__ == $0
34
+ if $0 == __FILE__
40
35
  module ZipFindConsoleRunner
41
-
42
- PATH_ARG_INDEX = 0;
43
- FILENAME_PATTERN_ARG_INDEX = 1;
44
- ZIPFILE_PATTERN_ARG_INDEX = 2;
36
+ PATH_ARG_INDEX = 0
37
+ FILENAME_PATTERN_ARG_INDEX = 1
38
+ ZIPFILE_PATTERN_ARG_INDEX = 2
45
39
 
46
40
  def self.run(args)
47
41
  check_args(args)
48
42
  Zip::ZipFind.find_file(args[PATH_ARG_INDEX],
49
- args[FILENAME_PATTERN_ARG_INDEX],
50
- args[ZIPFILE_PATTERN_ARG_INDEX]) {
51
- |fileName|
52
- report_entry_found fileName
53
- }
43
+ args[FILENAME_PATTERN_ARG_INDEX],
44
+ args[ZIPFILE_PATTERN_ARG_INDEX]) do |fileName|
45
+ report_entry_found fileName
46
+ end
54
47
  end
55
48
 
56
49
  def self.check_args(args)
57
- if (args.size != 3)
58
- usage
59
- exit
50
+ if args.size != 3
51
+ usage
52
+ exit
60
53
  end
61
54
  end
62
55
 
@@ -67,7 +60,6 @@ if __FILE__ == $0
67
60
  def self.report_entry_found(fileName)
68
61
  puts fileName
69
62
  end
70
-
71
63
  end
72
64
 
73
65
  ZipFindConsoleRunner.run(ARGV)