summaryrefslogtreecommitdiff
path: root/src/zip.cr
diff options
context:
space:
mode:
authorPaul Duncan <pabs@pablotron.org>2016-08-07 21:47:31 -0400
committerPaul Duncan <pabs@pablotron.org>2016-08-07 21:47:31 -0400
commitf8099f07f3ff1cbb259acb1f31f0a3975a9c3e93 (patch)
treec442b6a385c2aeb869deb405bf3d9c33116ee361 /src/zip.cr
parent09d26629c94f235680ddd11aeb97a44227a1d489 (diff)
downloadzip-crystal-f8099f07f3ff1cbb259acb1f31f0a3975a9c3e93.tar.bz2
zip-crystal-f8099f07f3ff1cbb259acb1f31f0a3975a9c3e93.zip
add dos date/time support
Diffstat (limited to 'src/zip.cr')
-rw-r--r--src/zip.cr19
1 files changed, 13 insertions, 6 deletions
diff --git a/src/zip.cr b/src/zip.cr
index c628f2b..5b6f4c4 100644
--- a/src/zip.cr
+++ b/src/zip.cr
@@ -3,7 +3,10 @@ require "zlib"
#
# TODO:
-# [ ] date/time
+# [x] date/time
+# [x] reader
+# [ ] documentation
+# [ ] full tests
# [ ] zip64
# [ ] legacy unicode (e.g., non-bit 11) path/comment support
# [ ] extra data
@@ -164,9 +167,13 @@ module Zip
module TimeHelper
def write_time(io : IO, time : Time) : UInt32
- # TODO
- 0_u32.to_u16.to_io(io, LE)
- 0_u32.to_u16.to_io(io, LE)
+ year = Math.max(1980, time.year) - 1980
+
+ # convert to dos timestamp
+ ((
+ (year << 25) | (time.month << 21) | (time.day << 16) |
+ (time.hour << 11) | (time.minute << 5) | (time.second >> 1)
+ ) & UInt32::MAX).to_u32.to_io(io, LE)
# return number of bytes written
4_u32
@@ -268,7 +275,7 @@ module Zip
end
# set zlib input buffer
- z.next_in = tmp_buf.pointer(0)
+ z.next_in = tmp_buf.to_unsafe
z.avail_in = tmp_buf.size.to_u32
# write compressed data to dst io
@@ -299,7 +306,7 @@ module Zip
loop do
# set zlib output buffer
- zp.value.next_out = buf.pointer(0)
+ zp.value.next_out = buf.to_unsafe
zp.value.avail_out = buf.size.to_u32
# compress data (TODO: check for error)