zif - gzip file reader

Module Description

The zif module implements a gzip file reader. See [gzf] for the gzip file header information.

Module Words

zif structure

zif% ( -- n )
Get the required space for a zif variable

gzip file reader variable creation, initialisation and destruction

zif-init ( zif -- )
Initialise the gzip file reader variable
zif-(free) ( zif -- )
Free the internal, private variables from the heap
zif-create ( "<spaces>name" -- ; -- zif )
Create a named gzip file reader variable in the dictionary
zif-new ( -- zif )
Create a new gzip file reader variable on the heap
zif-free ( zif -- )
Free the variable from the heap

Module words

zif+input-size! ( u -- )
Set the default input buffer size (default 2kb, min. 1kb)
zif+input-size@ ( -- u )
Get the default input buffer size
zif+output-size! ( u -- )
Set the default output buffer size (default 64kb+4kb, min. 64kb+4kb)
zif+output-size@ ( -- u )
Get the default output buffer size

Member words

zif-gzf@ ( zif -- gzf )
Get the reference to the gzip file header info after zif-read-header

File words

zif-open-file ( c-addr u zif -- ior )
Open an existing gzip file for reading with name c-addr u
zif-read-header ( zif -- ior )
Read the (next) header from the gzip file
zif-read-file ( c-addr1 u1 zif -- u2 ior )
Read/decompress maximum u1 bytes from the file and store those at c-addr1, return the actual read bytes
zif-close-file ( zif -- ior )
Close the file

Inspection

zif-dump ( zif -- )
Dump the variable

Examples

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


generated 03-Jun-2010 by ofcfrth-0.10.0