Tar
The tar (.tar) archive.
Parses POSIX ustar, pax and GNU archives (long names and base-256 numbers), gzip compressed archives are decompressed on parse.
Builds ustar archives, long paths and out of range values are written with pax headers.
lua Copy
local tar = Tar ()
tar:addDirectory ( "docs" )
tar:addFile ( "docs/readme.txt" , "hello" )
local data = tar:compile ()
print ( Tar ( data ):readFile ( "docs/readme.txt" ) ) -- hello
Constructor Tar (data : string ?): Tar Fields Name Type Description filesTar.File[] The entries of the archive. file_countinteger The number of entries. file_indexestable <string , integer >The indexes of entries in files by their paths. gzippedboolean true if the parsed archive was gzip compressed.datastring | nil The parsed archive data, the content of not yet read entries.
Methods addDirectoryAdds the directory to the archive, replaces the existing entry with the same path. addFileAdds the regular file to the archive, replaces the existing entry with the same path. addSymlinkAdds the symbolic link to the archive, replaces the existing entry with the same path. compileBuilds the .tar data of the archive. getFileReturns the entry of the archive. parseReplaces the archive with the parsed .tar or .tar.gz data. readFileReturns the content of the entry of the archive. removeFileRemoves the entry from the archive.
function Tar:addDirectory (directory_path : string , mode? : integer ): Tar.File
Adds the directory to the archive, replaces the existing entry with the same path.
Parameters
Name Type Description directory_pathstring The path of the directory inside of the archive, / is appended if missing. modeoptional integer The permission bits, 493 (0755) by default.
Returns
Type Name Description Tar.File fileThe added directory.
function Tar:addFile (file_path : string , content : string , mode? : integer ): Tar.File
Adds the regular file to the archive, replaces the existing entry with the same path.
Parameters
Name Type Description file_pathstring The path of the file inside of the archive, e.g. docs/readme.txt. contentstring The file content. modeoptional integer The permission bits, 420 (0644) by default.
Returns
Type Name Description Tar.File fileThe added file.
function Tar:addSymlink (link_path : string , target : string ): Tar.File
Adds the symbolic link to the archive, replaces the existing entry with the same path.
Parameters
Name Type Description link_pathstring The path of the link inside of the archive. targetstring The path the link points to.
Returns
Type Name Description Tar.File fileThe added link.
function Tar:compile (): string
Builds the .tar data of the archive.
Use gzip.compress on the result to get .tar.gz.
Returns
Type Name Description string dataThe .tar data.
function Tar:getFile (file_path : string ): Tar.File | nil
Returns the entry of the archive.
Parameters
Name Type Description file_pathstring The path of the entry inside of the archive.
Returns
Type Name Description Tar.File | nil fileThe entry, nil if there is no such entry.
function Tar:parse (data : string )
Replaces the archive with the parsed .tar or .tar.gz data.
File contents are not copied, they are read from the data on demand by readFile.
Parameters
Name Type Description datastring The .tar or .tar.gz data.
function Tar:readFile (file_path : string ): string | nil
Returns the content of the entry of the archive.
Parameters
Name Type Description file_pathstring The path of the entry inside of the archive.
Returns
Type Name Description string | nil contentThe content, nil if there is no such entry.
function Tar:removeFile (file_path : string ): boolean
Removes the entry from the archive.
Parameters
Name Type Description file_pathstring The path of the entry inside of the archive.
Returns
Type Name Description boolean removedtrue if the entry was removed, false if there is no such entry.