Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
September 28, 2021 05:09 pm GMT

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 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 objectlocal 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 readingarchive:open("read")-- extract and uncompress all the files in the current directoryarchive: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 writingarchive:open("write")-- add a file to this archivearchive: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 readingarchive:open("read")-- Iterate over each ZIP archive entries and extract itfor 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 readingarchive: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


Original Link: https://dev.to/samyeyo/working-with-zip-files-in-luart-2lm0

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To