Skip to content

Instantly share code, notes, and snippets.

@rdm
Created May 25, 2022 10:27
Show Gist options
  • Save rdm/8bf2d2393e1a7776bd265da93f9f41ca to your computer and use it in GitHub Desktop.
Save rdm/8bf2d2393e1a7776bd265da93f9f41ca to your computer and use it in GitHub Desktop.
cocurrent'zip0'
NB. read from "single disk store only" zip files (zip64 standard)
rd=: 1!:11 NB. indexed file read
lend=: [:<.p.&256@(a.&i.)S:0 NB. numbers from little endian representation
plend=: {{
struct1=. (e.~ i.@{:) +/\0,m
struct1 {{
nums=. lend m <;.1 (#m) {.y
struct2=. (e.~ i.@{:) +/\ (#m),n{nums
(]each nums),(#n){.a:,~struct2 <;.1 (#struct2){.y
}} n
}}
getdir=: {{{."1 getCD y}}
getfile=: {{
dir=. getCD y
'usz crc32 offhead'=. (({."1 dir) i.boxopen x) {:: {:"1 dir
({{)n
sig
vext flag method mtm mdt
crc32 csz unsz
nmlen exlen
nm ex
}}-.LF)=. (t=. 1 5 3 2#4 2 4 2) plend _2 _1 offhead}. LASTRAW
assert. 16b04034b50=sig
assert. method=0 NB. compression not supported here
assert. csz=usz NB. compression not supported here
assert. usz=unsz NB. compression not supported here
txt=. usz{.LASTRAW}.~ offhead+(+/t)+nmlen+exlen
assert. crc32=128!:3 txt
}}
LASTCDIR=: LASTRAW=: LASTFILE=: EMPTY NB. efficiency hack
NB. get Central Directory
NB. the master reference for all zip files
getCD=:{{
if. LASTFILE-:y do. LASTCDIR return.end.
raw=. LASTRAW=: fread y
if. _1-: raw do. NB. flush cache on missing file
LASTCDIR=: LASTFILE=: EMPTY return.
end.
off=. 4+{:I.(80 75 5 6{a.) E. raw
rec=. off}.raw
({{)n
nd ns nentries nfiles szcd offcd clen cmt
}}-.LF)=. (4 2 1#2 4 2) plend _1 rec
assert. nd=0 NB. multi-disk archives not supported here
assert. ns=0 NB. multi-disk archives not supported here
assert. nentries=nfiles NB. multi-disk archives not supported here
fh=. parseFileHeaders szcd{.offcd}. raw
assert. nfiles=#fh
LASTFILE=. y
LASTCDIR=. fh
}}
parseFileHeaders=: {{
raw=. y
r=. EMPTY
while.#raw do.
if.0=#y do.r return.end.
assert. (80 75 1 2 {a.)-:4{. raw
(;:{{)n
sig
ver0 ver1 flags method mtim mdat
crc32 csz usz
nmL exL cmL nstart iattrib
eattrib offhead
fnam extra comment
}}-.LF)=. (t=.1 6 3 5 2#4 2 4 2 4) plend 10 11 12 raw
assert. 16b02014b50=sig
assert. method=0 NB. compression not supported here
assert. csz=usz NB. compression not supported here
assert. nstart=0 NB. multi-disk archives not supported here
raw=. raw }.~ (+/t)+nmL+exL+cmL
r=. r,fnam;usz,crc32,offhead
end.
}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment