Class: HttpZip::Parser::CentralDirectory

Inherits:
Object
  • Object
show all
Defined in:
lib/http_zip/parser/central_directory.rb

Overview

Parses the End Of Central Directory (EOCD) block of a zip file.

Constant Summary collapse

EOCD_BLOCK_IDENTIFIER =
"\x50\x4B\x05\x06"
EOCD64_LOCATOR_BLOCK_IDENTIFER =
"\x50\x4b\x06\x07"
EOCD64_BLOCK_IDENTIFER =
"\x50\x4b\x06\x06"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(end_of_central_directory_bytes) ⇒ CentralDirectory

Create a new instance of CentralDirectory.

Parameters:

  • end_of_central_directory_bytes (String)

    the byte string including the EOCD block



16
17
18
19
20
# File 'lib/http_zip/parser/central_directory.rb', line 16

def initialize(end_of_central_directory_bytes)
  @bytes = end_of_central_directory_bytes

  parse!
end

Instance Attribute Details

#eocd64_offsetObject (readonly)

Returns the value of attribute eocd64_offset.



11
12
13
# File 'lib/http_zip/parser/central_directory.rb', line 11

def eocd64_offset
  @eocd64_offset
end

#offsetObject (readonly)

Returns the value of attribute offset.



11
12
13
# File 'lib/http_zip/parser/central_directory.rb', line 11

def offset
  @offset
end

#sizeObject (readonly)

Returns the value of attribute size.



11
12
13
# File 'lib/http_zip/parser/central_directory.rb', line 11

def size
  @size
end

Instance Method Details

#parse_eocd64!(eocd64_block) ⇒ Object

Read the size and offset of the central directory from a Zip64 EOCD block.

Parameters:

  • eocd64_block (String)

    the byte string including the EOCD block for a zip64 archive

Raises:

  • (ZipError)

    if the byte stream does not contain a valid EOCD64 block



26
27
28
29
30
31
32
# File 'lib/http_zip/parser/central_directory.rb', line 26

def parse_eocd64!(eocd64_block)
  unless eocd64_block.start_with?(EOCD64_BLOCK_IDENTIFER)
    raise ZipError, "EOCD64 record not found"
  end

  @size, @offset = eocd64_block[40..-1].unpack("Q<Q<")
end