Working with ZIP files in LuaRT

LuaRT provides a builtin zip 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 ori…


This content originally appeared on DEV Community and was authored by Sam

LuaRT provides a builtin zip 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 'zip' module provides an abstraction of zip files represented as a Zip Object. Only INFLATE/DEFLATE algorithms are supported.
This module 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 'zip' module :

-- require the builtin 'zip' module for the Zip object
local Zip = require "zip"

Extract files from a ZIP archive

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

local Zip = require "zip"

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

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

-- extract and uncompress all the files in the current directory
archive:extractall()

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

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

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 Zip = require "zip"

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

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

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

You can recursively add entire directories too :

archive:write("C:\\I_am_a_directory_addme\\"))

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 Zip = require "zip"

-- Create a Zip value to represent the ZIP file 'archive.zip'
local archive = 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

Reading ZIP archive entries in memory

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

local Zip = require "zip"

-- Create a Zip value to represent the ZIP file 'archive.zip'
local archive = 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")

As you can see, LuaRT's 'zip' module provides an easy way to manage ZIP archives. Additionally, you can read the complete Zip object specification Zip Object


This content originally appeared on DEV Community and was authored by Sam


Print Share Comment Cite Upload Translate Updates
APA

Sam | Sciencx (2021-09-28T17:09:06+00:00) Working with ZIP files in LuaRT. Retrieved from https://www.scien.cx/2021/09/28/working-with-zip-files-in-luart/

MLA
" » Working with ZIP files in LuaRT." Sam | Sciencx - Tuesday September 28, 2021, https://www.scien.cx/2021/09/28/working-with-zip-files-in-luart/
HARVARD
Sam | Sciencx Tuesday September 28, 2021 » Working with ZIP files in LuaRT., viewed ,<https://www.scien.cx/2021/09/28/working-with-zip-files-in-luart/>
VANCOUVER
Sam | Sciencx - » Working with ZIP files in LuaRT. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/09/28/working-with-zip-files-in-luart/
CHICAGO
" » Working with ZIP files in LuaRT." Sam | Sciencx - Accessed . https://www.scien.cx/2021/09/28/working-with-zip-files-in-luart/
IEEE
" » Working with ZIP files in LuaRT." Sam | Sciencx [Online]. Available: https://www.scien.cx/2021/09/28/working-with-zip-files-in-luart/. [Accessed: ]
rf:citation
» Working with ZIP files in LuaRT | Sam | Sciencx | https://www.scien.cx/2021/09/28/working-with-zip-files-in-luart/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.