Class: Omnizip::Formats::Zip::LocalFileHeader

Inherits:
Object
  • Object
show all
Includes:
Constants
Defined in:
lib/omnizip/formats/zip/local_file_header.rb

Overview

ZIP Local File Header

Constant Summary

Constants included from Constants

Constants::ATTR_ARCHIVE, Constants::ATTR_DIRECTORY, Constants::CENTRAL_DIRECTORY_SIGNATURE, Constants::COMPRESSION_BZIP2, Constants::COMPRESSION_DEFLATE, Constants::COMPRESSION_DEFLATE64, Constants::COMPRESSION_IMPLODED, Constants::COMPRESSION_LZMA, Constants::COMPRESSION_PPMD, Constants::COMPRESSION_REDUCED_1, Constants::COMPRESSION_REDUCED_2, Constants::COMPRESSION_REDUCED_3, Constants::COMPRESSION_REDUCED_4, Constants::COMPRESSION_SHRUNK, Constants::COMPRESSION_STORE, Constants::COMPRESSION_ZSTANDARD, Constants::DATA_DESCRIPTOR_SIGNATURE, Constants::END_OF_CENTRAL_DIRECTORY_SIGNATURE, Constants::FLAG_DATA_DESCRIPTOR, Constants::FLAG_ENCRYPTED, Constants::FLAG_STRONG_ENCRYPTION, Constants::FLAG_UTF8, Constants::LOCAL_FILE_HEADER_SIGNATURE, Constants::MAX_COMMENT_LENGTH, Constants::UNIX_DIR_PERMISSIONS, Constants::UNIX_EXTRA_FIELD_TAG, Constants::UNIX_FILE_PERMISSIONS, Constants::UNIX_SYMLINK_PERMISSIONS, Constants::VERSION_BZIP2, Constants::VERSION_DEFAULT, Constants::VERSION_DEFLATE, Constants::VERSION_LZMA, Constants::VERSION_MADE_BY_UNIX, Constants::VERSION_MADE_BY_WINDOWS, Constants::VERSION_ZIP64, Constants::ZIP64_END_OF_CENTRAL_DIRECTORY_LOCATOR_SIGNATURE, Constants::ZIP64_END_OF_CENTRAL_DIRECTORY_SIGNATURE, Constants::ZIP64_EXTRA_FIELD_TAG, Constants::ZIP64_LIMIT

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(signature: LOCAL_FILE_HEADER_SIGNATURE, version_needed: VERSION_DEFAULT, flags: 0, compression_method: COMPRESSION_STORE, last_mod_time: 0, last_mod_date: 0, crc32: 0, compressed_size: 0, uncompressed_size: 0, filename_length: 0, extra_field_length: 0, filename: "", extra_field: "") ⇒ LocalFileHeader

Returns a new instance of LocalFileHeader.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/omnizip/formats/zip/local_file_header.rb', line 16

def initialize(
  signature: LOCAL_FILE_HEADER_SIGNATURE,
  version_needed: VERSION_DEFAULT,
  flags: 0,
  compression_method: COMPRESSION_STORE,
  last_mod_time: 0,
  last_mod_date: 0,
  crc32: 0,
  compressed_size: 0,
  uncompressed_size: 0,
  filename_length: 0,
  extra_field_length: 0,
  filename: "",
  extra_field: ""
)
  @signature = signature
  @version_needed = version_needed
  @flags = flags
  @compression_method = compression_method
  @last_mod_time = last_mod_time
  @last_mod_date = last_mod_date
  @crc32 = crc32
  @compressed_size = compressed_size
  @uncompressed_size = uncompressed_size
  @filename_length = filename_length
  @extra_field_length = extra_field_length
  @filename = filename
  @extra_field = extra_field
end

Instance Attribute Details

#compressed_sizeObject

Returns the value of attribute compressed_size.



10
11
12
# File 'lib/omnizip/formats/zip/local_file_header.rb', line 10

def compressed_size
  @compressed_size
end

#compression_methodObject

Returns the value of attribute compression_method.



10
11
12
# File 'lib/omnizip/formats/zip/local_file_header.rb', line 10

def compression_method
  @compression_method
end

#crc32Object

Returns the value of attribute crc32.



10
11
12
# File 'lib/omnizip/formats/zip/local_file_header.rb', line 10

def crc32
  @crc32
end

#extra_fieldObject

Returns the value of attribute extra_field.



10
11
12
# File 'lib/omnizip/formats/zip/local_file_header.rb', line 10

def extra_field
  @extra_field
end

#extra_field_lengthObject

Returns the value of attribute extra_field_length.



10
11
12
# File 'lib/omnizip/formats/zip/local_file_header.rb', line 10

def extra_field_length
  @extra_field_length
end

#filenameObject

