Parent

Zip::OutputStream

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.

Attributes

comment[RW]

Public Class Methods

new(file_name, stream=false) click to toggle source

Opens the indicated zip file. If a file with that name already exists it will be overwritten.

# File lib/zip/output_stream.rb, line 27
def initialize(file_name, stream=false)
  super()
  @file_name = file_name
  @output_stream = if stream
                     iostream = @file_name.dup
                     iostream.reopen
                     iostream.rewind
                     iostream
                   else
                     ::File.new(@file_name, "wb")
                   end
  @entry_set = ::Zip::EntrySet.new
  @compressor = ::Zip::NullCompressor.instance
  @closed = false
  @current_entry = nil
  @comment = nil
end
open(file_name) click to toggle source
# File lib/zip/output_stream.rb, line 49
def open(file_name)
  return new(file_name) unless block_given?
  zos = new(file_name)
  yield zos
ensure
  zos.close if zos
end
write_buffer(io = ::StringIO.new('')) click to toggle source

Same as open but writes to a filestream instead

# File lib/zip/output_stream.rb, line 58
def write_buffer(io = ::StringIO.new(''))
  zos = new(io, true)
  yield zos
  zos.close_buffer
end

Public Instance Methods

<<(data) click to toggle source

Modeled after IO.<<

# File lib/zip/output_stream.rb, line 175
def << (data)
  @compressor << data
  self
end
close() click to toggle source

Closes the stream and writes the central directory to the zip file

# File lib/zip/output_stream.rb, line 66
def close
  return if @closed
  finalize_current_entry
  update_local_headers
  write_central_directory
  @output_stream.close
  @closed = true
end
close_buffer() click to toggle source

Closes the stream and writes the central directory to the zip file

# File lib/zip/output_stream.rb, line 76
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) click to toggle source
# File lib/zip/output_stream.rb, line 103
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.is_a?(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) click to toggle source

Closes the current entry and opens a new for writing. entry can be a ZipEntry object or a string.

# File lib/zip/output_stream.rb, line 87
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
  if entry_name.kind_of?(Entry)
    new_entry = entry_name
  else
    new_entry = Entry.new(@file_name, entry_name.to_s)
  end
  new_entry.comment = comment unless comment.nil?
  unless extra.nil?
    new_entry.extra = ExtraField === extra ? 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

Protected Instance Methods

finish() click to toggle source
# File lib/zip/output_stream.rb, line 168
def finish
  @compressor.finish
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.