Skip to content

Commit 7d6a6a0

Browse files
committed
Read and unpack the zip header in one go.
1 parent 1a80a7b commit 7d6a6a0

1 file changed

Lines changed: 3 additions & 19 deletions

File tree

arch/UNZIP.LUA

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,6 @@ function unzip(p)
4444

4545
local function S(n) f:seek("cur", n) end
4646

47-
---Read the next 2 bytes in the file as a 16-bit little endian number
48-
local function R16() local a, b = R(2):byte(1, 2) return a + b * 256 end
49-
50-
---Read the next 4 bytes in the file as a 32-bit little endian number
51-
local function R32() local a, b, c, d = R(4):byte(1, 4) return a + b * 256 + c * 65536 + d * 16777216 end
52-
5347
while true do
5448

5549
-- s = Signature
@@ -65,19 +59,9 @@ function unzip(p)
6559
-- of = Output file
6660
-- re = Remaining bytes
6761
-- ec = Expected crc32 checksum
68-
local s, v, fl, cm, cr, cs, us, nl, el, fn, di, of, re, ec = f:read(4)
69-
if not s or #s < 4 then break end
70-
if s ~= "PK\3\4" then break end
71-
72-
v , E = R16() D() -- version
73-
fl, E = R16() D() -- flags
74-
cm, E = R16() D() -- method
75-
S(4) -- skip modification time and date
76-
ec, E = R32() D() -- crc32
77-
cs, E = R32() D() -- compressed size
78-
us, E = R32() D() -- uncompressed size
79-
nl, E = R16() D() -- name length
80-
el, E = R16() D() -- extra name length
62+
local s, v, fl, cm, cr, cs, us, nl, el, fn, di, of, re, ec = f:read(30)
63+
if not s or #s < 30 or not s:find("^PK\3\4") then break end
64+
v, fl, cm, _, _, ec, cs, us, nl, el = string.unpack("<HHHHHIIIHH", s, 5)
8165

8266
fn, E = R(nl) D() -- filename
8367
if el > 0 then S(el) end

0 commit comments

Comments
 (0)