rubyzip/lib/zip/extra_field/universal_time.rb

48 lines
1.1 KiB
Ruby
Raw Normal View History

module Zip
# Info-ZIP Additional timestamp field
class ExtraField::UniversalTime < ExtraField::Generic
2015-03-21 16:27:44 +08:00
HEADER_ID = 'UT'
register_map
def initialize(binstr = nil)
@ctime = nil
@mtime = nil
@atime = nil
@flag = nil
2015-03-21 15:45:46 +08:00
binstr && merge(binstr)
end
attr_accessor :atime, :ctime, :mtime, :flag
def merge(binstr)
return if binstr.empty?
size, content = initial_parse(binstr)
2015-03-21 15:45:46 +08:00
size || return
2015-03-21 16:27:44 +08:00
@flag, mtime, atime, ctime = content.unpack('CVVV')
2015-03-21 15:45:46 +08:00
mtime && @mtime ||= ::Zip::DOSTime.at(mtime)
atime && @atime ||= ::Zip::DOSTime.at(atime)
ctime && @ctime ||= ::Zip::DOSTime.at(ctime)
end
def ==(other)
@mtime == other.mtime &&
@atime == other.atime &&
@ctime == other.ctime
end
def pack_for_local
2015-03-21 16:27:44 +08:00
s = [@flag].pack('C')
@flag & 1 != 0 && s << [@mtime.to_i].pack('V')
@flag & 2 != 0 && s << [@atime.to_i].pack('V')
@flag & 4 != 0 && s << [@ctime.to_i].pack('V')
s
end
def pack_for_c_dir
2015-03-21 16:27:44 +08:00
s = [@flag].pack('C')
@flag & 1 == 1 && s << [@mtime.to_i].pack('V')
s
end
end
end