rubyzip 1.2.3 → 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.
- checksums.yaml +4 -4
- data/README.md +10 -10
- data/lib/zip/file.rb +30 -17
- data/lib/zip/version.rb +1 -1
- data/test/file_test.rb +51 -6
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 971ff85e75ec367c6d983a2c5fdcb24f8c5d1a4a450068b6b925a2df735cd1aa
|
4
|
+
data.tar.gz: 665b1e4f89f765cd9138222b08ac7759ee8e3ad4ad6e7c68171f2afb523b2f9f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4322b88da024f5625fa1824c80a8ee8b07e9970baea5937b5a11004d21038a590aeb07c4beb2296d639fb67c1146f2ace736bef7faffb87afcc065538df37afb
|
7
|
+
data.tar.gz: 77ba210a9a4d66b35fa43a75ac72126c0df5fb65d04e79c50e2168698aeae66a8cf19c27fb86ccb39a6670a3e079bda953c22fd9917ed7a7b0f33c83753e3107
|
data/README.md
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
# rubyzip
|
2
|
+
|
2
3
|
[![Gem Version](https://badge.fury.io/rb/rubyzip.svg)](http://badge.fury.io/rb/rubyzip)
|
3
4
|
[![Build Status](https://secure.travis-ci.org/rubyzip/rubyzip.svg)](http://travis-ci.org/rubyzip/rubyzip)
|
4
5
|
[![Code Climate](https://codeclimate.com/github/rubyzip/rubyzip.svg)](https://codeclimate.com/github/rubyzip/rubyzip)
|
@@ -19,9 +20,10 @@ gem 'zip-zip' # will load compatibility for old rubyzip API.
|
|
19
20
|
|
20
21
|
## Requirements
|
21
22
|
|
22
|
-
|
23
|
+
- Ruby 1.9.2 or greater
|
23
24
|
|
24
25
|
## Installation
|
26
|
+
|
25
27
|
Rubyzip is available on RubyGems:
|
26
28
|
|
27
29
|
```
|
@@ -59,7 +61,8 @@ end
|
|
59
61
|
```
|
60
62
|
|
61
63
|
### Zipping a directory recursively
|
62
|
-
|
64
|
+
|
65
|
+
Copy from [here](https://github.com/rubyzip/rubyzip/blob/9d891f7353e66052283562d3e252fe380bb4b199/samples/example_recursive.rb)
|
63
66
|
|
64
67
|
```ruby
|
65
68
|
require 'zip'
|
@@ -83,7 +86,7 @@ class ZipFileGenerator
|
|
83
86
|
|
84
87
|
# Zip the input directory.
|
85
88
|
def write
|
86
|
-
entries = Dir.entries(@input_dir) - %w
|
89
|
+
entries = Dir.entries(@input_dir) - %w[. ..]
|
87
90
|
|
88
91
|
::Zip::File.open(@output_file, ::Zip::File::CREATE) do |zipfile|
|
89
92
|
write_entries entries, '', zipfile
|
@@ -97,7 +100,6 @@ class ZipFileGenerator
|
|
97
100
|
entries.each do |e|
|
98
101
|
zipfile_path = path == '' ? e : File.join(path, e)
|
99
102
|
disk_file_path = File.join(@input_dir, zipfile_path)
|
100
|
-
puts "Deflating #{disk_file_path}"
|
101
103
|
|
102
104
|
if File.directory? disk_file_path
|
103
105
|
recursively_deflate_directory(disk_file_path, zipfile, zipfile_path)
|
@@ -109,14 +111,12 @@ class ZipFileGenerator
|
|
109
111
|
|
110
112
|
def recursively_deflate_directory(disk_file_path, zipfile, zipfile_path)
|
111
113
|
zipfile.mkdir zipfile_path
|
112
|
-
subdir = Dir.entries(disk_file_path) - %w
|
114
|
+
subdir = Dir.entries(disk_file_path) - %w[. ..]
|
113
115
|
write_entries subdir, zipfile_path, zipfile
|
114
116
|
end
|
115
117
|
|
116
118
|
def put_into_archive(disk_file_path, zipfile, zipfile_path)
|
117
|
-
zipfile.
|
118
|
-
f.write(File.open(disk_file_path, 'rb').read)
|
119
|
-
end
|
119
|
+
zipfile.add(zipfile_path, disk_file_path)
|
120
120
|
end
|
121
121
|
end
|
122
122
|
```
|
@@ -177,7 +177,6 @@ But there is one exception when it is not working - General Purpose Flag Bit 3.
|
|
177
177
|
|
178
178
|
> If bit 3 (0x08) of the general-purpose flags field is set, then the CRC-32 and file sizes are not known when the header is written. The fields in the local header are filled with zero, and the CRC-32 and size are appended in a 12-byte structure (optionally preceded by a 4-byte signature) immediately after the compressed data
|
179
179
|
|
180
|
-
|
181
180
|
If `::Zip::InputStream` finds such entry in the zip archive it will raise an exception.
|
182
181
|
|
183
182
|
### Password Protection (Experimental)
|
@@ -220,7 +219,7 @@ File.open(new_path, "wb") {|f| f.write(buffer.string) }
|
|
220
219
|
|
221
220
|
## Configuration
|
222
221
|
|
223
|
-
By default, rubyzip will not overwrite files if they already exist inside of the extracted path.
|
222
|
+
By default, rubyzip will not overwrite files if they already exist inside of the extracted path. To change this behavior, you may specify a configuration option like so:
|
224
223
|
|
225
224
|
```ruby
|
226
225
|
Zip.on_exists_proc = true
|
@@ -251,6 +250,7 @@ You can set the default compression level like so:
|
|
251
250
|
```ruby
|
252
251
|
Zip.default_compression = Zlib::DEFAULT_COMPRESSION
|
253
252
|
```
|
253
|
+
|
254
254
|
It defaults to `Zlib::DEFAULT_COMPRESSION`. Possible values are `Zlib::BEST_COMPRESSION`, `Zlib::DEFAULT_COMPRESSION` and `Zlib::NO_COMPRESSION`
|
255
255
|
|
256
256
|
Sometimes file names inside zip contain non-ASCII characters. If you can assume which encoding was used for such names and want to be able to find such entries using `find_entry` then you can force assumed encoding like so:
|
data/lib/zip/file.rb
CHANGED
@@ -64,24 +64,38 @@ module Zip
|
|
64
64
|
|
65
65
|
# Opens a zip archive. Pass true as the second parameter to create
|
66
66
|
# a new archive if it doesn't exist already.
|
67
|
-
def initialize(
|
67
|
+
def initialize(path_or_io, create = false, buffer = false, options = {})
|
68
68
|
super()
|
69
|
-
@name =
|
69
|
+
@name = path_or_io.respond_to?(:path) ? path_or_io.path : path_or_io
|
70
70
|
@comment = ''
|
71
71
|
@create = create ? true : false # allow any truthy value to mean true
|
72
|
-
|
72
|
+
|
73
|
+
if ::File.size?(@name.to_s)
|
74
|
+
# There is a file, which exists, that is associated with this zip.
|
73
75
|
@create = false
|
74
|
-
@file_permissions = ::File.stat(
|
75
|
-
|
76
|
-
|
76
|
+
@file_permissions = ::File.stat(@name).mode
|
77
|
+
|
78
|
+
if buffer
|
79
|
+
read_from_stream(path_or_io)
|
80
|
+
else
|
81
|
+
::File.open(@name, 'rb') do |f|
|
82
|
+
read_from_stream(f)
|
83
|
+
end
|
77
84
|
end
|
85
|
+
elsif buffer && path_or_io.size > 0
|
86
|
+
# This zip is probably a non-empty StringIO.
|
87
|
+
read_from_stream(path_or_io)
|
78
88
|
elsif @create
|
89
|
+
# This zip is completely new/empty and is to be created.
|
79
90
|
@entry_set = EntrySet.new
|
80
|
-
elsif ::File.zero?(
|
81
|
-
|
91
|
+
elsif ::File.zero?(@name)
|
92
|
+
# A file exists, but it is empty.
|
93
|
+
raise Error, "File #{@name} has zero size. Did you mean to pass the create flag?"
|
82
94
|
else
|
83
|
-
|
95
|
+
# Everything is wrong.
|
96
|
+
raise Error, "File #{@name} not found"
|
84
97
|
end
|
98
|
+
|
85
99
|
@stored_entries = @entry_set.dup
|
86
100
|
@stored_comment = @comment
|
87
101
|
@restore_ownership = options[:restore_ownership] || false
|
@@ -119,17 +133,16 @@ module Zip
|
|
119
133
|
unless IO_METHODS.map { |method| io.respond_to?(method) }.all? || io.is_a?(String)
|
120
134
|
raise "Zip::File.open_buffer expects a String or IO-like argument (responds to #{IO_METHODS.join(', ')}). Found: #{io.class}"
|
121
135
|
end
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
end
|
136
|
+
|
137
|
+
io = ::StringIO.new(io) if io.is_a?(::String)
|
138
|
+
|
139
|
+
# https://github.com/rubyzip/rubyzip/issues/119
|
140
|
+
io.binmode if io.respond_to?(:binmode)
|
141
|
+
|
129
142
|
zf = ::Zip::File.new(io, true, true, options)
|
130
|
-
zf.read_from_stream(io)
|
131
143
|
return zf unless block_given?
|
132
144
|
yield zf
|
145
|
+
|
133
146
|
begin
|
134
147
|
zf.write_buffer(io)
|
135
148
|
rescue IOError => e
|
data/lib/zip/version.rb
CHANGED
data/test/file_test.rb
CHANGED
@@ -103,6 +103,13 @@ class ZipFileTest < MiniTest::Test
|
|
103
103
|
end
|
104
104
|
end
|
105
105
|
|
106
|
+
def test_open_buffer_with_string
|
107
|
+
string = File.read('test/data/rubycode.zip')
|
108
|
+
::Zip::File.open_buffer string do |zf|
|
109
|
+
assert zf.entries.map { |e| e.name }.include?('zippedruby1.rb')
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
106
113
|
def test_open_buffer_with_stringio
|
107
114
|
string_io = StringIO.new File.read('test/data/rubycode.zip')
|
108
115
|
::Zip::File.open_buffer string_io do |zf|
|
@@ -113,14 +120,52 @@ class ZipFileTest < MiniTest::Test
|
|
113
120
|
def test_close_buffer_with_stringio
|
114
121
|
string_io = StringIO.new File.read('test/data/rubycode.zip')
|
115
122
|
zf = ::Zip::File.open_buffer string_io
|
116
|
-
|
123
|
+
assert_nil zf.close
|
117
124
|
end
|
118
125
|
|
119
|
-
def
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
126
|
+
def test_open_buffer_no_op_does_not_change_file
|
127
|
+
Dir.mktmpdir do |tmp|
|
128
|
+
test_zip = File.join(tmp, 'test.zip')
|
129
|
+
FileUtils.cp 'test/data/rubycode.zip', test_zip
|
130
|
+
|
131
|
+
# Note: this may change the file if it is opened with r+b instead of rb.
|
132
|
+
# The 'extra fields' in this particular zip file get reordered.
|
133
|
+
File.open(test_zip, 'rb') do |file|
|
134
|
+
Zip::File.open_buffer(file) do |zf|
|
135
|
+
nil # do nothing
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
139
|
+
assert_equal \
|
140
|
+
File.binread('test/data/rubycode.zip'),
|
141
|
+
File.binread(test_zip)
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
145
|
+
def test_open_buffer_close_does_not_change_file
|
146
|
+
Dir.mktmpdir do |tmp|
|
147
|
+
test_zip = File.join(tmp, 'test.zip')
|
148
|
+
FileUtils.cp 'test/data/rubycode.zip', test_zip
|
149
|
+
|
150
|
+
File.open(test_zip, 'rb') do |file|
|
151
|
+
zf = Zip::File.open_buffer(file)
|
152
|
+
refute zf.commit_required?
|
153
|
+
assert_nil zf.close
|
154
|
+
end
|
155
|
+
|
156
|
+
assert_equal \
|
157
|
+
File.binread('test/data/rubycode.zip'),
|
158
|
+
File.binread(test_zip)
|
159
|
+
end
|
160
|
+
end
|
161
|
+
|
162
|
+
def test_open_buffer_with_io_and_block
|
163
|
+
File.open('test/data/rubycode.zip') do |io|
|
164
|
+
io.set_encoding(Encoding::BINARY) # not strictly required but can be set
|
165
|
+
Zip::File.open_buffer(io) do |zip_io|
|
166
|
+
# left empty on purpose
|
167
|
+
end
|
168
|
+
end
|
124
169
|
end
|
125
170
|
|
126
171
|
def test_open_buffer_without_block
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rubyzip
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.2.
|
4
|
+
version: 1.2.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alexander Simonov
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-
|
11
|
+
date: 2019-09-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -227,7 +227,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
227
227
|
- !ruby/object:Gem::Version
|
228
228
|
version: '0'
|
229
229
|
requirements: []
|
230
|
-
rubygems_version: 3.0.
|
230
|
+
rubygems_version: 3.0.3
|
231
231
|
signing_key:
|
232
232
|
specification_version: 4
|
233
233
|
summary: rubyzip is a ruby module for reading and writing zip files
|