rubyzip 1.1.7 → 1.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/README.md +143 -54
- data/Rakefile +3 -4
- data/lib/zip/central_directory.rb +8 -8
- data/lib/zip/compressor.rb +1 -2
- data/lib/zip/constants.rb +5 -5
- data/lib/zip/crypto/null_encryption.rb +4 -6
- data/lib/zip/crypto/traditional_encryption.rb +5 -5
- data/lib/zip/decompressor.rb +3 -3
- data/lib/zip/deflater.rb +8 -6
- data/lib/zip/dos_time.rb +5 -6
- data/lib/zip/entry.rb +132 -128
- data/lib/zip/entry_set.rb +14 -14
- data/lib/zip/errors.rb +2 -0
- data/lib/zip/extra_field/generic.rb +8 -8
- 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 -2
- data/lib/zip/extra_field.rb +8 -8
- data/lib/zip/file.rb +88 -81
- data/lib/zip/filesystem.rb +144 -143
- data/lib/zip/inflater.rb +5 -5
- data/lib/zip/input_stream.rb +22 -13
- data/lib/zip/ioextras/abstract_input_stream.rb +6 -10
- data/lib/zip/ioextras/abstract_output_stream.rb +3 -5
- data/lib/zip/ioextras.rb +1 -3
- 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 +13 -14
- data/lib/zip/pass_thru_compressor.rb +4 -4
- data/lib/zip/pass_thru_decompressor.rb +3 -4
- data/lib/zip/streamable_directory.rb +2 -2
- data/lib/zip/streamable_stream.rb +3 -3
- data/lib/zip/version.rb +1 -1
- data/lib/zip.rb +13 -5
- data/samples/example.rb +29 -39
- data/samples/example_filesystem.rb +16 -18
- data/samples/example_recursive.rb +31 -25
- data/samples/{gtkRubyzip.rb → gtk_ruby_zip.rb} +23 -25
- data/samples/qtzip.rb +18 -27
- data/samples/write_simple.rb +12 -13
- data/samples/zipfind.rb +26 -34
- 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/gpbit3stored.zip +0 -0
- data/test/data/notzippedruby.rb +1 -1
- data/test/data/oddExtraField.zip +0 -0
- data/test/data/path_traversal/Makefile +10 -0
- data/test/data/path_traversal/jwilk/README.md +5 -0
- data/test/data/path_traversal/jwilk/absolute1.zip +0 -0
- data/test/data/path_traversal/jwilk/absolute2.zip +0 -0
- data/test/data/path_traversal/jwilk/dirsymlink.zip +0 -0
- data/test/data/path_traversal/jwilk/dirsymlink2a.zip +0 -0
- data/test/data/path_traversal/jwilk/dirsymlink2b.zip +0 -0
- data/test/data/path_traversal/jwilk/relative0.zip +0 -0
- data/test/data/path_traversal/jwilk/relative2.zip +0 -0
- data/test/data/path_traversal/jwilk/symlink.zip +0 -0
- data/test/data/path_traversal/relative1.zip +0 -0
- data/test/data/path_traversal/tilde.zip +0 -0
- data/test/data/path_traversal/tuzovakaoff/README.md +3 -0
- data/test/data/path_traversal/tuzovakaoff/absolutepath.zip +0 -0
- data/test/data/path_traversal/tuzovakaoff/symlink.zip +0 -0
- data/test/data/rubycode.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 +1 -2
- data/test/extra_field_test.rb +19 -21
- data/test/file_extract_directory_test.rb +12 -14
- data/test/file_extract_test.rb +94 -39
- data/test/file_permissions_test.rb +65 -0
- data/test/file_split_test.rb +24 -27
- data/test/file_test.rb +286 -179
- data/test/filesystem/dir_iterator_test.rb +13 -17
- data/test/filesystem/directory_test.rb +101 -93
- data/test/filesystem/file_mutating_test.rb +52 -65
- data/test/filesystem/file_nonmutating_test.rb +223 -229
- data/test/filesystem/file_stat_test.rb +17 -19
- data/test/gentestfiles.rb +54 -62
- data/test/inflater_test.rb +1 -1
- data/test/input_stream_test.rb +52 -40
- data/test/ioextras/abstract_input_stream_test.rb +22 -23
- data/test/ioextras/abstract_output_stream_test.rb +33 -33
- 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/path_traversal_test.rb +141 -0
- data/test/samples/example_recursive_test.rb +37 -0
- data/test/settings_test.rb +18 -15
- data/test/test_helper.rb +52 -46
- data/test/unicode_file_names_and_comments_test.rb +17 -7
- data/test/zip64_full_test.rb +10 -12
- data/test/zip64_support_test.rb +0 -1
- metadata +100 -66
data/lib/zip/inflater.rb
CHANGED
@@ -3,9 +3,9 @@ module Zip
|
|
3
3
|
def initialize(input_stream, decrypter = NullDecrypter.new)
|
4
4
|
super(input_stream)
|
5
5
|
@zlib_inflater = ::Zlib::Inflate.new(-Zlib::MAX_WBITS)
|
6
|
-
@output_buffer = ''
|
6
|
+
@output_buffer = ''.dup
|
7
7
|
@has_returned_empty_string = false
|
8
|
-
@decrypter
|
8
|
+
@decrypter = decrypter
|
9
9
|
end
|
10
10
|
|
11
11
|
def sysread(number_of_bytes = nil, buf = '')
|
@@ -20,7 +20,7 @@ module Zip
|
|
20
20
|
end
|
21
21
|
|
22
22
|
def produce_input
|
23
|
-
if
|
23
|
+
if @output_buffer.empty?
|
24
24
|
internal_produce_input
|
25
25
|
else
|
26
26
|
@output_buffer.slice!(0...(@output_buffer.length))
|
@@ -33,8 +33,8 @@ module Zip
|
|
33
33
|
@output_buffer.empty? && internal_input_finished?
|
34
34
|
end
|
35
35
|
|
36
|
-
alias :eof
|
37
|
-
alias :eof?
|
36
|
+
alias :eof input_finished?
|
37
|
+
alias :eof? input_finished?
|
38
38
|
|
39
39
|
private
|
40
40
|
|
data/lib/zip/input_stream.rb
CHANGED
@@ -86,14 +86,14 @@ module Zip
|
|
86
86
|
@output_buffer.empty? && @decompressor.eof
|
87
87
|
end
|
88
88
|
|
89
|
-
alias :eof?
|
89
|
+
alias :eof? eof
|
90
90
|
|
91
91
|
class << self
|
92
92
|
# Same as #initialize but if a block is passed the opened
|
93
93
|
# stream is passed to the block and closed when the block
|
94
94
|
# returns.
|
95
95
|
def open(filename_or_io, offset = 0, decrypter = nil)
|
96
|
-
zio =
|
96
|
+
zio = new(filename_or_io, offset, decrypter)
|
97
97
|
return zio unless block_given?
|
98
98
|
begin
|
99
99
|
yield zio
|
@@ -103,16 +103,15 @@ module Zip
|
|
103
103
|
end
|
104
104
|
|
105
105
|
def open_buffer(filename_or_io, offset = 0)
|
106
|
-
puts
|
107
|
-
|
106
|
+
puts 'open_buffer is deprecated!!! Use open instead!'
|
107
|
+
open(filename_or_io, offset)
|
108
108
|
end
|
109
109
|
end
|
110
110
|
|
111
111
|
protected
|
112
112
|
|
113
113
|
def get_io(io_or_file, offset = 0)
|
114
|
-
|
115
|
-
when IO, StringIO
|
114
|
+
if io_or_file.respond_to?(:seek)
|
116
115
|
io = io_or_file.dup
|
117
116
|
io.seek(offset, ::IO::SEEK_SET)
|
118
117
|
io
|
@@ -125,21 +124,31 @@ module Zip
|
|
125
124
|
|
126
125
|
def open_entry
|
127
126
|
@current_entry = ::Zip::Entry.read_local_entry(@archive_io)
|
128
|
-
if @current_entry
|
127
|
+
if @current_entry && @current_entry.gp_flags & 1 == 1 && @decrypter.is_a?(NullEncrypter)
|
129
128
|
raise Error, 'password required to decode zip file'
|
130
129
|
end
|
131
|
-
@
|
130
|
+
if @current_entry && @current_entry.gp_flags & 8 == 8 && @current_entry.crc == 0 \
|
131
|
+
&& @current_entry.compressed_size == 0 \
|
132
|
+
&& @current_entry.size == 0 && !@complete_entry
|
133
|
+
raise GPFBit3Error,
|
134
|
+
'General purpose flag Bit 3 is set so not possible to get proper info from local header.' \
|
135
|
+
'Please use ::Zip::File instead of ::Zip::InputStream'
|
136
|
+
end
|
137
|
+
@decompressor = get_decompressor
|
132
138
|
flush
|
133
139
|
@current_entry
|
134
140
|
end
|
135
141
|
|
136
142
|
def get_decompressor
|
137
|
-
|
138
|
-
when @current_entry.nil?
|
143
|
+
if @current_entry.nil?
|
139
144
|
::Zip::NullDecompressor
|
140
|
-
|
141
|
-
|
142
|
-
|
145
|
+
elsif @current_entry.compression_method == ::Zip::Entry::STORED
|
146
|
+
if @current_entry.gp_flags & 8 == 8 && @current_entry.crc == 0 && @current_entry.size == 0 && @complete_entry
|
147
|
+
::Zip::PassThruDecompressor.new(@archive_io, @complete_entry.size)
|
148
|
+
else
|
149
|
+
::Zip::PassThruDecompressor.new(@archive_io, @current_entry.size)
|
150
|
+
end
|
151
|
+
elsif @current_entry.compression_method == ::Zip::Entry::DEFLATED
|
143
152
|
header = @archive_io.read(@decrypter.header_bytesize)
|
144
153
|
@decrypter.reset!(header)
|
145
154
|
::Zip::Inflater.new(@archive_io, @decrypter)
|
@@ -33,9 +33,9 @@ module Zip
|
|
33
33
|
sysread(number_of_bytes, buf)
|
34
34
|
end
|
35
35
|
|
36
|
-
if tbuf.nil? || tbuf.
|
36
|
+
if tbuf.nil? || tbuf.empty?
|
37
37
|
return nil if number_of_bytes
|
38
|
-
return
|
38
|
+
return ''
|
39
39
|
end
|
40
40
|
|
41
41
|
@pos += tbuf.length
|
@@ -75,15 +75,13 @@ module Zip
|
|
75
75
|
over_limit = (number_of_bytes && @output_buffer.bytesize >= number_of_bytes)
|
76
76
|
while (match_index = @output_buffer.index(a_sep_string, buffer_index)).nil? && !over_limit
|
77
77
|
buffer_index = [buffer_index, @output_buffer.bytesize - a_sep_string.bytesize].max
|
78
|
-
if input_finished?
|
79
|
-
return @output_buffer.empty? ? nil : flush
|
80
|
-
end
|
78
|
+
return @output_buffer.empty? ? nil : flush if input_finished?
|
81
79
|
@output_buffer << produce_input
|
82
80
|
over_limit = (number_of_bytes && @output_buffer.bytesize >= number_of_bytes)
|
83
81
|
end
|
84
82
|
sep_index = [match_index + a_sep_string.bytesize, number_of_bytes || @output_buffer.bytesize].min
|
85
|
-
@pos
|
86
|
-
|
83
|
+
@pos += sep_index
|
84
|
+
@output_buffer.slice!(0...sep_index)
|
87
85
|
end
|
88
86
|
|
89
87
|
def ungetc(byte)
|
@@ -103,9 +101,7 @@ module Zip
|
|
103
101
|
end
|
104
102
|
|
105
103
|
def each_line(a_sep_string = $/)
|
106
|
-
while true
|
107
|
-
yield readline(a_sep_string)
|
108
|
-
end
|
104
|
+
yield readline(a_sep_string) while true
|
109
105
|
rescue EOFError
|
110
106
|
end
|
111
107
|
|
@@ -10,23 +10,22 @@ module Zip
|
|
10
10
|
data.to_s.bytesize
|
11
11
|
end
|
12
12
|
|
13
|
-
|
14
13
|
def print(*params)
|
15
14
|
self << params.join($,) << $\.to_s
|
16
15
|
end
|
17
16
|
|
18
17
|
def printf(a_format_string, *params)
|
19
|
-
self <<
|
18
|
+
self << format(a_format_string, *params)
|
20
19
|
end
|
21
20
|
|
22
21
|
def putc(an_object)
|
23
22
|
self << case an_object
|
24
|
-
when
|
23
|
+
when Integer
|
25
24
|
an_object.chr
|
26
25
|
when String
|
27
26
|
an_object
|
28
27
|
else
|
29
|
-
raise TypeError, 'putc: Only
|
28
|
+
raise TypeError, 'putc: Only Integer and String supported'
|
30
29
|
end
|
31
30
|
an_object
|
32
31
|
end
|
@@ -39,7 +38,6 @@ module Zip
|
|
39
38
|
self << "\n" unless val[-1, 1] == "\n"
|
40
39
|
end
|
41
40
|
end
|
42
|
-
|
43
41
|
end
|
44
42
|
end
|
45
43
|
end
|
data/lib/zip/ioextras.rb
CHANGED
data/lib/zip/null_compressor.rb
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
module Zip
|
2
2
|
module NullDecompressor #:nodoc:all
|
3
|
-
|
3
|
+
module_function
|
4
4
|
|
5
|
-
def sysread(
|
5
|
+
def sysread(_numberOfBytes = nil, _buf = nil)
|
6
6
|
nil
|
7
7
|
end
|
8
8
|
|
@@ -18,7 +18,7 @@ module Zip
|
|
18
18
|
true
|
19
19
|
end
|
20
20
|
|
21
|
-
alias
|
21
|
+
alias eof? eof
|
22
22
|
end
|
23
23
|
end
|
24
24
|
|
File without changes
|
data/lib/zip/output_stream.rb
CHANGED
@@ -24,7 +24,7 @@ module Zip
|
|
24
24
|
|
25
25
|
# Opens the indicated zip file. If a file with that name already
|
26
26
|
# exists it will be overwritten.
|
27
|
-
def initialize(file_name, stream=false, encrypter=nil)
|
27
|
+
def initialize(file_name, stream = false, encrypter = nil)
|
28
28
|
super()
|
29
29
|
@file_name = file_name
|
30
30
|
@output_stream = if stream
|
@@ -33,7 +33,7 @@ module Zip
|
|
33
33
|
iostream.rewind
|
34
34
|
iostream
|
35
35
|
else
|
36
|
-
::File.new(@file_name,
|
36
|
+
::File.new(@file_name, 'wb')
|
37
37
|
end
|
38
38
|
@entry_set = ::Zip::EntrySet.new
|
39
39
|
@compressor = ::Zip::NullCompressor.instance
|
@@ -86,15 +86,15 @@ module Zip
|
|
86
86
|
# Closes the current entry and opens a new for writing.
|
87
87
|
# +entry+ can be a ZipEntry object or a string.
|
88
88
|
def put_next_entry(entry_name, comment = nil, extra = nil, compression_method = Entry::DEFLATED, level = Zip.default_compression)
|
89
|
-
raise Error,
|
90
|
-
if entry_name.kind_of?(Entry)
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
89
|
+
raise Error, 'zip stream is closed' if @closed
|
90
|
+
new_entry = if entry_name.kind_of?(Entry)
|
91
|
+
entry_name
|
92
|
+
else
|
93
|
+
Entry.new(@file_name, entry_name.to_s)
|
94
|
+
end
|
95
95
|
new_entry.comment = comment unless comment.nil?
|
96
96
|
unless extra.nil?
|
97
|
-
new_entry.extra = ExtraField
|
97
|
+
new_entry.extra = extra.is_a?(ExtraField) ? extra : ExtraField.new(extra.to_s)
|
98
98
|
end
|
99
99
|
new_entry.compression_method = compression_method unless compression_method.nil?
|
100
100
|
init_next_entry(new_entry, level)
|
@@ -103,8 +103,8 @@ module Zip
|
|
103
103
|
|
104
104
|
def copy_raw_entry(entry)
|
105
105
|
entry = entry.dup
|
106
|
-
raise Error,
|
107
|
-
raise Error,
|
106
|
+
raise Error, 'zip stream is closed' if @closed
|
107
|
+
raise Error, 'entry is not a ZipEntry' unless entry.is_a?(Entry)
|
108
108
|
finalize_current_entry
|
109
109
|
@entry_set << entry
|
110
110
|
src_pos = entry.local_header_offset
|
@@ -123,7 +123,6 @@ module Zip
|
|
123
123
|
|
124
124
|
def finalize_current_entry
|
125
125
|
return unless @current_entry
|
126
|
-
@output_stream << @encrypter.header(@current_entry.mtime)
|
127
126
|
finish
|
128
127
|
@current_entry.compressed_size = @output_stream.tell - @current_entry.local_header_offset - @current_entry.calculate_local_header_size
|
129
128
|
@current_entry.size = @compressor.size
|
@@ -139,6 +138,7 @@ module Zip
|
|
139
138
|
@entry_set << entry
|
140
139
|
entry.write_local_entry(@output_stream)
|
141
140
|
@encrypter.reset!
|
141
|
+
@output_stream << @encrypter.header(entry.mtime)
|
142
142
|
@compressor = get_compressor(entry, level)
|
143
143
|
end
|
144
144
|
|
@@ -177,11 +177,10 @@ module Zip
|
|
177
177
|
public
|
178
178
|
|
179
179
|
# Modeled after IO.<<
|
180
|
-
def <<
|
180
|
+
def <<(data)
|
181
181
|
@compressor << data
|
182
182
|
self
|
183
183
|
end
|
184
|
-
|
185
184
|
end
|
186
185
|
end
|
187
186
|
|
@@ -3,13 +3,13 @@ module Zip
|
|
3
3
|
def initialize(outputStream)
|
4
4
|
super()
|
5
5
|
@output_stream = outputStream
|
6
|
-
@crc = Zlib
|
6
|
+
@crc = Zlib.crc32
|
7
7
|
@size = 0
|
8
8
|
end
|
9
|
-
|
10
|
-
def <<
|
9
|
+
|
10
|
+
def <<(data)
|
11
11
|
val = data.to_s
|
12
|
-
@crc = Zlib
|
12
|
+
@crc = Zlib.crc32(val, @crc)
|
13
13
|
@size += val.bytesize
|
14
14
|
@output_stream << val
|
15
15
|
end
|
@@ -1,6 +1,5 @@
|
|
1
1
|
module Zip
|
2
|
-
class PassThruDecompressor < Decompressor
|
3
|
-
|
2
|
+
class PassThruDecompressor < Decompressor #:nodoc:all
|
4
3
|
def initialize(input_stream, chars_to_read)
|
5
4
|
super(input_stream)
|
6
5
|
@chars_to_read = chars_to_read
|
@@ -31,8 +30,8 @@ module Zip
|
|
31
30
|
@read_so_far >= @chars_to_read
|
32
31
|
end
|
33
32
|
|
34
|
-
alias
|
35
|
-
alias
|
33
|
+
alias eof input_finished?
|
34
|
+
alias eof? input_finished?
|
36
35
|
end
|
37
36
|
end
|
38
37
|
|
@@ -4,8 +4,8 @@ module Zip
|
|
4
4
|
super(zipfile, entry)
|
5
5
|
|
6
6
|
@ftype = :directory
|
7
|
-
entry.get_extra_attributes_from_path(srcPath) if
|
8
|
-
@unix_perms = permissionInt if
|
7
|
+
entry.get_extra_attributes_from_path(srcPath) if srcPath
|
8
|
+
@unix_perms = permissionInt if permissionInt
|
9
9
|
end
|
10
10
|
end
|
11
11
|
end
|
@@ -1,11 +1,11 @@
|
|
1
1
|
module Zip
|
2
|
-
class StreamableStream < DelegateClass(Entry) #nodoc:all
|
2
|
+
class StreamableStream < DelegateClass(Entry) # nodoc:all
|
3
3
|
def initialize(entry)
|
4
4
|
super(entry)
|
5
5
|
dirname = if zipfile.is_a?(::String)
|
6
6
|
::File.dirname(zipfile)
|
7
7
|
else
|
8
|
-
|
8
|
+
nil
|
9
9
|
end
|
10
10
|
@temp_file = Tempfile.new(::File.basename(name), dirname)
|
11
11
|
@temp_file.binmode
|
@@ -24,7 +24,7 @@ module Zip
|
|
24
24
|
end
|
25
25
|
|
26
26
|
def get_input_stream
|
27
|
-
|
27
|
+
unless @temp_file.closed?
|
28
28
|
raise StandardError, "cannot open entry for reading while its open for writing - #{name}"
|
29
29
|
end
|
30
30
|
@temp_file.open # reopens tempfile from top
|
data/lib/zip/version.rb
CHANGED
data/lib/zip.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
require 'delegate'
|
2
2
|
require 'singleton'
|
3
3
|
require 'tempfile'
|
4
|
+
require 'tmpdir'
|
4
5
|
require 'fileutils'
|
5
6
|
require 'stringio'
|
6
7
|
require 'zlib'
|
@@ -30,14 +31,19 @@ require 'zip/streamable_stream'
|
|
30
31
|
require 'zip/streamable_directory'
|
31
32
|
require 'zip/constants'
|
32
33
|
require 'zip/errors'
|
33
|
-
if defined? JRUBY_VERSION
|
34
|
-
require 'jruby'
|
35
|
-
JRuby.objectspace = true
|
36
|
-
end
|
37
34
|
|
38
35
|
module Zip
|
39
36
|
extend self
|
40
|
-
attr_accessor :unicode_names,
|
37
|
+
attr_accessor :unicode_names,
|
38
|
+
:on_exists_proc,
|
39
|
+
:continue_on_exists_proc,
|
40
|
+
:sort_entries,
|
41
|
+
:default_compression,
|
42
|
+
:write_zip64_support,
|
43
|
+
:warn_invalid_date,
|
44
|
+
:case_insensitive_match,
|
45
|
+
:force_entry_names_encoding,
|
46
|
+
:validate_entry_sizes
|
41
47
|
|
42
48
|
def reset!
|
43
49
|
@_ran_once = false
|
@@ -48,6 +54,8 @@ module Zip
|
|
48
54
|
@default_compression = ::Zlib::DEFAULT_COMPRESSION
|
49
55
|
@write_zip64_support = false
|
50
56
|
@warn_invalid_date = true
|
57
|
+
@case_insensitive_match = false
|
58
|
+
@validate_entry_sizes = false
|
51
59
|
end
|
52
60
|
|
53
61
|
def setup
|
data/samples/example.rb
CHANGED
@@ -1,89 +1,79 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
|
3
|
-
$: <<
|
4
|
-
system(
|
3
|
+
$: << '../lib'
|
4
|
+
system('zip example.zip example.rb gtk_ruby_zip.rb')
|
5
5
|
|
6
6
|
require 'zip'
|
7
7
|
|
8
8
|
####### Using ZipInputStream alone: #######
|
9
9
|
|
10
|
-
Zip::InputStream.open(
|
11
|
-
|zis|
|
10
|
+
Zip::InputStream.open('example.zip') do |zis|
|
12
11
|
entry = zis.get_next_entry
|
13
12
|
print "First line of '#{entry.name} (#{entry.size} bytes): "
|
14
13
|
puts "'#{zis.gets.chomp}'"
|
15
14
|
entry = zis.get_next_entry
|
16
15
|
print "First line of '#{entry.name} (#{entry.size} bytes): "
|
17
16
|
puts "'#{zis.gets.chomp}'"
|
18
|
-
|
19
|
-
|
17
|
+
end
|
20
18
|
|
21
19
|
####### Using ZipFile to read the directory of a zip file: #######
|
22
20
|
|
23
|
-
zf = Zip::File.new(
|
24
|
-
zf.each_with_index
|
25
|
-
|entry, index|
|
26
|
-
|
21
|
+
zf = Zip::File.new('example.zip')
|
22
|
+
zf.each_with_index do |entry, index|
|
27
23
|
puts "entry #{index} is #{entry.name}, size = #{entry.size}, compressed size = #{entry.compressed_size}"
|
28
24
|
# use zf.get_input_stream(entry) to get a ZipInputStream for the entry
|
29
25
|
# entry can be the ZipEntry object or any object which has a to_s method that
|
30
26
|
# returns the name of the entry.
|
31
|
-
|
32
|
-
|
27
|
+
end
|
33
28
|
|
34
29
|
####### Using ZipOutputStream to write a zip file: #######
|
35
30
|
|
36
|
-
Zip::OutputStream.open(
|
37
|
-
|
38
|
-
zos.
|
39
|
-
zos.puts "Hello hello hello hello hello hello hello hello hello"
|
31
|
+
Zip::OutputStream.open('exampleout.zip') do |zos|
|
32
|
+
zos.put_next_entry('the first little entry')
|
33
|
+
zos.puts 'Hello hello hello hello hello hello hello hello hello'
|
40
34
|
|
41
|
-
zos.put_next_entry(
|
42
|
-
zos.puts
|
35
|
+
zos.put_next_entry('the second little entry')
|
36
|
+
zos.puts 'Hello again'
|
43
37
|
|
44
38
|
# Use rubyzip or your zip client of choice to verify
|
45
39
|
# the contents of exampleout.zip
|
46
|
-
|
40
|
+
end
|
47
41
|
|
48
42
|
####### Using ZipFile to change a zip file: #######
|
49
43
|
|
50
|
-
Zip::File.open(
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
}
|
44
|
+
Zip::File.open('exampleout.zip') do |zip_file|
|
45
|
+
zip_file.add('thisFile.rb', 'example.rb')
|
46
|
+
zip_file.rename('thisFile.rb', 'ILikeThisName.rb')
|
47
|
+
zip_file.add('Again', 'example.rb')
|
48
|
+
end
|
56
49
|
|
57
50
|
# Lets check
|
58
|
-
Zip::File.open(
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
}
|
51
|
+
Zip::File.open('exampleout.zip') do |zip_file|
|
52
|
+
puts "Changed zip file contains: #{zip_file.entries.join(', ')}"
|
53
|
+
zip_file.remove('Again')
|
54
|
+
puts "Without 'Again': #{zip_file.entries.join(', ')}"
|
55
|
+
end
|
64
56
|
|
65
57
|
####### Using ZipFile to split a zip file: #######
|
66
58
|
|
67
59
|
# Creating large zip file for splitting
|
68
|
-
Zip::OutputStream.open(
|
69
|
-
puts
|
60
|
+
Zip::OutputStream.open('large_zip_file.zip') do |zos|
|
61
|
+
puts 'Creating zip file...'
|
70
62
|
10.times do |i|
|
71
63
|
zos.put_next_entry("large_entry_#{i}.txt")
|
72
|
-
zos.puts
|
64
|
+
zos.puts 'Hello' * 104_857_600
|
73
65
|
end
|
74
66
|
end
|
75
67
|
|
76
68
|
# Splitting created large zip file
|
77
|
-
part_zips_count = Zip::File.split(
|
69
|
+
part_zips_count = Zip::File.split('large_zip_file.zip', 2_097_152, false)
|
78
70
|
puts "Zip file splitted in #{part_zips_count} parts"
|
79
71
|
|
80
72
|
# Track splitting an archive
|
81
|
-
Zip::File.split(
|
82
|
-
|
83
|
-
puts "#{part_index} of #{part_count} part splitting: #{(chunk_bytes.to_f/segment_bytes.to_f * 100).to_i}%"
|
73
|
+
Zip::File.split('large_zip_file.zip', 1_048_576, true, 'part_zip_file') do |part_count, part_index, chunk_bytes, segment_bytes|
|
74
|
+
puts "#{part_index} of #{part_count} part splitting: #{(chunk_bytes.to_f / segment_bytes.to_f * 100).to_i}%"
|
84
75
|
end
|
85
76
|
|
86
|
-
|
87
77
|
# For other examples, look at zip.rb and ziptest.rb
|
88
78
|
|
89
79
|
# Copyright (C) 2002 Thomas Sondergaard
|