The zif module implements a gzip file reader. See [gzf] for the gzip file header information.
include ffl/zif.fs
\ Convert gzips modification time to a string
include ffl/dos.fs
dtm-create gzf-dtm
tos-create gzf-tos
: mtime-to-str ( u1 -- c-addr u2 = Convert gzips mtime u1 to the string c-addr u2 )
0 dtm.unix-epoch gzf-dtm dtm-set-with-seconds \ Calculate the date/time
gzf-dtm s" %c" gzf-tos dos-write-format \ Format the date/time string
gzf-tos str-get \ Get the string
;
\ Copy contents gzip file to another file
: copy-to-file ( c-addr u zif -- ior = Copy contents )
>r
r/w create-file throw \ Create the destination file
BEGIN \ Zolang data in gzip file Do
pad 80 r@ zif-read-file
dup 0= IF
over 0>
ELSE
false
THEN
WHILE
drop
over pad -rot write-file throw \ Write data in file
REPEAT
nip
swap close-file throw
rdrop
;
\ Example: Read a gzip file and save the result in a file
\ Create the gzip file reader variable
zif-new value zif1
s" gzipped.gz" zif1 zif-open-file ?dup 0= [IF]
.( gzipped.gz succesfully opened) cr
\ Read the header info
zif1 zif-read-header ?dup 0= [IF]
\ Get the header info
zif1 zif-gzf@
.( Text file : ) dup gzf-text@ . cr
.( Operating system : ) dup gzf-os@ . cr
.( Modification time : ) dup gzf-mtime@ mtime-to-str type cr
.( Name : ) dup gzf-name@ type cr
.( Comment : ) dup gzf-comment@ type cr
gzf-name@ zif1 copy-to-file ?dup 0= [IF]
.( Gzip file is succesfully inflated.) cr
[ELSE]
.( Error during inflation: ) . cr
[THEN]
[THEN]
zif1 zif-close-file drop
[ELSE]
.( Error opening gzipped.gz:) . cr
[THEN]
\ Free the zif variable from the heap
zif1 zif-free