Merge pull request #123 from ubpb/master
Add config to set the default compression level.
This commit is contained in:
commit
945528172c
|
@ -7,6 +7,7 @@ test/huge.zip
|
||||||
test/centralEntryHeader.bin
|
test/centralEntryHeader.bin
|
||||||
test/data/generated/
|
test/data/generated/
|
||||||
test/deflatertest.bin
|
test/deflatertest.bin
|
||||||
|
test/compressiontest_*.bin
|
||||||
test/dummy.txt
|
test/dummy.txt
|
||||||
test/localEntryHeader.bin
|
test/localEntryHeader.bin
|
||||||
test/okToDeleteMoved.txt
|
test/okToDeleteMoved.txt
|
||||||
|
|
|
@ -164,6 +164,14 @@ If you want to store non english names and want to open properly file on Windows
|
||||||
Zip.unicode_names = true
|
Zip.unicode_names = true
|
||||||
```
|
```
|
||||||
|
|
||||||
|
You can set the default compression level like so:
|
||||||
|
|
||||||
|
```ruby
|
||||||
|
Zip.default_compression = Zlib::DEFAULT_COMPRESSION
|
||||||
|
```
|
||||||
|
|
||||||
|
It defaults to `Zlib::DEFAULT_COMPRESSION`. Possible values are `Zlib::BEST_COMPRESSION`, `Zlib::DEFAULT_COMPRESSION` and `Zlib::NO_COMPRESSION`
|
||||||
|
|
||||||
All settings in same time
|
All settings in same time
|
||||||
|
|
||||||
```ruby
|
```ruby
|
||||||
|
@ -171,6 +179,7 @@ All settings in same time
|
||||||
c.on_exists_proc = true
|
c.on_exists_proc = true
|
||||||
c.continue_on_exists_proc = true
|
c.continue_on_exists_proc = true
|
||||||
c.unicode_names = true
|
c.unicode_names = true
|
||||||
|
c.default_compression = Zlib::BEST_COMPRESSION
|
||||||
end
|
end
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
|
@ -34,7 +34,7 @@ end
|
||||||
|
|
||||||
module Zip
|
module Zip
|
||||||
extend self
|
extend self
|
||||||
attr_accessor :unicode_names, :on_exists_proc, :continue_on_exists_proc, :sort_entries
|
attr_accessor :unicode_names, :on_exists_proc, :continue_on_exists_proc, :sort_entries, :default_compression
|
||||||
|
|
||||||
def reset!
|
def reset!
|
||||||
@_ran_once = false
|
@_ran_once = false
|
||||||
|
@ -42,6 +42,7 @@ module Zip
|
||||||
@on_exists_proc = false
|
@on_exists_proc = false
|
||||||
@continue_on_exists_proc = false
|
@continue_on_exists_proc = false
|
||||||
@sort_entries = false
|
@sort_entries = false
|
||||||
|
@default_compression = ::Zlib::DEFAULT_COMPRESSION
|
||||||
end
|
end
|
||||||
|
|
||||||
def setup
|
def setup
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
module Zip
|
module Zip
|
||||||
class Deflater < Compressor #:nodoc:all
|
class Deflater < Compressor #:nodoc:all
|
||||||
|
|
||||||
def initialize(output_stream, level = ::Zlib::DEFAULT_COMPRESSION)
|
def initialize(output_stream, level = Zip.default_compression)
|
||||||
super()
|
super()
|
||||||
@output_stream = output_stream
|
@output_stream = output_stream
|
||||||
@zlib_deflater = ::Zlib::Deflate.new(level, -::Zlib::MAX_WBITS)
|
@zlib_deflater = ::Zlib::Deflate.new(level, -::Zlib::MAX_WBITS)
|
||||||
|
|
|
@ -84,7 +84,7 @@ module Zip
|
||||||
|
|
||||||
# Closes the current entry and opens a new for writing.
|
# Closes the current entry and opens a new for writing.
|
||||||
# +entry+ can be a ZipEntry object or a string.
|
# +entry+ can be a ZipEntry object or a string.
|
||||||
def put_next_entry(entryname, comment = nil, extra = nil, compression_method = Entry::DEFLATED, level = Zlib::DEFAULT_COMPRESSION)
|
def put_next_entry(entryname, comment = nil, extra = nil, compression_method = Entry::DEFLATED, level = Zip.default_compression)
|
||||||
raise ZipError, "zip stream is closed" if @closed
|
raise ZipError, "zip stream is closed" if @closed
|
||||||
if entryname.kind_of?(Entry)
|
if entryname.kind_of?(Entry)
|
||||||
new_entry = entryname
|
new_entry = entryname
|
||||||
|
@ -129,7 +129,7 @@ module Zip
|
||||||
@compressor = NullCompressor.instance
|
@compressor = NullCompressor.instance
|
||||||
end
|
end
|
||||||
|
|
||||||
def init_next_entry(entry, level = Zlib::DEFAULT_COMPRESSION)
|
def init_next_entry(entry, level = Zip.default_compression)
|
||||||
finalize_current_entry
|
finalize_current_entry
|
||||||
@entry_set << entry
|
@entry_set << entry
|
||||||
entry.write_local_entry(@output_stream)
|
entry.write_local_entry(@output_stream)
|
||||||
|
|
|
@ -669,6 +669,25 @@ class DeflaterTest < Test::Unit::TestCase
|
||||||
assert_equal(txt, inflatedTxt)
|
assert_equal(txt, inflatedTxt)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_default_compression
|
||||||
|
txt = load_file("data/file2.txt")
|
||||||
|
|
||||||
|
Zip.default_compression = ::Zlib::BEST_COMPRESSION
|
||||||
|
deflate(txt, "compressiontest_best_compression.bin")
|
||||||
|
Zip.default_compression = ::Zlib::DEFAULT_COMPRESSION
|
||||||
|
deflate(txt, "compressiontest_default_compression.bin")
|
||||||
|
Zip.default_compression = ::Zlib::NO_COMPRESSION
|
||||||
|
deflate(txt, "compressiontest_no_compression.bin")
|
||||||
|
|
||||||
|
best = File.size("compressiontest_best_compression.bin")
|
||||||
|
default = File.size("compressiontest_default_compression.bin")
|
||||||
|
no = File.size("compressiontest_no_compression.bin")
|
||||||
|
|
||||||
|
assert(best < default)
|
||||||
|
assert(best < no)
|
||||||
|
assert(default < no)
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
def load_file(fileName)
|
def load_file(fileName)
|
||||||
txt = nil
|
txt = nil
|
||||||
|
|
Loading…
Reference in New Issue