Skip to content

moteus/ZipWriter

Repository files navigation

Library for creating ZIP archive for Lua 5.1/5.2/5.3

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

Build Status Coverage Status Licence

Documentation

Documentation

Dependencies

Core

Optional

Test

Supports

  • write to non seekable stream
  • utf8 file names in archives (required iconv)
  • ZIP64 (does not use stream:seek())

Install

Since version 0.1.5 rockspec does not have any dependencies because there no way to specify them as optional. E.g. if you already have installed lua-zlib and you then install lzlib then your existed code may stop working. So you have to install appropriate library by hand. Also some dependencies need only for specific Lua version.

  • Install zlib binding
luarocks install lua-zlib

or

luarocks install lzlib
  • Lua 5.1/5.2/JIT dependencies
luarocks install struct
  • Lua 5.1 dependencies
luarocks install bit32
  • Install ZipWriter itself
luarocks install zipwriter

Usage

Make simple archive

local ZipWriter = require "ZipWriter"

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()

Reading file from FTP and saving archive on FTP

local ZipWriter = require "ZipWriter"
local FTP = require "socket.ftp"

local ZipStream = ZipWriter.new()

-- write zip file directly to ftp
-- lua 5.1 needs coco
ZipStream:open_writer(ZipWriter.co_writer(function(reader)
  FTP.put{
    -- ftp params ...
    path = 'test.zip';
    src  = reader;
  }
end))

-- read from FTP
FTP.get{
  -- ftp params ...
  path = 'test.txt'
  sink = ZipWriter.sink(ZipStream, 'test.txt', {isfile=true;istext=1})
}

ZipStream:close()

Make encrypted archive

local ZipWriter  = require"ZipWriter"
local AesEncrypt = require"ZipWriter.encrypt.aes"

ZipStream = ZipWriter.new{
  encrypt = AesEncrypt.new('password')
}

-- as before