From f8099f07f3ff1cbb259acb1f31f0a3975a9c3e93 Mon Sep 17 00:00:00 2001 From: Paul Duncan Date: Sun, 7 Aug 2016 21:47:31 -0400 Subject: add dos date/time support --- src/zip.cr | 19 +++++++++++++------ 1 file 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) -- cgit v1.2.3