8000 Merge pull request #2 from rubyzip/master · rubyzip/rubyzip@63d388c · GitHub
[go: up one dir, main page]

Skip to content

Commit 63d388c

Browse files
committed
Merge pull request #2 from rubyzip/master
update master
2 parents 3efd167 + d289780 commit 63d388c

24 files changed

+122
-59
lines changed

README.md

Lines changed: 49 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
[![Code Climate](https://codeclimate.com/github/rubyzip/rubyzip.svg)](https://codeclimate.com/github/rubyzip/rubyzip)
55
[![Coverage Status](https://img.shields.io/coveralls/rubyzip/rubyzip.svg)](https://coveralls.io/r/rubyzip/rubyzip?branch=master)
66

7-
rubyzip is a ruby library for reading and writing zip files.
7+
Rubyzip is a ruby library for reading and writing zip files.
88

99
## Important note
1010

11-
Rubyzip interface changed!!! No need to do `require "zip/zip"` and `Zip` prefix in class names removed.
11+
The Rubyzip interface has changed!!! No need to do `require "zip/zip"` and `Zip` prefix in class names removed.
1212

1313
If you have issues with any third-party gems that require an old version of rubyzip, you can use this workaround:
1414

@@ -22,7 +22,7 @@ gem 'zip-zip' # will load compatibility for old rubyzip API.
2222
* Ruby 1.9.2 or greater
2323

2424
## Installation
25-
rubyzip is available on RubyGems, so:
25+
Rubyzip is available on RubyGems:
2626

2727
```
2828
gem install rubyzip
@@ -78,39 +78,54 @@ require 'zip'
7878

7979
class ZipFileGenerator
8080
# Initialize with the directory to zip and the location of the output archive.
81-
def initialize(inputDir, outputFile)
82-
@inputDir = inputDir
83-
@outputFile = outputFile
81+
def initialize(input_dir, output_file)
82+
@input_dir = input_dir
83+
@output_file = output_file
8484
end
85+
8586
# Zip the input directory.
86-
def write()
87-
entries = Dir.entries(@inputDir); entries.delete("."); entries.delete("..")
88-
io = Zip::File.open(@outputFile, Zip::File::CREATE);
89-
writeEntries(entries, "", io)
90-
io.close();
87+
def write
88+
entries = Dir.entries(@input_dir) - %w(. ..)
89+
90+
::Zip::File.open(@output_file, ::Zip::File::CREATE) do |io|
91+
write_entries entries, '', io
92+
end
9193
end
92-
# A helper method to make the recursion work.
94+
9395
private
94-
def writeEntries(entries, path, io)
95-
entries.each { |e|
96-
zipFilePath = path == "" ? e : File.join(path, e)
97-
diskFilePath = File.join(@inputDir, zipFilePath)
98-
puts "Deflating " + diskFilePath
99-
if File.directory?(diskFilePath)
100-
io.mkdir(zipFilePath)
101-
subdir =Dir.entries(diskFilePath); subdir.delete("."); subdir.delete("..")
102-
writeEntries(subdir, zipFilePath, io)
96+
97+
# A helper method to make the recursion work.
98+
def write_entries(entries, path, io)
99+
entries.each do |e|
100+
zip_file_path = path == '' ? e : File.join(path, e)
101+
disk_file_path = File.join(@input_dir, zip_file_path)
102+
puts "Deflating #{disk_file_path}"
103+
104+
if File.directory? disk_file_path
105+
recursively_deflate_directory(disk_file_path, io, zip_file_path)
103106
else
104-
io.get_output_stream(zipFilePath) { |f| f.print(File.open(diskFilePath, "rb").read())}
107+
put_into_archive(disk_file_path, io, zip_file_path)
105108
end
106-
}
109+
end
110+
end
111+
112+
def recursively_deflate_directory(disk_file_path, io, zip_file_path)
113+
io.mkdir zip_file_path
114+
subdir = Dir.entries(disk_file_path) - %w(. ..)
115+
write_entries subdir, zip_file_path, io
116+
end
117+
118+
def put_into_archive(disk_file_path, io, zip_file_path)
119+
io.get_output_stream(zip_file_path) do |f|
120+
f.puts(File.open(disk_file_path, 'rb').read)
121+
end
107122
end
108123
end
109124
```
110125

111126
### Save zip archive entries in sorted by name state
112127

113-
To saving zip archives in sorted order like below you need to set `::Zip.sort_entries` to `true`
128+
To save zip archives in sorted order like below, you need to set `::Zip.sort_entries` to `true`
114129

115130
```
116131
Vegetable/
@@ -124,7 +139,7 @@ fruit/mango
124139
fruit/orange
125140
```
126141

127-
After this entries in zip archive will be saved in ordered state.
142+
After this, entries in the zip archive will be saved in ordered state.
128143

129144
### Reading a Zip file
130145

@@ -150,17 +165,17 @@ end
150165

151166
`::Zip::InputStream` usable for fast reading zip file content because it not read Central directory.
152167

153-
But there is one exception when it not working - General Purpose Flag Bit 3.
168+
But there is one exception when it is not working - General Purpose Flag Bit 3.
154169

155170
```
156171
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
157172
```
158173

159-
If `::Zip::InputStream` will found such entry in zip archive it will raise exception.
174+
If `::Zip::InputStream` finds such entry in the zip archive it will raise an exception.
160175

161176
### Password Protection (Experimental)
162177

163-
RubyZip supports reading/writing zip files with traditional zip encryption (a.k.a. "ZipCrypto"). AES encryption is not yet supported. It can be used with buffer streams, e.g.:
178+
Rubyzip supports reading/writing zip files with traditional zip encryption (a.k.a. "ZipCrypto"). AES encryption is not yet supported. It can be used with buffer streams, e.g.:
164179

165180
```ruby
166181
Zip::OutputStream.write_buffer(::StringIO.new(''), Zip::TraditionalEncrypter.new('password')) do |out|
@@ -212,13 +227,13 @@ Additionally, if you want to configure rubyzip to overwrite existing files while
212227
Zip.continue_on_exists_proc = true
213228
```
214229

215-
If you want to store non english names and want to open properly file on Windows(pre 7) you need to set next option:
230+
If you want to store non-english names and want to open them on Windows(pre 7) you need to set this option:
216231

217232
```ruby
218233
Zip.unicode_names = true
219234
```
220235

221-
In some zip date of files stored in incorrect format. You can hide warning about it by using:
236+
Some zip files might have an invalid date format, which will raise a warning. You can hide this warning with the following setting:
222237

223238
```ruby
224239
Zip.warn_invalid_date = false
@@ -231,7 +246,7 @@ Zip.default_compression = Zlib::DEFAULT_COMPRESSION
231246
```
232247
It defaults to `Zlib::DEFAULT_COMPRESSION`. Possible values are `Zlib::BEST_COMPRESSION`, `Zlib::DEFAULT_COMPRESSION` and `Zlib::NO_COMPRESSION`
233248

234-
All settings in same time
249+
You can set multiple settings at the same time by using a block:
235250

236251
```ruby
237252
Zip.setup do |c|
@@ -242,7 +257,7 @@ All settings in same time
242257
end
243258
```
244259

245-
By default Zip64 support is disabled for writing. To enable it do next:
260+
By default, Zip64 support is disabled for writing. To enable it do this:
246261

247262
```ruby
248263
Zip.write_zip64_support = true
@@ -252,7 +267,7 @@ _NOTE_: If you will enable Zip64 writing then you will need zip extractor with Z
252267

253268
## Developing
254269

255-
To run tests you need run next commands:
270+
To run the test you need to do this:
256271

257272
```
258273
bundle install
@@ -279,5 +294,5 @@ extra-field support contributed by Tatsuki Sugiura (sugi at nemui.org)
279294

280295
## License
281296

282-
rubyzip is distributed under the same license as ruby. See
297+
Rubyzip is distributed under the same license as ruby. See
283298
http://www.ruby-lang.org/en/LICENSE.txt

lib/zip/central_directory.rb

100755100644
File mode changed.

lib/zip/compressor.rb

100755100644
File mode changed.

lib/zip/decompressor.rb

100755100644
File mode changed.

lib/zip/deflater.rb

100755100644
File mode changed.

lib/zip/dos_time.rb

100755100644
File mode changed.

lib/zip/entry.rb

100755100644
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,7 @@ def get_input_stream(&block)
496496
end
497497
else
498498
zis = ::Zip::InputStream.new(@zipfile, local_header_offset)
499+
zis.instance_variable_set(:@internal, true)
499500
zis.get_next_entry
500501
if block_given?
501502
begin

lib/zip/entry_set.rb

100755100644
File mode changed.

lib/zip/extra_field.rb

100755100644
File mode changed.

lib/zip/file.rb

100755100644
File mode changed.

lib/zip/filesystem.rb

100755100644
File mode changed.

lib/zip/inflater.rb

100755100644
File mode changed.

lib/zip/input_stream.rb

100755100644
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ def open_entry
130130
end
131131
if @current_entry && @current_entry.gp_flags & 8 == 8 && @current_entry.crc == 0 \
132132
&& @current_entry.compressed_size == 0 \
133-
&& @current_entry.size == 0
133+
&& @current_entry.size == 0 && !@internal
134134
raise GPFBit3Error,
135135
'General purpose flag Bit 3 is set so not possible to get proper info from local header.' + \
136136
'Please use ::Zip::File instead of ::Zip::InputStream'

lib/zip/ioextras.rb

100755100644
File mode changed.

lib/zip/null_compressor.rb

100755100644
File mode changed.

lib/zip/null_decompressor.rb

100755100644
File mode changed.

lib/zip/null_input_stream.rb

100755100644
File mode changed.

lib/zip/output_stream.rb

100755100644
File mode changed.

lib/zip/pass_thru_compressor.rb

100755100644
File mode changed.

lib/zip/pass_thru_decompressor.rb

100755100644
File mode changed.

lib/zip/streamable_directory.rb

100755100644
File mode changed.

lib/zip/streamable_stream.rb

100755100644
File mode changed.

samples/example_recursive.rb

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,42 +7,52 @@
77
#
88
# Usage:
99
# directoryToZip = "/tmp/input"
10-
# outputFile = "/tmp/out.zip"
11-
# zf = ZipFileGenerator.new(directoryToZip, outputFile)
10+
# output_file = "/tmp/out.zip"
11+
# zf = ZipFileGenerator.new(directory_to_zip, output_file)
1212
# zf.write()
1313
class ZipFileGenerator
1414

1515
# Initialize with the directory to zip and the location of the output archive.
16-
def initialize(inputDir, outputFile)
17-
@inputDir = inputDir
18-
@outputFile = outputFile
16+
def initialize(input_dir, output_file)
17+
@input_dir = input_dir
18+
@output_file = output_file
1919
end
2020

2121
# 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);
22+
def write
23+
entries = Dir.entries(@input_dir) - %w(. ..)
2524

26-
writeEntries(entries, "", io)
27-
io.close();
25+
::Zip::File.open(@output_file, ::Zip::File::CREATE) do |io|
26+
write_entries entries, '', io
27+
end
2828
end
2929

30-
# A helper method to make the recursion work.
3130
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)
31+
32+
# A helper method to make the recursion work.
33+
def write_entries(entries, path, io)
34+
entries.each do |e|
35+
zip_file_path = path == '' ? e : File.join(path, e)
36+
disk_file_path = File.join(@input_dir, zip_file_path)
37+
puts "Deflating #{disk_file_path}"
38+
39+
if File.directory? disk_file_path
40+
recursively_deflate_directory(disk_file_path, io, zip_file_path)
4241
else
43-
io.get_output_stream(zipFilePath) { |f| f.puts(File.open(diskFilePath, "rb").read())}
42+
put_into_archive(disk_file_path, io, zip_file_path)
4443
end
45-
}
44+
end
4645
end
4746

48-
end
47+
def recursively_deflate_directory(disk_file_path, io, zip_file_path)
48+
io.mkdir zip_file_path
49+
subdir = Dir.entries(disk_file_path) - %w(. ..)
50+
write_entries subdir, zip_file_path, io
51+
end
52+
53+
def put_into_archive(disk_file_path, io, zip_file_path)
54+
io.get_output_stream(zip_file_path) do |f|
55+
f.puts(File.open(disk_file_path, 'rb').read)
56+
end
57+
end
58+
end
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
require 'test_helper'
2+
require 'fileutils'
3+
require_relative '../../samples/example_recursive'
4+
5+
class ExampleRecursiveTest < MiniTest::Test
6+
DIRECTORY_TO_ZIP = 'test/data/globTest'
7+
OUTPUT_DIRECTORY = 'test/data/example_recursive.zip'
8+
TEMP_DIRECTORY = 'test/data/tmp'
9+
10+
def setup
11+
@generator = ::ZipFileGenerator.new(DIRECTORY_TO_ZIP, OUTPUT_DIRECTORY)
12+
end
13+
14+
def teardown
15+
FileUtils.rm_rf TEMP_DIRECTORY
16+
FileUtils.rm_f OUTPUT_DIRECTORY
17+
end
18+
19+
def test_write
20+
@generator.write
21+
unzip
22+
assert_equal Dir.entries(DIRECTORY_TO_ZIP).sort, Dir.entries(TEMP_DIRECTORY).sort
23+
end
24+
25+
private
26+
27+
def unzip(file = OUTPUT_DIRECTORY)
28+
Zip::File.open(file) do |zip_file|
29+
zip_file.each do |f|
30+
file_path = File.join(TEMP_DIRECTORY, f.name)
31+
FileUtils.mkdir_p(File.dirname(file_path))
32+
33+
zip_file.extract(f, file_path) unless File.exist?(file_path)
34+
end
35+
end
36+
end
37+
end

0 commit comments

Comments
 (0)
0