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.
local zip = Zip()
zip:addFile( "docs/readme.txt", "hello" )
local data = zip:compile()
print( Zip( data ):readFile( "docs/readme.txt" ) ) -- helloConstructor
Zip(data: string?): ZipFields
| Name | Type | Description |
|---|---|---|
comment | string | The comment of the archive. |
files | Zip.File[] | The files of the archive. |
file_count | integer | The number of files. |
file_indexes | table<string, integer> | The indexes of files in files by their paths. |
data | string | nil | The parsed archive data, the content of not yet read files. |
Signature | integer | PK\3\4 as little endian uint32. |
Methods
addDirectory | Adds the directory to the archive, replaces the existing entry with the same path. |
addFile | Adds the file to the archive, replaces the existing file with the same path. |
compile | Builds the .zip data of the archive. |
getFile | Returns the file of the archive. |
parse | Replaces the archive with the parsed .zip data. |
readFile | Returns the uncompressed content of the file of the archive. |
removeFile | Removes the file from the archive. |
verify | Decompresses all files and checks their CRC-32 checksums. |
#Zip:addDirectory
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.
| Name | Type | Description |
|---|---|---|
directory_path | string | The path of the directory inside of the archive, / is appended if missing. |
| Type | Name | Description |
|---|---|---|
Zip.File | file | The added directory entry. |
#Zip:addFile
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.
| Name | Type | Description |
|---|---|---|
file_path | string | The path of the file inside of the archive, e.g. docs/readme.txt. |
content | string | The file content. |
leveloptional | integer | The compression level ( 0 - 9 ), 0 stores the content without compression, 6 by default for deflate, 5 for LZMA. |
methodoptional | Zip.Method | The compression method, 0 (stored), 8 (deflate) or 14 (LZMA), 8 by default. |
| Type | Name | Description |
|---|---|---|
Zip.File | file | The added file. |
#Zip:compile
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.
| Type | Name | Description |
|---|---|---|
string | data | The .zip data. |
#Zip:getFile
function Zip:getFile(file_path: string): Zip.File | nil
Returns the file of the archive.
| Name | Type | Description |
|---|---|---|
file_path | string | The path of the file inside of the archive. |
| Type | Name | Description |
|---|---|---|
Zip.File | nil | file | The file, nil if there is no such file. |
#Zip:parse
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.
| Name | Type | Description |
|---|---|---|
data | string | The .zip data. |
#Zip:readFile
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.
| Name | Type | Description |
|---|---|---|
file_path | string | The path of the file inside of the archive. |
| Type | Name | Description |
|---|---|---|
string | nil | content | The file content, nil if there is no such file. |
#Zip:removeFile
function Zip:removeFile(file_path: string): boolean
Removes the file from the archive.
| Name | Type | Description |
|---|---|---|
file_path | string | The path of the file inside of the archive. |
| Type | Name | Description |
|---|---|---|
boolean | removed | true if the file was removed, false if there is no such file. |
#Zip:verify
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.
| Type | Name | Description |
|---|---|---|
boolean | is_valid | true if all files are fine. |
string | nil | file_path | The path of the first damaged file. |