rubyzip 2.4.1 → 3.1.1

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.
Files changed (63) hide show
  1. checksums.yaml +4 -4
  2. data/Changelog.md +457 -0
  3. data/LICENSE.md +24 -0
  4. data/README.md +138 -40
  5. data/Rakefile +15 -13
  6. data/lib/zip/central_directory.rb +169 -123
  7. data/lib/zip/compressor.rb +3 -1
  8. data/lib/zip/constants.rb +29 -21
  9. data/lib/zip/crypto/aes_encryption.rb +120 -0
  10. data/lib/zip/crypto/decrypted_io.rb +20 -14
  11. data/lib/zip/crypto/encryption.rb +4 -2
  12. data/lib/zip/crypto/null_encryption.rb +5 -13
  13. data/lib/zip/crypto/traditional_encryption.rb +10 -6
  14. data/lib/zip/decompressor.rb +4 -3
  15. data/lib/zip/deflater.rb +12 -8
  16. data/lib/zip/dirtyable.rb +32 -0
  17. data/lib/zip/dos_time.rb +45 -5
  18. data/lib/zip/entry.rb +373 -249
  19. data/lib/zip/entry_set.rb +11 -9
  20. data/lib/zip/errors.rb +136 -16
  21. data/lib/zip/extra_field/aes.rb +45 -0
  22. data/lib/zip/extra_field/generic.rb +6 -13
  23. data/lib/zip/extra_field/ntfs.rb +6 -4
  24. data/lib/zip/extra_field/old_unix.rb +3 -1
  25. data/lib/zip/extra_field/universal_time.rb +3 -1
  26. data/lib/zip/extra_field/unix.rb +5 -3
  27. data/lib/zip/extra_field/unknown.rb +33 -0
  28. data/lib/zip/extra_field/zip64.rb +12 -5
  29. data/lib/zip/extra_field.rb +17 -22
  30. data/lib/zip/file.rb +166 -265
  31. data/lib/zip/file_split.rb +91 -0
  32. data/lib/zip/filesystem/dir.rb +86 -0
  33. data/lib/zip/filesystem/directory_iterator.rb +48 -0
  34. data/lib/zip/filesystem/file.rb +263 -0
  35. data/lib/zip/filesystem/file_stat.rb +110 -0
  36. data/lib/zip/filesystem/zip_file_name_mapper.rb +81 -0
  37. data/lib/zip/filesystem.rb +27 -596
  38. data/lib/zip/inflater.rb +11 -8
  39. data/lib/zip/input_stream.rb +76 -57
  40. data/lib/zip/ioextras/abstract_input_stream.rb +19 -13
  41. data/lib/zip/ioextras/abstract_output_stream.rb +13 -3
  42. data/lib/zip/ioextras.rb +7 -7
  43. data/lib/zip/null_compressor.rb +3 -1
  44. data/lib/zip/null_decompressor.rb +6 -3
  45. data/lib/zip/null_input_stream.rb +3 -1
  46. data/lib/zip/output_stream.rb +55 -56
  47. data/lib/zip/pass_thru_compressor.rb +3 -1
  48. data/lib/zip/pass_thru_decompressor.rb +8 -5
  49. data/lib/zip/streamable_directory.rb +3 -1
  50. data/lib/zip/streamable_stream.rb +4 -1
  51. data/lib/zip/version.rb +4 -1
  52. data/lib/zip.rb +25 -22
  53. data/rubyzip.gemspec +39 -0
  54. data/samples/example.rb +8 -3
  55. data/samples/example_filesystem.rb +3 -2
  56. data/samples/example_recursive.rb +3 -1
  57. data/samples/gtk_ruby_zip.rb +5 -3
  58. data/samples/qtzip.rb +7 -6
  59. data/samples/write_simple.rb +2 -1
  60. data/samples/zipfind.rb +1 -0
  61. metadata +87 -49
  62. data/TODO +0 -15
  63. data/lib/zip/extra_field/zip64_placeholder.rb +0 -15
