Class: Zip::CentralDirectory

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Dirtyable
Defined in:
lib/zip/central_directory.rb

Constant Summary collapse

END_OF_CD_SIG =
0x06054b50
ZIP64_END_OF_CD_SIG =
0x06064b50
ZIP64_EOCD_LOCATOR_SIG =
0x07064b50
STATIC_EOCD_SIZE =
22
ZIP64_STATIC_EOCD_SIZE =
56
ZIP64_EOCD_LOC_SIZE =
20
MAX_FILE_COMMENT_SIZE =
(1 << 16) - 1
MAX_END_OF_CD_SIZE =
MAX_FILE_COMMENT_SIZE + STATIC_EOCD_SIZE + ZIP64_EOCD_LOC_SIZE

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Dirtyable

#dirty?, included

Constructor Details

#initialize(entries = EntrySet.new, comment = '') ⇒ CentralDirectory

:nodoc:


31
32
33
34
35
# File 'lib/zip/central_directory.rb', line 31

def initialize(entries = EntrySet.new, comment = '') #:nodoc:
  super(dirty_on_create: false)
  @entry_set = entries.kind_of?(EntrySet) ? entries : EntrySet.new(entries)
  @comment   = comment
end

Instance Attribute Details

#commentObject

Returns the value of attribute comment.


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

def comment
  @comment
end

Instance Method Details

#==(other) ⇒ Object

:nodoc:


68
69
70
71
72
# File 'lib/zip/central_directory.rb', line 68

def ==(other) #:nodoc:
  return false unless other.kind_of?(CentralDirectory)

  @entry_set.entries.sort == other.entries.sort && comment == other.comment
end

#count_entries(io) ⇒ Object

Reads the End of Central Directory Record (and the Zip64 equivalent if needs be) and returns the number of entries in the archive. This is a convenience method that avoids reading in all of the entry data to get a very quick entry count.


63
64
65
66
# File 'lib/zip/central_directory.rb', line 63

def count_entries(io)
  read_eocds(io)
  @size
end

#read_from_stream(io) ⇒ Object


37
38
39
40
# File 'lib/zip/central_directory.rb', line 37

def read_from_stream(io)
  read_eocds(io)
  read_central_directory_entries(io)
end

#write_to_stream(io) ⇒ Object

:nodoc:


42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/zip/central_directory.rb', line 42

def write_to_stream(io) #:nodoc:
  cdir_offset = io.tell
  @entry_set.each { |entry| entry.write_c_dir_entry(io) }
  eocd_offset = io.tell
  cdir_size = eocd_offset - cdir_offset
  if ::Zip.write_zip64_support
    need_zip64_eocd = cdir_offset > 0xFFFFFFFF || cdir_size > 0xFFFFFFFF \
                      || @entry_set.size > 0xFFFF
    need_zip64_eocd ||= @entry_set.any?(&:zip64?)
    if need_zip64_eocd
      write_64_e_o_c_d(io, cdir_offset, cdir_size)
      write_64_eocd_locator(io, eocd_offset)
    end
  end
  write_e_o_c_d(io, cdir_offset, cdir_size)
end