Class: Zip::ZipOutputStream

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

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(fileName, stream = false) ⇒ ZipOutputStream

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
# File 'lib/zip/zip_output_stream.rb', line 27

def initialize(fileName, stream=false)
  super()
  @fileName = fileName
  if stream
    @outputStream = StringIO.new
  else
    @outputStream = ::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.



23
24
25
# File 'lib/zip/zip_output_stream.rb', line 23

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.



45
46
47
48
49
50
51
# File 'lib/zip/zip_output_stream.rb', line 45

def ZipOutputStream.open(fileName)
  return new(fileName) unless block_given?
  zos = new(fileName)
  yield zos
ensure
  zos.close if zos
end

.write_buffer {|zos| ... } ⇒ Object

Same as #open but writes to a filestream instead

Yields:

  • (zos)


54
55
56
57
58
# File 'lib/zip/zip_output_stream.rb', line 54

def ZipOutputStream.write_buffer
  zos = new('', true)
  yield zos
  return zos.close_buffer
end

Instance Method Details

#<<(data) ⇒ Object

Modeled after IO.<<



165
166
167
# File 'lib/zip/zip_output_stream.rb', line 165

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

#closeObject

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



61
62
63
64
65
66
67
68
# File 'lib/zip/zip_output_stream.rb', line 61

def close
  return if @closed
  finalize_current_entry
  update_local_headers
  write_central_directory
  @outputStream.close
  @closed = true
end

#close_bufferObject

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



71
72
73
74
75
76
77
78
# File 'lib/zip/zip_output_stream.rb', line 71

def close_buffer
  return @outputStream if @closed
  finalize_current_entry
  update_local_headers
  write_central_directory
  @closed = true
  @outputStream
end

#copy_raw_entry(entry) ⇒ Object

Raises:



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/zip/zip_output_stream.rb', line 98

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

#put_next_entry(entryname, comment = nil, extra = nil, compression_method = ZipEntry::DEFLATED, level = Zlib::DEFAULT_COMPRESSION) ⇒ Object

entry can be a ZipEntry object or a string.

Raises:



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/zip/zip_output_stream.rb', line 82

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
  if entryname.kind_of?(ZipEntry)
    new_entry = entryname
  else
    new_entry = ZipEntry.new(@fileName, entryname.to_s)
  end
  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 if !compression_method.nil?
  init_next_entry(new_entry, level)
  @currentEntry = new_entry
end