@@ -1,5 +1,7 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Zip
2
- module TraditionalEncryption
4
+ module TraditionalEncryption # :nodoc:
3
5
  def initialize(password)
4
6
  @password = password
5
7
  reset_keys!
@@ -26,7 +28,7 @@ module Zip
26
28
 
27
29
  def update_keys(num)
28
30
  @key0 = ~Zlib.crc32(num, ~@key0)
29
- @key1 = ((@key1 + (@key0 & 0xff)) * 134_775_813 + 1) & 0xffffffff
31
+ @key1 = (((@key1 + (@key0 & 0xff)) * 134_775_813) + 1) & 0xffffffff
30
32
  @key2 = ~Zlib.crc32((@key1 >> 24).chr, ~@key2)
31
33
  end
32
34
 
@@ -36,7 +38,7 @@ module Zip
36
38
  end
37
39
  end
38
40
 
39
- class TraditionalEncrypter < Encrypter
41
+ class TraditionalEncrypter < Encrypter # :nodoc:
40
42
  include TraditionalEncryption
41
43
 
42
44
  def header(mtime)
@@ -53,8 +55,8 @@ module Zip
53
55
  data.unpack('C*').map { |x| encode x }.pack('C*')
54
56
  end
55
57
 
56
- def data_descriptor(crc32, compressed_size, uncomprssed_size)
57
- [0x08074b50, crc32, compressed_size, uncomprssed_size].pack('VVVV')
58
+ def data_descriptor(crc32, compressed_size, uncompressed_size)
59
+ [0x08074b50, crc32, compressed_size, uncompressed_size].pack('VVVV')
58
60
  end
59
61
 
60
62
  def reset!
@@ -70,7 +72,7 @@ module Zip
70
72
  end
71
73
  end
72
74
 
73
- class TraditionalDecrypter < Decrypter
75
+ class TraditionalDecrypter < Decrypter # :nodoc:
74
76
  include TraditionalEncryption
75
77
 
76
78
  def decrypt(data)
@@ -84,6 +86,8 @@ module Zip
84
86
  end
85
87
  end
86
88
 
89
+ def check_integrity!(_io); end
90
+
87
91
  private
88
92
 
89
93
  def decode(num)
@@ -1,5 +1,7 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Zip
2
- class Decompressor #:nodoc:all
4
+ class Decompressor # :nodoc:all
3
5
  CHUNK_SIZE = 32_768
4
6
 
5
7
  def self.decompressor_classes
@@ -14,8 +16,7 @@ module Zip
14
16
  decompressor_classes[compression_method]
15
17
  end
16
18
 
17
- attr_reader :input_stream
18
- attr_reader :decompressed_size
19
+ attr_reader :decompressed_size, :input_stream
19
20
 
20
21
  def initialize(input_stream, decompressed_size = nil)
21
22
  super()
data/lib/zip/deflater.rb CHANGED
@@ -1,5 +1,7 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Zip
2
- class Deflater < Compressor #:nodoc:all
4
+ class Deflater < Compressor # :nodoc:all
3
5
  def initialize(output_stream, level = Zip.default_compression, encrypter = NullEncrypter.new)
4
6
  super()
5
7
  @output_stream = output_stream
@@ -13,16 +15,18 @@ module Zip
13
15
  val = data.to_s
14
16
  @crc = Zlib.crc32(val, @crc)
15
17
  @size += val.bytesize
16
- buffer = @zlib_deflater.deflate(data)
17
- if buffer.empty?
18
- @output_stream
19
- else
20
- @output_stream << @encrypter.encrypt(buffer)
21
- end
18
+
19
+ # When JRuby#3962 is fixed, we can remove the flushing parameter here.
20
+ buffer = @zlib_deflater.deflate(data, Zip::ZLIB_FLUSHING_STRATEGY)
21
+ return @output_stream if buffer.empty?
22
+
23
+ @output_stream << @encrypter.encrypt(buffer)
22
24
  end
