DEV Community

Sam
Sam

Posted on • Updated on

Working with ZIP files in LuaRT

LuaRT provides a builtin compression module to work with compressed files in ZIP format, without any additional dependencies.

ZIP archive files

ZIP is a file format that supports lossless compression. A lossless compression algorithm allows the original data to be perfectly reconstructed from the compressed data. A ZIP file contains one or more compressed files, making it an ideal way to reduce the size of large files and keep related files together.

The Zip Object

The LuaRT compression module provides an abstraction of zip files represented as a Zip Object. Only INFLATE/DEFLATE algorithms are supported.
This Object facilitates the creation of ZIP archive files and provides methods and properties for adding and extracting compressed files.

To use the Zip Object, you must first require for the compression module :

-- require the builtin 'compression' module
local compression = require "compression"
Enter fullscreen mode Exit fullscreen mode

Extract files from a ZIP archive

To extract files from an existing ZIP archive, you can use the Zip:extractall() method :

local compression = require "compression"

-- Create a Zip instance to represent the ZIP file 'archive.zip'
local archive = compression.Zip("archive.zip")

-- open the ZIP archive for reading
archive:open("read")

-- extract and uncompress all the files in the current directory
archive:extractall()
Enter fullscreen mode Exit fullscreen mode

You can extract a specific entry with the Zip:extract() method :

-- extract the ZIP entry "extractme.bin" in the current directory 
archive:extract("extractme.bin")
Enter fullscreen mode Exit fullscreen mode

A destination path can optionaly be provided :

-- extract the ZIP entry "data.bin" in the specified directory 
archive:extract("data.bin", "C:\\Extract\\Me\\Here")
Enter fullscreen mode Exit fullscreen mode

Creating a ZIP archive

Zip:open() can be used to create an empty ZIP archive. Files can be added to the archive with the Zip:write() method:

local compression = require "compression"

-- Create a Zip value to represent the ZIP file 'new.zip'
local archive = compression.Zip("new.zip")

-- open the ZIP archive for writing
archive:open("write")

-- add a file to this archive
archive:write("C:\\addme.txt"))
Enter fullscreen mode Exit fullscreen mode

You can recursively add entire directories too :

archive:write("C:\\I_am_a_directory_addme\\"))
Enter fullscreen mode Exit fullscreen mode

Iterating over files in a ZIP archive

Zip object is iterable with the each() function, returning at every iteration the next entry name in the ZIP archive previously opened in "read" mode:

local compmression = require "compression"

-- Create a Zip value to represent the ZIP file 'archive.zip'
local archive= compression.Zip("archive.zip")

-- open the ZIP archive for reading
archive:open("read")

-- Iterate over each ZIP archive entries and extract it
for entry in each(archive) do
   print("Extracting '"..entry.."'")
   archive:extract(entry)
end
Enter fullscreen mode Exit fullscreen mode

Reading ZIP archive entries in memory

ZIP archive entries can also be extracted in-memory using the Zip:read() method :

local compression = require "compression"

-- Create a Zip value to represent the ZIP file 'archive.zip'
local archive = compression.Zip("archive.zip")

-- open the ZIP archive for reading
archive:open("read")

-- print the content of the ZIP archive entry 'README.TXT'
print(archive:read("README.TXT"))
Enter fullscreen mode Exit fullscreen mode

As you can see, LuaRT's compression module provides an easy way to manage ZIP archives. Additionally, you can read the complete compression module module documentation.

Top comments (0)