Class: Zip::ZipOutputStream
- Includes:
- IOExtras::AbstractOutputStream
- Defined in:
- lib/ruby_archive/handlers/rubyzip/zip/zip.rb
Overview
ZipOutputStream is the basic class for writing zip files. It is possible to create a ZipOutputStream object directly, passing the zip file name to the constructor, but more often than not the ZipOutputStream will be obtained from a ZipFile (perhaps using the ZipFileSystem interface) object for a particular entry in the zip archive.
A ZipOutputStream inherits IOExtras::AbstractOutputStream in order to provide an IO-like interface for writing to a single zip entry. Beyond methods for mimicking an IO-object it contains the method put_next_entry that closes the current entry and creates a new.
Please refer to ZipInputStream for example code.
java.util.zip.ZipOutputStream is the original inspiration for this class.
Instance Attribute Summary collapse
-
#comment ⇒ Object
Returns the value of attribute comment.
Class Method Summary collapse
-
.open(fileName) ⇒ Object
Same as #initialize but if a block is passed the opened stream is passed to the block and closed when the block returns.
Instance Method Summary collapse
-
#<<(data) ⇒ Object
Modeled after IO.<<.
-
#close ⇒ Object
Closes the stream and writes the central directory to the zip file.
- #copy_raw_entry(entry) ⇒ Object
-
#initialize(fileName) ⇒ ZipOutputStream
constructor
Opens the indicated zip file.
-
#put_next_entry(entryname, comment = nil, extra = nil, compression_method = ZipEntry::DEFLATED, level = Zlib::DEFAULT_COMPRESSION) ⇒ Object
Closes the current entry and opens a new for writing.
Methods included from IOExtras::AbstractOutputStream
#print, #printf, #putc, #puts, #write
Methods included from IOExtras::FakeIO
Constructor Details
#initialize(fileName) ⇒ ZipOutputStream
Opens the indicated zip file. If a file with that name already exists it will be overwritten.
946 947 948 949 950 951 952 953 954 955 |
# File 'lib/ruby_archive/handlers/rubyzip/zip/zip.rb', line 946 def initialize(fileName) super() @fileName = fileName @outputStream = File.new(@fileName, "wb") @entrySet = ZipEntrySet.new @compressor = NullCompressor.instance @closed = false @currentEntry = nil @comment = nil end |
Instance Attribute Details
#comment ⇒ Object
Returns the value of attribute comment.
942 943 944 |
# File 'lib/ruby_archive/handlers/rubyzip/zip/zip.rb', line 942 def comment @comment end |
Class Method Details
.open(fileName) ⇒ Object
Same as #initialize but if a block is passed the opened stream is passed to the block and closed when the block returns.
960 961 962 963 964 965 966 |
# File 'lib/ruby_archive/handlers/rubyzip/zip/zip.rb', line 960 def ZipOutputStream.open(fileName) return new(fileName) unless block_given? zos = new(fileName) yield zos ensure zos.close if zos end |
Instance Method Details
#<<(data) ⇒ Object
Modeled after IO.<<
1061 1062 1063 |
# File 'lib/ruby_archive/handlers/rubyzip/zip/zip.rb', line 1061 def << (data) @compressor << data end |
#close ⇒ Object
Closes the stream and writes the central directory to the zip file
969 970 971 972 973 974 975 976 |
# File 'lib/ruby_archive/handlers/rubyzip/zip/zip.rb', line 969 def close return if @closed finalize_current_entry update_local_headers write_central_directory @outputStream.close @closed = true end |
#copy_raw_entry(entry) ⇒ Object
992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 |
# File 'lib/ruby_archive/handlers/rubyzip/zip/zip.rb', line 992 def copy_raw_entry(entry) entry = entry.dup raise ZipError, "zip stream is closed" if @closed raise ZipError, "entry is not a ZipEntry" if !entry.kind_of?(ZipEntry) finalize_current_entry @entrySet << entry src_pos = entry.local_entry_offset entry.write_local_entry(@outputStream) @compressor = NullCompressor.instance entry.get_raw_input_stream { |is| is.seek(src_pos, IO::SEEK_SET) IOExtras.copy_stream_n(@outputStream, is, entry.compressed_size) } @compressor = NullCompressor.instance @currentEntry = nil end |
#put_next_entry(entryname, comment = nil, extra = nil, compression_method = ZipEntry::DEFLATED, level = Zlib::DEFAULT_COMPRESSION) ⇒ Object
Closes the current entry and opens a new for writing. entry
can be a ZipEntry object or a string.
980 981 982 983 984 985 986 987 988 989 990 |
# File 'lib/ruby_archive/handlers/rubyzip/zip/zip.rb', line 980 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 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 |