Changed ZipOutputStream.put_next_entry to make it possible to specifiy comments, extra field and compression method.

This commit is contained in:
thomas 2010-01-01 12:43:52 +00:00
parent 14198a40ed
commit d0faedc82b
3 changed files with 40 additions and 8 deletions

5
NEWS
View File

@ -1,3 +1,8 @@
= Version 0.9.4
Changed ZipOutputStream.put_next_entry signature (API CHANGE!). Now
allows comment, extra field and compression method to be specified.
= Version 0.9.3
Fixed: Added ZipEntry::name_encoding which retrieves the character

View File

@ -21,7 +21,7 @@ end
module Zip
VERSION = '0.9.3'
VERSION = '0.9.4'
RUBY_MINOR_VERSION = RUBY_VERSION.split(".")[1].to_i
@ -385,9 +385,14 @@ module Zip
else
@fstype = FSTYPE_UNIX
end
@zipfile, @comment, @compressed_size, @crc, @extra, @compression_method,
@name, @size = zipfile, comment, compressed_size, crc,
extra, compression_method, name, size
@zipfile = zipfile
@comment = comment
@compressed_size = compressed_size
@crc = crc
@extra = extra
@compression_method = compression_method
@name = name
@size = size
@time = time
@follow_symlinks = false
@ -972,11 +977,16 @@ module Zip
# Closes the current entry and opens a new for writing.
# +entry+ can be a ZipEntry object or a string.
def put_next_entry(entry, level = Zlib::DEFAULT_COMPRESSION)
def put_next_entry(entryname, comment = nil, extra = nil, compression_method = ZipEntry::DEFLATED, level = Zlib::DEFAULT_COMPRESSION)
raise ZipError, "zip stream is closed" if @closed
newEntry = entry.kind_of?(ZipEntry) ? entry : ZipEntry.new(@fileName, entry.to_s)
init_next_entry(newEntry, level)
@currentEntry=newEntry
new_entry = ZipEntry.new(@fileName, entryname.to_s)
new_entry.comment = comment if !comment.nil?
if (!extra.nil?)
new_entry.extra = ZipExtraField === extra ? extra : ZipExtraField.new(extra.to_s)
end
new_entry.compression_method = compression_method
init_next_entry(new_entry, level)
@currentEntry = new_entry
end
def copy_raw_entry(entry)

View File

@ -632,6 +632,23 @@ class ZipOutputStreamTest < Test::Unit::TestCase
end
end
def test_put_next_entry
stored_text = "hello world in stored text"
entry_name = "file1"
comment = "my comment"
ZipOutputStream.open(TEST_ZIP.zip_name) do
|zos|
zos.put_next_entry(entry_name, comment, nil, ZipEntry::STORED)
zos << stored_text
end
assert(File.read(TEST_ZIP.zip_name).grep(stored_text))
ZipFile.open(TEST_ZIP.zip_name) do
|zf|
assert_equal(stored_text, zf.read(entry_name))
end
end
def assert_i_o_error_in_closed_stream
assert_raise(IOError) {
zos = ZipOutputStream.new("test_putOnClosedStream.zip")