Class: Zip::ZipOutputStream

Inherits:
Object show all
Includes:
IOExtras::AbstractOutputStream
Defined in:
lib/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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from IOExtras::AbstractOutputStream

#print, #printf, #putc, #puts, #write

Methods included from IOExtras::FakeIO

#kind_of?

Constructor Details

#initialize(fileish) ⇒ ZipOutputStream

Opens the indicated zipfileish thing. If fileish has an IO-like interface, it becomes the output stream. Otherwise, it is taken to be the name of a file to open for writing. If a file with that name already exists it will be overwritten.



935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
# File 'lib/zip/zip.rb', line 935

def initialize(fileish)
  super()
  @outputStream = if fileish.respond_to?(:tell)
                    @fileName = ''
                    fileish
                  else
                    @fileName = fileish
                    File.new(@fileName, 'wb')
                  end
  @entrySet = ZipEntrySet.new
  @compressor = NullCompressor.instance
  @closed = false
  @currentEntry = nil
  @comment = nil
end

Instance Attribute Details

#commentObject

Returns the value of attribute comment.



929
930
931
# File 'lib/zip/zip.rb', line 929

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.



954
955
956
957
958
959
960
# File 'lib/zip/zip.rb', line 954

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.<<



1052
1053
1054
# File 'lib/zip/zip.rb', line 1052

def << (data)
  @compressor << data
end

#closeObject

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



963
964
965
966
967
968
969
970
971
972
# File 'lib/zip/zip.rb', line 963

def close
  return if @closed
  finalize_current_entry
  update_local_headers
  write_central_directory
  # Don't close a StringIO, because there's no way to reopen it, and
  # the caller may still want to use it.
  @outputStream.close unless @outputStream.is_a? StringIO
  @closed = true
end

#copy_raw_entry(entry) ⇒ Object

Raises:



983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
# File 'lib/zip/zip.rb', line 983

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
  @outputStream << entry.get_raw_input_stream { 
	|is| 
	is.seek(src_pos, IO::SEEK_SET)
	is.read(entry.compressed_size)
  }
  @compressor = NullCompressor.instance
  @currentEntry = nil
end

#put_next_entry(entry, level = Zlib::DEFAULT_COMPRESSION) ⇒ Object

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

Raises:



976
977
978
979
980
981
# File 'lib/zip/zip.rb', line 976

def put_next_entry(entry, 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
end