9951 explained code solutions for 126 technologies


luaHow to compress a file to ZIP archive


We'll use zip lib to create zip archive from file:

zip = require 'brimworks.zip'

local zip_arc = zip.open('/tmp/file.zip', zip.CREATE)
file_idx = zip_arc:add('test.txt', "file", '/tmp/test.txt')

zip_arc:close()ctrl + c
require 'brimworks.zip'

load zip lib

zip.open

create new zip archive

/tmp/file.zip

name of the archive to create

zip_arc:add

adds new file to zip archive

test.txt

name of the file in zip archive (you can choose any name)

"file"

type of the object in archive (file in our case as we will compress file)

'/tmp/test.txt'

path to file to compress and save in our archive

zip_arc:close()

close archive handler to write changes on disk


Usage example

zip = require 'brimworks.zip'
os.remove('/tmp/file.zip');
local zip_arc = zip.open('/tmp/file.zip', zip.CREATE)
file_idx = zip_arc:add('test.txt', "file", '/tmp/test.txt')
zip_arc:close()