Class: Archive::Zip::DataDescriptor

Inherits:
Object
  • Object
show all
Defined in:
lib/archive/zip/data_descriptor.rb

Overview

Archive::Zip::DataDescriptor is a convenience class which bundles important information concerning the compressed data in a ZIP archive entry and allows easy comparisons between instances of itself.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(crc32, compressed_size, uncompressed_size) ⇒ DataDescriptor

Create a new instance of this class where crc32, compressed_size, and uncompressed_size are all integers representing a CRC32 checksum of uncompressed data, the size of compressed data, and the size of uncompressed data respectively.



12
13
14
15
16
# File 'lib/archive/zip/data_descriptor.rb', line 12

def initialize(crc32, compressed_size, uncompressed_size)
  @crc32 = crc32
  @compressed_size = compressed_size
  @uncompressed_size = uncompressed_size
end

Instance Attribute Details

#compressed_sizeObject (readonly)

A count of the number of bytes of compressed data associated with a set of uncompressed data.



22
23
24
# File 'lib/archive/zip/data_descriptor.rb', line 22

def compressed_size
  @compressed_size
end

#crc32Object (readonly)

A CRC32 checksum over some set of uncompressed data.



19
20
21
# File 'lib/archive/zip/data_descriptor.rb', line 19

def crc32
  @crc32
end

#uncompressed_sizeObject (readonly)

A count of the number of bytes of a set of uncompressed data.



24
25
26
# File 'lib/archive/zip/data_descriptor.rb', line 24

def uncompressed_size
  @uncompressed_size
end

Instance Method Details

#dump(io) ⇒ Object

Writes the data wrapped in this object to io which must be a writable, IO-like object providing a write method. Returns the number of bytes written.



48
49
50
51
52
53
54
55
56
# File 'lib/archive/zip/data_descriptor.rb', line 48

def dump(io)
  io.write(
    [
      crc32,
      compressed_size,
      uncompressed_size
    ].pack('VVV')
  )
end

#verify(other) ⇒ Object

Compares the crc32 and uncompressed_size attributes of this object with like-named attributes of other and raises Archive::Zip::Error for any mismatches.

NOTE: The compressed_size attribute is not checked because encrypted entries may have misleading compressed sizes. Checking only the CRC32 and uncompressed size of the data should be sufficient to ensure that an entry has been successfully extracted.



34
35
36
37
38
39
40
41
42
43
# File 'lib/archive/zip/data_descriptor.rb', line 34

def verify(other)
  unless crc32 == other.crc32 then
    raise Zip::Error,
      "CRC32 mismatch: #{crc32.to_s(16)} vs. #{other.crc32.to_s(16)}"
  end
  unless uncompressed_size == other.uncompressed_size then
    raise Zip::Error,
      "uncompressed size mismatch: #{uncompressed_size} vs. #{other.uncompressed_size}"
  end
end