Class: DooDah::ZipEntry

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
CentralDirectoryHeader, LocalDirectoryHeader
Defined in:
lib/doo_dah/zip_entry.rb

Constant Summary

Constants included from ZipEntryHeader

DooDah::ZipEntryHeader::CENTRAL_ENTRY_HEADER_SIGNATURE, DooDah::ZipEntryHeader::DEFLATED, DooDah::ZipEntryHeader::END_CENTRAL_DIRECTORY_SIGNATURE, DooDah::ZipEntryHeader::GP_FLAGS_CRC_UNKNOWN, DooDah::ZipEntryHeader::GP_FLAGS_UTF8, DooDah::ZipEntryHeader::LOCAL_ENTRY_FOOTER_SIGNATURE, DooDah::ZipEntryHeader::LOCAL_ENTRY_HEADER_SIGNATURE, DooDah::ZipEntryHeader::LOCAL_ENTRY_STATIC_HEADER_LENGTH, DooDah::ZipEntryHeader::LOCAL_ENTRY_TRAILING_DESCRIPTOR_LENGTH, DooDah::ZipEntryHeader::STORED, DooDah::ZipEntryHeader::VERSION_NEEDED_TO_EXTRACT

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from CentralDirectoryHeader

central_header_size, end_of_central_directory_size, version_made_by_size, #write_central_header, #write_end_of_central_directory, #write_version_made_by

Methods included from ZipEntryHeader

common_header_size, name_size, #write_common_header, #write_infozip_utf8_name, #write_name

Methods included from ZipHeader

signature_size, #write_signature

Methods included from LocalDirectoryHeader

local_footer_size, local_header_size, #write_local_footer, #write_local_header

Constructor Details

#initialize(zip_stream, name, size = 0, crc = 0) ⇒ ZipEntry

Returns a new instance of ZipEntry.



20
21
22
23
24
25
26
27
28
29
# File 'lib/doo_dah/zip_entry.rb', line 20

def initialize(zip_stream, name, size = 0, crc = 0)
  @zip_stream = zip_stream
  @name = name
  @crc = 0
  @size = 0
  @expected_crc = crc
  @expected_size = size
  @closed = false
  write_local_header
end

Instance Attribute Details

#closedObject (readonly) Also known as: closed?

Returns the value of attribute closed.



14
15
16
# File 'lib/doo_dah/zip_entry.rb', line 14

def closed
  @closed
end

#nameObject (readonly)

Returns the value of attribute name.



14
15
16
# File 'lib/doo_dah/zip_entry.rb', line 14

def name
  @name
end

Class Method Details

.overhead(name, size = 0, crc = 0) ⇒ Object



9
10
11
12
# File 'lib/doo_dah/zip_entry.rb', line 9

def self.overhead(name, size=0, crc=0)
  LocalDirectoryHeader.local_header_size(name) + ((size == 0 || crc == 0) ? LocalDirectoryHeader.local_footer_size : 0) +
    CentralDirectoryHeader.central_header_size(name)  
end

Instance Method Details

#closeObject



43
44
45
46
47
# File 'lib/doo_dah/zip_entry.rb', line 43

def close
  return if closed
  write_local_footer if local_footer_required?
  @closed = true
end

#crcObject



35
36
37
# File 'lib/doo_dah/zip_entry.rb', line 35

def crc
  @expected_crc != 0 ? @expected_crc : @crc
end

#last_modified_dateObject



59
60
61
# File 'lib/doo_dah/zip_entry.rb', line 59

def last_modified_date
  @last_modified_date ||= Time.now.extend(DosTime::Formatter).to_dos_date
end

#last_modified_timeObject



55
56
57
# File 'lib/doo_dah/zip_entry.rb', line 55

def last_modified_time
  @last_modified_time ||= Time.now.extend(DosTime::Formatter).to_dos_time
end

Returns:

  • (Boolean)


39
40
41
# File 'lib/doo_dah/zip_entry.rb', line 39

def local_footer_required?
  @expected_size == 0 || @expected_crc == 0
end

#sizeObject



31
32
33
# File 'lib/doo_dah/zip_entry.rb', line 31

def size
  @expected_size != 0 ? @expected_size : @size
end

#write_file_data(data) ⇒ Object



49
50
51
52
53
# File 'lib/doo_dah/zip_entry.rb', line 49

def write_file_data(data)
  raise 'Zip entry already closed' if closed
  @size += write(data)
  @crc = Zlib::crc32(data, @crc)
end