23
25
 
24
26
  def finish
25
- @output_stream << @encrypter.encrypt(@zlib_deflater.finish) until @zlib_deflater.finished?
27
+ buffer = @zlib_deflater.finish
28
+ @output_stream << @encrypter.encrypt(buffer) unless buffer.empty?
29
+ @zlib_deflater.close
26
30
  end
27
31
 
28
32
  attr_reader :size, :crc
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Zip
4
+ module Dirtyable # :nodoc:all
5
+ def initialize(dirty_on_create: true)
6
+ @dirty = dirty_on_create
7
+ end
8
+
9
+ def dirty?
10
+ @dirty
11
+ end
12
+
13
+ module ClassMethods # :nodoc:
14
+ def mark_dirty(*symbols) # :nodoc:
15
+ # Move the original method and call it after we've set the dirty flag.
16
+ symbols.each do |symbol|
17
+ orig_name = "orig_#{symbol}"
18
+ alias_method orig_name, symbol
19
+
20
+ define_method(symbol) do |param|
21
+ @dirty = true
22
+ send(orig_name, param)
23
+ end
24
+ end
25
+ end
26
+ end
27
+
28
+ def self.included(base)
29
+ base.extend(ClassMethods)
30
+ end
31
+ end
32
+ end
data/lib/zip/dos_time.rb CHANGED
@@ -1,5 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rubygems'
4
+
1
5
  module Zip
2
- class DOSTime < Time #:nodoc:all
6
+ class DOSTime < Time # :nodoc:all
3
7
  # MS-DOS File Date and Time format as used in Interrupt 21H Function 57H:
4
8
 
5
9
  # Register CX, the Time:
@@ -12,6 +16,14 @@ module Zip
12
16
  # bits 5-8 month (1-12)
13
17
  # bits 9-15 year (four digit year minus 1980)
14
18
 
19
+ attr_writer :absolute_time # :nodoc:
20
+
21
+ def absolute_time?
22
+ # If absolute time is not set, we can assume it is an absolute time
23
+ # because times do have timezone information by default.
24
+ @absolute_time.nil? || @absolute_time
25
+ end
26
+
15
27
  def to_binary_dos_time
16
28
  (sec / 2) +
17
29
  (min << 5) +
@@ -24,9 +36,9 @@ module Zip
24
36
  ((year - 1980) << 9)
25
37
  end
26
38
 
27
- def dos_equals(other)
28
- Zip.warn_about_v3_api('DOSTime#dos_equals')
29
-
39
+ # Deprecated. Remove for version 4.
40
+ def dos_equals(other) # rubocop:disable Naming/PredicateMethod
41
+ warn 'Zip::DOSTime#dos_equals is deprecated. Use `==` instead.'
30
42
  self == other
31
43
  end
32
44
 
@@ -50,7 +62,35 @@ module Zip
50
62
  month = (0b111100000 & bin_dos_date) >> 5
51
63
  year = ((0b1111111000000000 & bin_dos_date) >> 9) + 1980
52
64
 
53
- local(year, month, day, hour, minute, second)
65
+ time = local(year, month, day, hour, minute, second)
66
+ time.absolute_time = false
67
+ time
68
+ end
69
+
70
+ if defined? JRUBY_VERSION && Gem::Version.new(JRUBY_VERSION) < '9.2.18.0'
71
+ module JRubyCMP # :nodoc:
72
+ def ==(other)
73
+ (self <=> other).zero?
74
+ end
75
+
76
+ def <(other)
77
+ (self <=> other).negative?
78
+ end
79
+
80
+ def <=(other)
81
+ (self <=> other) <= 0
82
+ end
83
+
84
+ def >(other)
85
+ (self <=> other).positive?
86
+ end
87
+
88
+ def >=(other)
89
+ (self <=> other) >= 0
90
+ end
91
+ end
92
+
93
+ include JRubyCMP
54
94
  end
55
95
  end
56
96
  end