Module ZipWriter

Create zip archives.

Based on http://wiki.tcl.tk/15158

Usage:

  •  local ZipWriter = require "ZipWriter"
    
     local function make_reader(fname)
       local f = assert(io.open(fname, 'rb'))
       local chunk_size = 1024
       local desc = { -- `-rw-r-----` on Unix
         istext   = true,
         isfile   = true,
         isdir    = false,
         mtime    = 1348048902, -- lfs.attributes('modification')
         platform = 'unix',
         exattrib = {
           ZipWriter.NIX_FILE_ATTR.IFREG,
           ZipWriter.NIX_FILE_ATTR.IRUSR,
           ZipWriter.NIX_FILE_ATTR.IWUSR,
           ZipWriter.NIX_FILE_ATTR.IRGRP,
           ZipWriter.DOS_FILE_ATTR.ARCH,
         },
       }
       return desc, desc.isfile and function()
         local chunk = f:read(chunk_size)
         if chunk then return chunk end
         f:close()
       end
     end
    
     ZipStream = ZipWriter.new()
     ZipStream:open_stream( assert(io.open('readme.zip', 'w+b')), true )
     ZipStream:write('README.md', make_reader('README.md'))
     ZipStream:close()
    
  •  -- Make encrypted archive
     local ZipWriter  = require"ZipWriter"
     local AesEncrypt = require"ZipWriter.encrypt.aes"
    
     ZipStream = ZipWriter.new{
       encrypt = AesEncrypt.new('password')
     }
    
     -- as before
    

Functions

new (options) Create new `ZipWriter` object
co_writer (fn) Run coroutine and return writer function
co_reader (fn) Run coroutine and return reader function
sink (stream, fname, desc) Create new sink that write result to `ZipWriter`

Tables

COMPRESSION_LEVEL Supported compression levels.
FILE_DESCRIPTION Params that describe file or directory
NIX_FILE_ATTR Extra file attributes for Unix
DOS_FILE_ATTR Extra file attributes for Windows/DOS/FAT32

Class ZipWriter

ZipWriter:set_level (lvl) Set compression level
ZipWriter:open_stream (stream[, autoclose]) Set stream as output
ZipWriter:open_writer (writer[, seek]) Set writer as output
ZipWriter:write (fileName, fileDesc, reader, comment) Add one file to archive.
ZipWriter:close (comment) Close archive.


Functions

new (options)
Create new `ZipWriter` object

Parameters:

  • options table {utf8 = false, zip64 = false, level = DEFAULT}

See also:

co_writer (fn)
Run coroutine and return writer function

Parameters:

  • fn

See also:

Usage:

     writer = co_writer(function(reader) ... end)
co_reader (fn)
Run coroutine and return reader function

Parameters:

  • fn

See also:

Usage:

  •  reader = co_reader(function(writer) ... end)
  • 
     local function put(reader)
       local chunk, err
       while true do
         chunk, err = reader()
         if not chunk then break end
         print(chunk) -- proceed data
       end
     end
    
     local function get(writer)
       local t = {1111,2222,3333,4444,5555}
       for k, v in ipairs(t) do
         writer(tostring(v)) -- send data
       end
       writer() -- EOS
     end
    
     get(ZipWriter.co_writer(put))
     put(ZipWriter.co_reader(get))
sink (stream, fname, desc)
Create new sink that write result to `ZipWriter`

Parameters:

  • stream
  • fname
  • desc

Usage:

     local ZipStream = ZipWriter.new()
    
     -- write to ftp
     --[=[ lua 5.1 needs coco
     ZipStream:open_writer(ZipWriter.co_writer(function(reader)
       FTP.put{
         path = 'test.zip';
         src  = reader;
       }
     end))
     --]=]
    
     -- write to file
     ZipStream:open_stream(assert(io.open('test.zip', 'wb+'))
    
     -- read from FTP
     FTP.get{
       -- ftp params ...
       path = 'test.txt'
       sink = ZipWriter.sink(ZipStream, 'test.txt', {isfile=true;istext=1})
     }
    
     ZipStream:close()

Tables

COMPRESSION_LEVEL
Supported compression levels.

Fields:

  • NO
  • DEFAULT
  • SPEED
  • BEST
FILE_DESCRIPTION
Params that describe file or directory

Fields:

  • isfile boolean
  • isdir boolean
  • istext boolean
  • mtime number last modification time. If nil then os.clock used.
  • ctime number
  • atime number
  • exattrib number or table on Windows it can be result of GetFileAttributes. Also it can be array of flags.
  • platform string
  • data optional string file content

See also:

NIX_FILE_ATTR
Extra file attributes for Unix

Fields:

  • IFIFO named pipe (fifo)
  • IFCHR character special
  • IFDIR directory
  • IFBLK block special
  • IFREG regular
  • IFLNK symbolic link
  • IFSOCK socket
  • ISUID set user id on execution
  • ISGID set group id on execution
  • ISTXT sticky bit
  • IRWXU RWX mask for owner
  • IRUSR R for owner
  • IWUSR W for owner
  • IXUSR X for owner
  • IRWXG RWX mask for group
  • IRGRP R for group
  • IWGRP W for group
  • IXGRP X for group
  • IRWXO RWX mask for other
  • IROTH R for other
  • IWOTH W for other
  • IXOTH X for other
  • ISVTX save swapped text even after use
DOS_FILE_ATTR
Extra file attributes for Windows/DOS/FAT32

Fields:

  • NORMAL Normal file
  • RDONLY Read-only file
  • HIDDEN Hidden file
  • SYSTEM System file
  • VOLID Volume ID
  • SUBDIR Subdirectory
  • ARCH File changed since last archive

Class ZipWriter

ZipWriter:set_level (lvl)
Set compression level

Parameters:

  • lvl table {value=compression level, flag=compression flag, method=compression method }

See also:

ZipWriter:open_stream (stream[, autoclose])
Set stream as output

Parameters:

  • stream have to support write method. Also strean can support seek method.
  • autoclose boolean if true then steam:close is called
ZipWriter:open_writer (writer[, seek])
Set writer as output

Parameters:

  • writer callable object. This function called when there is a new chunk of data.
  • seek callable object.
ZipWriter:write (fileName, fileDesc, reader, comment)
Add one file to archive.

Parameters:

  • fileName string
  • fileDesc FILE_DESCRIPTION
  • reader optional callable must return nil on end of data
  • comment optional string

See also:

ZipWriter:close (comment)
Close archive.

Parameters:

generated by LDoc 1.4.0