Class: Archive::Zip::Codec::Store::Writer

Inherits:
IO::LikeHelpers::DelegatedIO
  • Object
show all
Defined in:
lib/archive/zip/codec/store/writer.rb

Overview

Archive::Zip::Codec::Store#compressor method.

Instance Method Summary collapse

Constructor Details

#initialize(delegate, autoclose: true) ⇒ Writer

Creates a new instance of this class using io as a data sink. io must be writable and must provide a write method as IO does or errors will be raised when performing write operations.



23
24
25
26
27
# File 'lib/archive/zip/codec/store/writer.rb', line 23

def initialize(delegate, autoclose: true)
  super
  @crc32 = 0
  @uncompressed_size = 0
end

Instance Method Details

#data_descriptorObject

Returns an instance of Archive::Zip::DataDescriptor with information regarding the data which has passed through this object to the delegate object. The close or flush methods should be called before using this method in order to ensure that any possibly buffered data is flushed to the delegate object; otherwise, the contents of the data descriptor may be inaccurate.



35
36
37
# File 'lib/archive/zip/codec/store/writer.rb', line 35

def data_descriptor
  DataDescriptor.new(@crc32, @uncompressed_size, @uncompressed_size)
end

#seek(amount, whence = IO::SEEK_SET) ⇒ Object

Allows resetting this object and the delegate object back to the beginning of the stream or reporting the current position in the stream.

Raises Errno::EINVAL unless offset is 0 and whence is either IO::SEEK_SET or IO::SEEK_CUR. Raises Errno::EINVAL if whence is IO::SEEK_SEK and the delegate object does not respond to the rewind method.

Raises:

  • (Errno::ESPIPE)


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/archive/zip/codec/store/writer.rb', line 46

def seek(amount, whence = IO::SEEK_SET)
  assert_open
  raise Errno::ESPIPE if amount != 0 || whence == IO::SEEK_END

  case whence
  when IO::SEEK_SET
    result = super
    return result if Symbol === result
    @crc32 = 0
    @uncompressed_size = 0
    result
  when IO::SEEK_CUR
    @uncompressed_size
  else
    raise Errno::EINVAL
  end
end

#write(buffer, length: buffer.bytesize) ⇒ Object

Writes string to the delegate object and returns the number of bytes actually written. Updates the uncompressed_size and crc32 attributes as a side effect.



67
68
69
70
71
72
73
74
75
76
# File 'lib/archive/zip/codec/store/writer.rb', line 67

def write(buffer, length: buffer.bytesize)
  result = super
  return result if Symbol === result

  @uncompressed_size += result
  buffer = buffer[0, result] if result < buffer.bytesize
  @crc32 = Zlib.crc32(buffer, @crc32)

  result
end