dreamworkAPI
Ctrl K
GitHub
classDocs / Zip

Zip

classsharedmenusource
Object → Zip

The zip (.zip) archive.

Parses stored, deflated and LZMA files (ZIP64 too), builds archives with deflated or LZMA files, the files of a parsed archive are copied without recompression.

lua
local zip = Zip()
zip:addFile( "docs/readme.txt", "hello" )

local data = zip:compile()
print( Zip( data ):readFile( "docs/readme.txt" ) ) -- hello

Constructor

Zip(data: string?): Zip

Fields

NameTypeDescription
commentstringThe comment of the archive.
filesZip.File[]The files of the archive.
file_countintegerThe number of files.
file_indexestable<string, integer>The indexes of files in files by their paths.
datastring | nilThe parsed archive data, the content of not yet read files.
SignatureintegerPK\3\4 as little endian uint32.

Methods

addDirectoryAdds the directory to the archive, replaces the existing entry with the same path.
addFileAdds the file to the archive, replaces the existing file with the same path.
compileBuilds the .zip data of the archive.
getFileReturns the file of the archive.
parseReplaces the archive with the parsed .zip data.
readFileReturns the uncompressed content of the file of the archive.
removeFileRemoves the file from the archive.
verifyDecompresses all files and checks their CRC-32 checksums.

#Zip:addDirectory

sharedmenusource
function Zip:addDirectory(directory_path: string): Zip.File

Adds the directory to the archive, replaces the existing entry with the same path.

Directories don't have to be added for their files, but some tools show empty directories only this way.

Parameters
NameTypeDescription
directory_pathstringThe path of the directory inside of the archive, / is appended if missing.
Returns
TypeNameDescription
Zip.FilefileThe added directory entry.

#Zip:addFile

sharedmenuasyncsource
async function Zip:addFile(file_path: string, content: string, level?: integer, method?: Zip.Method): Zip.File

Adds the file to the archive, replaces the existing file with the same path.

The content is compressed right away, it is stored as is if compression doesn't make it smaller.

LZMA uses lzma.compress, so it runs on a separate thread when HolyLib is installed and the call is made from a coroutine.

Throws an error if method is not STORED, DEFLATE or LZMA.

Parameters
NameTypeDescription
file_pathstringThe path of the file inside of the archive, e.g. docs/readme.txt.
contentstringThe file content.
leveloptionalintegerThe compression level ( 0 - 9 ), 0 stores the content without compression, 6 by default for deflate, 5 for LZMA.
methodoptionalZip.MethodThe compression method, 0 (stored), 8 (deflate) or 14 (LZMA), 8 by default.
Returns
TypeNameDescription
Zip.FilefileThe added file.

#Zip:compile

sharedmenunodiscardsource
function Zip:compile(): string

Builds the .zip data of the archive.

Files are written with the extended timestamp, ZIP64 records are added only for more than 65534 files.

Returns
TypeNameDescription
stringdataThe .zip data.

#Zip:getFile

sharedmenunodiscardsource
function Zip:getFile(file_path: string): Zip.File | nil

Returns the file of the archive.

Parameters
NameTypeDescription
file_pathstringThe path of the file inside of the archive.
Returns
TypeNameDescription
Zip.File | nilfileThe file, nil if there is no such file.

#Zip:parse

sharedmenusource
function Zip:parse(data: string)

Replaces the archive with the parsed .zip data.

File contents are not copied, they are read from the data on demand by readFile.

Multi-disk archives are not supported.

Parameters
NameTypeDescription
datastringThe .zip data.

#Zip:readFile

sharedmenunodiscardsource
function Zip:readFile(file_path: string): string | nil

Returns the uncompressed content of the file of the archive.

Throws an error if the file is encrypted, uses an unsupported compression method or its data is malformed.

Parameters
NameTypeDescription
file_pathstringThe path of the file inside of the archive.
Returns
TypeNameDescription
string | nilcontentThe file content, nil if there is no such file.

#Zip:removeFile

sharedmenusource
function Zip:removeFile(file_path: string): boolean

Removes the file from the archive.

Parameters
NameTypeDescription
file_pathstringThe path of the file inside of the archive.
Returns
TypeNameDescription
booleanremovedtrue if the file was removed, false if there is no such file.

#Zip:verify

sharedmenunodiscardsource
function Zip:verify(): boolean, string | nil

Decompresses all files and checks their CRC-32 checksums.

Encrypted files and files with unsupported compression methods are reported as damaged.

Returns
TypeNameDescription
booleanis_validtrue if all files are fine.
string | nilfile_pathThe path of the first damaged file.
Generated from the dreamwork meta branch.