Returns the value of attribute filename.



10
11
12
# File 'lib/omnizip/formats/zip/local_file_header.rb', line 10

def filename
  @filename
end

#filename_lengthObject

Returns the value of attribute filename_length.



10
11
12
# File 'lib/omnizip/formats/zip/local_file_header.rb', line 10

def filename_length
  @filename_length
end

#flagsObject

Returns the value of attribute flags.



10
11
12
# File 'lib/omnizip/formats/zip/local_file_header.rb', line 10

def flags
  @flags
end

#last_mod_dateObject

Returns the value of attribute last_mod_date.



10
11
12
# File 'lib/omnizip/formats/zip/local_file_header.rb', line 10

def last_mod_date
  @last_mod_date
end

#last_mod_timeObject

Returns the value of attribute last_mod_time.



10
11
12
# File 'lib/omnizip/formats/zip/local_file_header.rb', line 10

def last_mod_time
  @last_mod_time
end

#signatureObject

Returns the value of attribute signature.



10
11
12
# File 'lib/omnizip/formats/zip/local_file_header.rb', line 10

def signature
  @signature
end

#uncompressed_sizeObject

Returns the value of attribute uncompressed_size.



10
11
12
# File 'lib/omnizip/formats/zip/local_file_header.rb', line 10

def uncompressed_size
  @uncompressed_size
end

#version_neededObject

Returns the value of attribute version_needed.



10
11
12
# File 'lib/omnizip/formats/zip/local_file_header.rb', line 10

def version_needed
  @version_needed
end

Class Method Details

.from_binary(data) ⇒ Object

Parse from binary data



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/omnizip/formats/zip/local_file_header.rb', line 95

def self.from_binary(data)
  signature, version_needed, flags, compression_method,
  last_mod_time, last_mod_date, crc32,
  compressed_size, uncompressed_size,
  filename_length, extra_field_length = data.unpack("VvvvvvVVVvv")

  unless signature == LOCAL_FILE_HEADER_SIGNATURE
    raise Omnizip::FormatError,
          "Invalid local file header signature"
  end

  offset = 30
  filename = data[offset, filename_length].force_encoding("UTF-8")
  offset += filename_length

  extra_field = data[offset, extra_field_length]

  new(
    signature: signature,
    version_needed: version_needed,
    flags: flags,
    compression_method: compression_method,
    last_mod_time: last_mod_time,
    last_mod_date: last_mod_date,
    crc32: crc32,
    compressed_size: compressed_size,
    uncompressed_size: uncompressed_size,
    filename_length: filename_length,
    extra_field_length: extra_field_length,
    filename: filename,
    extra_field: extra_field,
  )
end

Instance Method Details

#directory?Boolean

Check if this is a directory entry

Returns:

  • (Boolean)


47
48
49
# File 'lib/omnizip/formats/zip/local_file_header.rb', line 47

def directory?
  filename.end_with?("/")
end

#encrypted?Boolean

Check if entry is encrypted

Returns:

  • (Boolean)


58
59
60
# File 'lib/omnizip/formats/zip/local_file_header.rb', line 58

def encrypted?
  flags.anybits?(FLAG_ENCRYPTED)
end

#has_data_descriptor?Boolean

Check if data descriptor follows

Returns:

  • (Boolean)


63
64
65
# File 'lib/omnizip/formats/zip/local_file_header.rb', line 63

def has_data_descriptor?
  flags.anybits?(FLAG_DATA_DESCRIPTOR)
end

#header_sizeObject

Size of the header in bytes



130
131
132
# File 'lib/omnizip/formats/zip/local_file_header.rb', line 130

def header_size
  30 + filename_length + extra_field_length
end

#to_binaryObject

Serialize to binary format



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/omnizip/formats/zip/local_file_header.rb', line 73

def to_binary
  @filename_length = filename.bytesize
  @extra_field_length = extra_field.bytesize

  [
    signature,
    version_needed,
    flags,
    compression_method,
    last_mod_time,
    last_mod_date,
    crc32,
    compressed_size,
    uncompressed_size,
    filename_length,
    extra_field_length,
  ].pack("VvvvvvVVVvv") +
    filename.b +
    extra_field.b
end

#utf8?Boolean

Check if UTF-8 encoding is used

Returns:

  • (Boolean)


68
69
70
# File 'lib/omnizip/formats/zip/local_file_header.rb', line 68

def utf8?
  flags.anybits?(FLAG_UTF8)
end

#zip64?Boolean

Check if ZIP64 format is needed

Returns:

  • (Boolean)


52
53
54
55
# File 'lib/omnizip/formats/zip/local_file_header.rb', line 52

def zip64?
  compressed_size == ZIP64_LIMIT ||
    uncompressed_size == ZIP64_LIMIT
end