Ns zlib

From AOLserver Wiki
Jump to navigation Jump to search


ns_zlib - Zlib compression support


ns_zlib compress string

ns_zlib gunzip file

ns_zlib gzip string

ns_zlib gzipfile file

ns_zlib uncompress string


The ns_zlib command enables compressing and uncompressing of strings or files. The command is available if the nszlib.so module is loaded into AOLserver or the libnszlib.so, nszlib.dll, or libnszlib.dylib dynamic library is loaded using the load command in a suitable tclsh such as nstclsh.

  • ns_zlib compress string
This command compresses the given string and returns a Tcl byte array object with the compressed data.
  • ns_zlib gunzip file
This command uncompresses the contents of the given gzipped file and returns a string as the result.
  • ns_zlib gzipfile file
This command is similar to the gzip shell routines, compressing the given file into a new file with the .gz extension. If successful, the original uncompressed file is deleted.
  • ns_zlib uncompress bytearray
This command takes a byte array object which includes compressed data and returns an uncompressed string object.


The following examples demonstrate compressing and uncompressing a string;
    # Compress Tcl string
    set test "This is test string"
    set data [ns_zlib compress $test]
    set test [ns_zlib uncompress $data]
    #--> returns "This is test string"
    # Compress the string into gzip format
    set gzip [ns_zlib gzip $test]
    # Save as gzip file
    set fd [open /tmp/test.gz w]
    fconfigure $fd -translation binary -encoding binary
    puts -nonewline $fd $gzip
    close $fd
    # Uncompress gzipped file
    set test [ns_zlib gunzip /tmp/test.gz]
    #--> returns "This is test string"