Class: Zip::OutputStream
- Inherits:
-
Object
- Object
- Zip::OutputStream
- Includes:
- IOExtras::AbstractOutputStream
- Defined in:
- lib/zip/output_stream.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(file_name, dep_encrypter = nil, encrypter: nil) ⇒ Object
-
.write_buffer(io = ::StringIO.new, dep_encrypter = nil, encrypter: nil) {|zos| ... } ⇒ Object
Same as #open but writes to a filestream instead.
Instance Method Summary collapse
-
#<<(data) ⇒ Object
Modeled after IO.<<.
-
#close ⇒ Object
Closes the stream and writes the central directory to the zip file.
-
#close_buffer ⇒ Object
Closes the stream and writes the central directory to the zip file.
- #copy_raw_entry(entry) ⇒ Object
-
#initialize(file_name, dep_stream = false, dep_encrypter = nil, stream: false, encrypter: nil) ⇒ OutputStream
constructor
Opens the indicated zip file.
-
#put_next_entry(entry_name, comment = nil, extra = nil, compression_method = Entry::DEFLATED, level = Zip.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(file_name, dep_stream = false, dep_encrypter = nil, stream: false, encrypter: nil) ⇒ OutputStream
Opens the indicated zip file. If a file with that name already exists it will be overwritten.
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/zip/output_stream.rb', line 27 def initialize(file_name, dep_stream = false, dep_encrypter = nil, stream: false, encrypter: nil) super() Zip.warn_about_v3_api('Zip::OutputStream.new') if dep_stream || !dep_encrypter.nil? @file_name = file_name @output_stream = if stream || dep_stream iostream = @file_name.dup iostream.reopen(@file_name) iostream.rewind iostream else ::File.new(@file_name, 'wb') end @entry_set = ::Zip::EntrySet.new @compressor = ::Zip::NullCompressor.instance @encrypter = encrypter || dep_encrypter || ::Zip::NullEncrypter.new @closed = false @current_entry = nil @comment = nil end |
Instance Attribute Details
#comment ⇒ Object
Returns the value of attribute comment.
23 24 25 |
# File 'lib/zip/output_stream.rb', line 23 def comment @comment end |
Class Method Details
.open(file_name, dep_encrypter = nil, encrypter: nil) ⇒ Object
53 54 55 56 57 58 59 60 61 62 |
# File 'lib/zip/output_stream.rb', line 53 def open(file_name, dep_encrypter = nil, encrypter: nil) return new(file_name) unless block_given? Zip.warn_about_v3_api('Zip::OutputStream.open') unless dep_encrypter.nil? zos = new(file_name, stream: false, encrypter: (encrypter || dep_encrypter)) yield zos ensure zos.close if zos end |
.write_buffer(io = ::StringIO.new, dep_encrypter = nil, encrypter: nil) {|zos| ... } ⇒ Object
Same as #open but writes to a filestream instead
65 66 67 68 69 70 71 72 |
# File 'lib/zip/output_stream.rb', line 65 def write_buffer(io = ::StringIO.new, dep_encrypter = nil, encrypter: nil) Zip.warn_about_v3_api('Zip::OutputStream.write_buffer') unless dep_encrypter.nil? io.binmode if io.respond_to?(:binmode) zos = new(io, stream: true, encrypter: (encrypter || dep_encrypter)) yield zos zos.close_buffer end |
Instance Method Details
#<<(data) ⇒ Object
Modeled after IO.<<
196 197 198 199 |
# File 'lib/zip/output_stream.rb', line 196 def <<(data) @compressor << data self end |
#close ⇒ Object
Closes the stream and writes the central directory to the zip file
76 77 78 79 80 81 82 83 84 |
# File 'lib/zip/output_stream.rb', line 76 def close return if @closed finalize_current_entry update_local_headers write_central_directory @output_stream.close @closed = true end |
#close_buffer ⇒ Object
Closes the stream and writes the central directory to the zip file
87 88 89 90 91 92 93 94 95 |
# File 'lib/zip/output_stream.rb', line 87 def close_buffer return @output_stream if @closed finalize_current_entry update_local_headers write_central_directory @closed = true @output_stream end |
#copy_raw_entry(entry) ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/zip/output_stream.rb', line 116 def copy_raw_entry(entry) entry = entry.dup raise Error, 'zip stream is closed' if @closed raise Error, 'entry is not a ZipEntry' unless entry.kind_of?(Entry) finalize_current_entry @entry_set << entry src_pos = entry.local_header_offset entry.write_local_entry(@output_stream) @compressor = NullCompressor.instance entry.get_raw_input_stream do |is| is.seek(src_pos, IO::SEEK_SET) ::Zip::Entry.read_local_entry(is) IOExtras.copy_stream_n(@output_stream, is, entry.compressed_size) end @compressor = NullCompressor.instance @current_entry = nil end |
#put_next_entry(entry_name, comment = nil, extra = nil, compression_method = Entry::DEFLATED, level = Zip.default_compression) ⇒ Object
Closes the current entry and opens a new for writing. entry
can be a ZipEntry object or a string.
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/zip/output_stream.rb', line 99 def put_next_entry(entry_name, comment = nil, extra = nil, compression_method = Entry::DEFLATED, level = Zip.default_compression) raise Error, 'zip stream is closed' if @closed new_entry = if entry_name.kind_of?(Entry) entry_name else Entry.new(@file_name, entry_name.to_s) end new_entry.comment = comment unless comment.nil? unless extra.nil? new_entry.extra = extra.kind_of?(ExtraField) ? extra : ExtraField.new(extra.to_s) end new_entry.compression_method = compression_method unless compression_method.nil? init_next_entry(new_entry, level) @current_entry = new_entry end |