Making, listing, checking and extracting zip, tar, tar.gz, tar.zst, gzip, zstd and RAR files, with passwords and multi-volume RAR.
import archive
An archive gathers several files into one. Zip compresses each file on its own; tar only puts the files one after another and is usually gzipped afterwards (.tar.gz).
The functions work both in memory and on files. They take the archive data as an address and a length, such as b.data and b.len of a byte buffer.
Yes: the headers, each solid block and each file are checked against their CRC-32; the result is a status code.
Multi-volume sets work for RAR through RarOpenVolumes, for 7z through SevenZipOpenVolumes and for zip through ZipOpenFile (pass the .zip, the .z01 or the .zip.001 piece; the other pieces are found next to it) and written with ZipCreateFileSplit. Passwords work for RAR through RarOpen and for 7z through SevenZipOpen. Zip entries with a password are read through ZipOpenFile and ZipFileSetPassword (ZipCrypto and WinZip AES) and written through ZipFileInput.password.
The compression itself comes from compress; use that package directly to gzip a single file or stream. For reading and writing the files on disk around an archive, see fs.
Limits to know about
•
Zip: zip64 is read and written (64-bit sizes and offsets, the zip64 end record, more than 65535 entries), but the package counts bytes in an int, so in practice an archive and each entry must stay under 2 GB. A bigger entry is listed with its true size, and extracting it reports ZIP_TOO_BIG; ZipCreate returns an empty buffer (and ZipCreateFile false) for input it cannot hold: a negative length, a null address with a length, a name over 65535 bytes, or about 1.9 GB in total. ZipCreate switches to zip64 on its own only for 65535 entries or more; ZipCreate64 always writes the zip64 form, which unzip and 7z read. Methods 0 (stored), 8 (DEFLATE), 12 (bzip2), 14 (LZMA) and 93 (zstd) are read and written, and 9 (Deflate64, what 7-Zip writes with -mm=Deflate64) is read; any other method is reported as ZIP_UNSUPPORTED, and encrypted entries are written as ZipCrypto or WinZip AES-128, AES-192 or AES-256 (ZipFileInput.password and encryption); the in-memory readers refuse them, use ZipOpenFile. ZipExtractByName and ZipExtractByNameMax do not check the CRC-32; ZipExtractVerified, ZipVerify and ZipExtractAll do, and ZipExtractAll skips an entry that fails the check and returns false.
•
Zip names and dates: a name with non-ASCII bytes is written with the UTF-8 flag (general purpose bit 11), so Windows Explorer, unzip and 7-Zip show Persian names correctly. When reading, a name is taken as it is (UTF-8) unless the Info-ZIP "Unicode path" field is present, which then wins. Names from other code pages are not converted. Dates are DOS dates: two second steps, between 1980 and 2107, with no time zone, read here as UTC. Set ZipFileInput.mtime (seconds since 1970) to date an entry; 0, or any time before 1980, writes 1 January 1980. The extra timestamp field is not written. ZipExtractAll sets the modification time of the files it writes from that date, except on Windows and 32 bit systems, and does not touch folders.
•
Tar: names of any length round-trip: a long name is stored in the ustar prefix field when it splits at a slash, otherwise in a PAX path record, and reading understands ustar prefixes, PAX (x and g) and GNU long names and links. Symbolic and hard links are real entries (Entry.kind and Entry.link); devices, pipes and other special entries are listed with kind 4 and skipped by TarExtractAll. Times and modes you set are written (a time past the ustar range goes into a PAX record), and extraction applies the mode and the modification time of files and folders, but not their owner. On Windows symbolic links are not created (the call returns false), hard links become copies and times are not set; times are not set on 32 bit systems either. Entries that are not given a time get 0, and the mode you leave at 0 becomes 644 for files, 755 for folders and 777 for symbolic links.
•
Tar extraction:TarExtractAll skips, and returns false for, a name that is absolute or climbs out with .., a name that passes through a symbolic link it created, a symbolic link whose target is absolute or leaves the target folder (a .. after a name is refused too), and a hard link to a name outside the folder. Set-user-id and similar mode bits are never applied.
•
RAR: RAR 5 archives are read completely: every compression level, solid archives and the built-in filters, with each entry's CRC-32 checked. Older archives (RAR 1.5 to 4.x) are read the same way, including PPMd text compression and the standard filters. Password-protected archives open with RarOpen and a password, including ones whose file names are encrypted; without it they report RAR_ENCRYPTED, and a wrong password reports RAR_BAD_PASSWORD. Multi-volume archives open from the list of volumes (RarOpenVolumes, or RarReadVolumes to load a set from disk); a set with a missing volume reports RAR_MULTIVOLUME for the files that cross it. A file that is both encrypted and split across volumes is read too (checked against the reference unrar on RAR 1.5, 2.0, 3 and 5 sets). The old RAR 1.5 and 2.x passwords work as well, tested with a real WinRAR 2.02 file. The RAR 7 format version 1 (dictionaries over 4 GB, fractional sizes) is read; only a match distance beyond 4 GB has never been run. The in-memory functions need the archive and each entry under 2 GB (a bigger entry reports RAR_TOO_BIG); for bigger RAR 5 files use RarFileOpen, which reads from disk and unpacks in pieces. Extraction restores the Unix permissions and the modification time (on 64-bit systems other than Windows), recreates symbolic links whose target stays inside the destination, and writes hard links and file copies as plain copies; a link that points outside is skipped and reported as RAR_UNSAFE_PATH. New archives are RAR 5 with the files stored, because only WinRAR itself can compress to RAR; any RAR tool opens them.
•
7z: read, and written with stored data only. LZMA, LZMA2, PPMd, BZip2, Deflate, Deflate64, copy, BCJ2, the branch filters (x86, ARM, ARM64, RISC-V and others), byte swap and delta are supported, with AES-256 passwords (headers too); Multi-volume sets (.7z.001) and self-extracting archives (an executable stub before the archive) are read. Unix modes, times and symbolic links are restored on extraction, and a link that would point outside the target folder is refused. The calls that take a pointer and a length hold everything unpacked in memory, so a solid block is capped at about 1 GiB and the dictionary of an LZMA stream may not pass 1 GiB; SevenZipOpenFile has no such limit (see below). Names are UTF-8; the time is the last change.
The guide below shows the usual jobs: zipping a set of files, making and reading a .tar.gz, telling formats apart by their first bytes, and extracting without letting an archive write outside the target folder.
Zip a set of files
Each file you put into a zip is a ZipFileInput: its path inside the archive, the address and length of its bytes, and the method: 8 for DEFLATE, 0 to store it as it is, or 12, 14 and 93 for bzip2, LZMA and zstd (the ZipMethod... constants). MakeZipFileInputLevel also sets a level for those compressors. Every unzip reads bzip2 entries; LZMA and zstd entries are read by 7-Zip and by Python's zipfile (zstd from Python 3.14), but not by older tools such as Info-ZIP unzip, so use them only when you know the reader. A name that ends with / is a folder. ZipCreateFile writes the archive to disk and returns false when it cannot; bzip2, LZMA and zstd entries are streamed through the encoders in std/compress, so an entry of any size needs only the encoder window in memory (about 1 MiB for zstd at the default level, a few MiB for LZMA, one block for bzip2); ZipCreate keeps it in memory, for example to send it as an HTTP response.
archive-guide-bundle.salam
import archive
import compress
funcmain:
html := compress.BufFromStr(
"<html><body><h1>Hello</h1><p>Hello again, hello once more.</p></body></html>\n"
)
css := compress.BufFromStr("body { margin: 0; }\nh1 { margin: 0; }\np { margin: 0; }\n")
logo := compress.GzipCompressStr("pretend this is an image that is already compressed")
mut files := Vector {} asVector<archive.ZipFileInput>
files.Push(
archive.MakeZipFileInput("site/", null, 0, 0)
)
files.Push(archive.MakeZipFileInput("site/index.html", html.data, html.len, 8))
files.Push(archive.MakeZipFileInput("site/style.css", css.data, css.len, 8))
files.Push(archive.MakeZipFileInput("site/logo.bin", logo.data, logo.len, 0))
ifnot archive.ZipCreateFile(files, "site.zip"):
println"could not write site.zip"ret1endmut ok := false
z := archive.ReadBinaryFile("site.zip", ok)
mut entries := archive.ZipList(z.data, z.len)
each e in entries:
if e.is_dir:
println e.name, "(folder)"else:
println e.name, e.size, "->", e.compressed_size, "method", e.method
endend
archive.FreeEntries(entries)
ret0end
Text shrinks well. The logo is already compressed, so it uses method 0: running DEFLATE on it again would only cost time and could even make it bigger. The same rule holds for JPEG, PNG, MP4 and other zips.
Make and read a .tar.gz
Tar does not compress. It puts each file after a 512 byte header and pads its contents to a multiple of 512 bytes, so three small files already take 3.5 KB. The usual answer is to gzip the whole tar, and all that padding then nearly disappears. In memory that is TarCreate followed by compress.GzipCompressBuf; for a tar that is already on disk, GzipCompressFile and GzipDecompressFile do it file to file. TarGzCreate and TarGzCreateFile do both steps at once.
archive-guide-targz.salam
import archive
import compress
funcmain:
readme := compress.BufFromStr("Backup of the config folder.\n")
conf := compress.BufFromStr("port = 8080\nworkers = 4\nport_admin = 8081\n")
mut files := Vector {} asVector<archive.TarFileInput>
files.Push(
archive.MakeTarFileInput("config", null, 0, true)
)
files.Push(archive.MakeTarFileInput("config/README", readme.data, readme.len, false))
files.Push(archive.MakeTarFileInput("config/app.conf", conf.data, conf.len, false))
tar := archive.TarCreate(files)
tgz := compress.GzipCompressBuf(tar)
println"tar:", tar.len, "bytes, tar.gz:", tgz.len, "bytes"
archive.WriteBinaryFileBuf("config.tar.gz", tgz)
mut ok := false
packed := archive.ReadBinaryFile("config.tar.gz", ok)
plain := compress.GzipDecompressBuf(packed, ok)
ifnot ok:
println"config.tar.gz is not valid gzip"ret1endmut entries := archive.TarList(plain.data, plain.len)
each e in entries:
println e.name, e.size, e.is_dir
end
archive.FreeEntries(entries)
app := archive.TarExtractByName(plain.data, plain.len, "config/app.conf", ok)
print compress.BufToStrUnsafe(app)
ret0end
Reading goes the other way: TarGzList, TarGzExtractByName and TarGzExtractAll gunzip and read in one call, and set ok to false when the data is not a valid gzip. They refuse to inflate past 1 GiB; give the Max forms a smaller limit for input you do not trust. Directory entries in a tar have size 0 and is_dir set.
Zstandard works the same way: TarZstdCreate builds the tar and compresses it, and TarZstdList, TarZstdExtractByName and TarZstdExtractAll decompress and read in one call, with Max forms to cap the unpacked size. A .tar.zst made with tar --zstd reads the same way. For a single file there is ZstdCompressFile and ZstdDecompressFile.
Read a 7z archive
7z archives are read, and can be made with stored data (no compression). SevenZipList and SevenZipListStatus list the entries, SevenZipExtractByName unpacks one, and SevenZipExtractAll writes everything under a folder. The LZMA, LZMA2, PPMd, BZip2, Deflate and Deflate64 methods are understood, with BCJ2, the branch filters (x86, ARM, ARM64, RISC-V, PowerPC, SPARC, IA-64), byte swap and delta in front of them, solid blocks, empty files and folders, encrypted data and encrypted headers (AES-256). The signature header, the header, every solid block and every file are checked against their CRC-32. Anti items, which mean "delete this name" in an update archive, are left out.
archive-guide-sevenzip.salam
import archive
import compress
import filepath
import os
const PACKED := """
377abcaf271c0004285eeffde1000000000000002200000000000000b6539000e0009a00455d00271bca866798db2a9932
ecb108f48e70217d76d380887c03113d3cbf05381d9da1ba6b83a1a220c4656aeac3932a993abd9ea11cf8b4a761ae3af5
25cd41b942bff3ee0000000000813307ae0fd29aba3d40c090d2ff7d694d8f17af096379cb439267a35290e659701f3be3
6ecf209c510fa5d7b46a9c1bc2ee771f3e97490b78775d756e5d08a454586e1ec479608d69d1666dfa5b88efa423415340
9765eb30ad5f38ecae89e94cbd79558938ae1d384c90fe7db292aab05a6ec1eb816b7fbbeaf3ea941a1f4ff09d06216330
82fa2e2e1849c8fb9600000017064d0109809400070b01000123030101055d001000000c80ce0a01885a3c9d0000
"""funcnib(c: int): int:
if c >= 48and c <= 57:
ret c - 48endif c >= 97and c <= 102:
ret c - 87endret -1endfuncunhex(s: str): compress.ByteBuf:
mut b := compress.NewByteBuf()
bp := s asu8*
mut hi := -1repeat s.Len() in i:
v := nib(bp[i] asint)
if v < 0:
continueendif hi < 0:
hi = v
else:
compress.BufPush(b, hi * 16 + v)
hi = -1endendret b
endfuncmain:
packed := unhex(PACKED)
println"7z archive:", archive.IsSevenZip(packed.data, packed.len)
mut status := 0mut list := archive.SevenZipListStatus(packed.data, packed.len, status)
println"entries:", list.Len(), archive.SevenZipStatusText(status)
repeat list.Len() in i:
e := list.Get(i)
mut kind := "file"if e.is_dir:
kind = "folder"endprintln" ", e.name, kind, e.sizeend
archive.FreeEntries(list)
mut ok := false
readme := archive.SevenZipExtractByName(packed.data, packed.len, "readme.txt", ok)
println"readme.txt:", ok, readme.len, "bytes"
capped := archive.SevenZipExtractByNameMax(packed.data, packed.len, "readme.txt", 50, ok)
println"capped at 50:", ok, capped.len
dir := filepath.Join(os.TempDir(), "salam_sevenzip_guide")
os.RemoveAll(dir)
println"extracted:", archive.SevenZipExtractAll(packed.data, packed.len, dir)
println os.Exists(filepath.Join(dir, "src/main.salam")),
os.Exists(filepath.Join(dir, "empty.log"))
os.RemoveAll(dir)
mut damaged := compress.BufClone(packed)
compress.BufSet(damaged, damaged.len - 3, compress.BufGet(damaged, damaged.len - 3) ^ 1)
broken := archive.SevenZipExtract(damaged.data, damaged.len, "readme.txt", 0, status)
println"damaged:", archive.SevenZipStatusText(status), broken.len
end
A solid block is one compressed stream holding several files, so reading one file out of it unpacks the whole block; to get many files out of one archive use SevenZipExtractAll, which unpacks each block once. Every call returns a SEVENZIP_... status instead of guessing: a wrong password is SEVENZIP_BAD_PASSWORD, a missing password SEVENZIP_ENCRYPTED, a damaged file SEVENZIP_BAD_CRC or SEVENZIP_CORRUPT. Open an encrypted archive with SevenZipOpen and its password, then use SevenZipEntries, SevenZipRead and SevenZipReadAll:
Tell the format from the first bytes
A file name can lie, the bytes cannot. Every format starts with a fixed signature, so a program that accepts uploads should look at the bytes before it picks a reader:
Format
Signature
Check
zip
50 4b 03 04 (PK) at the start
compare the first four bytes
gzip
1f 8b at the start
compare the first two bytes
xz
fd 37 7a 58 5a 00 at the start
compare the first six bytes
RAR
Rar! at the start
IsRar
7z
37 7a bc af 27 1c at the start (a self-extracting archive has it later)
IsSevenZip
tar
ustar at byte 257
compare five bytes from offset 257
archive-guide-detect.salam
import archive
import compress
funckind(b: compress.ByteBuf): str:
if b.len >= 4and compress.BufGet(b, 0) == 80and compress.BufGet(b, 1) == 75and
compress.BufGet(b, 2) == 3and compress.BufGet(b, 3) == 4:
ret"zip"endif b.len >= 2and compress.BufGet(b, 0) == 31and compress.BufGet(b, 1) == 139:
ret"gzip"endif archive.IsRar(b.data, b.len):
ret"rar"endif b.len >= 262and compress.BufGet(b, 257) == 117and compress.BufGet(b, 258) == 115and
compress.BufGet(b, 259) == 116and compress.BufGet(b, 260) == 97and
compress.BufGet(b, 261) == 114:
ret"tar"endret"unknown"endfunccount(b: compress.ByteBuf): int:
k := kind(b)
if k == "zip": ret archive.ZipList(b.data, b.len).Len() endif k == "tar": ret archive.TarList(b.data, b.len).Len() endif k == "rar": ret archive.RarList(b.data, b.len).Len() endif k == "gzip":
mut ok := false
inner := compress.GzipDecompressBuf(b, ok)
if ok: retcount(inner) endendret0endfuncmain:
note := compress.BufFromStr("a short note\n")
mut zf := Vector {} asVector<archive.ZipFileInput>
zf.Push(
archive.MakeZipFileInput("note.txt", note.data, note.len, 8)
)
mut tf := Vector {} asVector<archive.TarFileInput>
tf.Push(
archive.MakeTarFileInput("note.txt", note.data, note.len, false)
)
tar := archive.TarCreate(tf)
samples := [archive.ZipCreate(zf), tar, compress.GzipCompressBuf(tar), note]
each s in samples:
printlnkind(s), s.len, "bytes,", count(s), "entries"endend
Output
zip 129 bytes, 1 entries
tar 2048 bytes, 1 entries
gzip 100 bytes, 1 entries
unknown 13 bytes, 0 entries
The gzip sample is the tar from the line above it, gzipped, so the program opens it and counts the entries of the tar inside. A plain text file matches nothing.
Extract safely
ZipExtractAll and TarExtractAll create the folders they need under the target directory. An archive from someone else can hold names like ../escape.txt or /etc/passwd that would land outside it; both functions skip such entries, write the rest, and return false so you know something was left out. ZipExtractByName and TarExtractByName set ok to false when the name is not in the archive.
archive-guide-extract.salam
import archive
import compress
funcmain:
a := compress.BufFromStr("first file\n")
b := compress.BufFromStr("second file\n")
mut files := Vector {} asVector<archive.ZipFileInput>
files.Push(
archive.MakeZipFileInput("docs/a.txt", a.data, a.len, 8)
)
files.Push(archive.MakeZipFileInput("docs/deep/b.txt", b.data, b.len, 8))
z := archive.ZipCreate(files)
println"all extracted:", archive.ZipExtractAll(z.data, z.len, "out")
mut ok := false
back := archive.ReadBinaryFile("out/docs/deep/b.txt", ok)
print"out/docs/deep/b.txt:", compress.BufToStrUnsafe(back)
_ := archive.ZipExtractByName(z.data, z.len, "docs/c.txt", ok)
println"docs/c.txt found:", ok
mut evil := Vector {} asVector<archive.ZipFileInput>
evil.Push(
archive.MakeZipFileInput("../escape.txt", a.data, a.len, 0)
)
evil.Push(archive.MakeZipFileInput("safe.txt", a.data, a.len, 0))
ez := archive.ZipCreate(evil)
println"all extracted:", archive.ZipExtractAll(ez.data, ez.len, "out2")
_ := archive.ReadBinaryFile("escape.txt", ok)
println"escape.txt written:", ok
_ := archive.ReadBinaryFile("out2/safe.txt", ok)
println"out2/safe.txt written:", ok
end
Output
all extracted: true
out/docs/deep/b.txt: second file
docs/c.txt found: false
all extracted: false
escape.txt written: false
out2/safe.txt written: true
A false result from an extract call is therefore not always a disk error: check the entry names with ZipList or TarList when you need to tell the two apart.
Gzip a single file (.gz)
A .gz holds one file, not a folder. GzipCompressFile and GzipDecompressFile go from a path to a path and return false when they cannot read, write or decode; in memory, compress.GzipCompressBuf and compress.GzipDecompressBuf do the same on a buffer. Decompressing checks the trailer, so a damaged or cut file is refused instead of giving back wrong bytes. For data from someone else use compress.GzipDecompressMax, which stops at a size you choose, so a tiny file cannot expand into gigabytes.
archive-guide-gzip.salam
import archive
import compress
funcmain:
note := compress.BufFromStr(
"a log line that repeats, a log line that repeats, a log line that repeats\n"
)
archive.WriteBinaryFileBuf("note.txt", note)
println"gzip a file:", archive.GzipCompressFile("note.txt", "note.txt.gz")
println"gunzip it:", archive.GzipDecompressFile("note.txt.gz", "note-copy.txt")
mut ok := false
copy := archive.ReadBinaryFile("note-copy.txt", ok)
println"same bytes:", compress.BufEquals(copy, note)
packed := compress.GzipCompressBuf(note)
println"in memory:", note.len, "bytes become", packed.len
back := compress.GzipDecompressBuf(packed, ok)
println"round trip:", ok, compress.BufEquals(back, note)
p := packed.data asu8*
p[packed.len / 2] = ((p[packed.len / 2] asint) ^ 1) asu8
_ := compress.GzipDecompressBuf(packed, ok)
println"damaged copy accepted:", ok
capped := compress.GzipCompressBuf(note)
_ := compress.GzipDecompressMax(capped.data, capped.len, 10, ok)
println"over a 10 byte limit accepted:", ok
end
Output
gzip a file: true
gunzip it: true
same bytes: true
in memory: 74 bytes become 48
round trip: true true
damaged copy accepted: false
over a 10 byte limit accepted: false
To compress several files, put them in a tar first (the .tar.gz section above). To open a .tar.gz you do the same two steps backwards.
Check an archive
"Check" means two different things. To see what is inside without unpacking, list it: ZipList, TarList and RarList (or RarEntries) give every name, size, folder flag and, for zip and RAR, the stored CRC-32; free the list with FreeEntries. To know the data is intact, you need a checksum, and formats differ:
•
gzip and RAR verify themselves. A gzip call returns ok = false; a RAR call sets the status to RAR_BAD_CRC, or RAR_BAD_PASSWORD when a password was wrong.
•
zip stores a CRC-32 for each entry. ZipExtractVerified compares it with the data and sets status to ZIP_OK, ZIP_BAD_CRC and so on; ZipVerify checks every entry and returns how many failed; ZipBadEntries lists them. ZipExtractByName stays unchecked, for speed and for old code: compare compress.CRC32Buf(data) with Entry.crc32 yourself, as the example below does.
•
tar has no checksum for the contents. All you can check is that the listing is sane (names, sizes); for integrity gzip the tar and rely on the gzip trailer, or keep a hash next to the file.
archive-guide-check.salam
import archive
import compress
funcmain:
a := compress.BufFromStr("first file, stored as it is\n")
b := compress.BufFromStr("second file, a little longer than the first one\n")
mut files := Vector {} asVector<archive.ZipFileInput>
files.Push(
archive.MakeZipFileInput("a.txt", a.data, a.len, 0)
)
files.Push(archive.MakeZipFileInput("b.txt", b.data, b.len, 0))
z := archive.ZipCreate(files)
p := z.data asu8*
p[40] = ((p[40] asint) ^ 1) asu8mut entries := archive.ZipList(z.data, z.len)
mut ok := falseeach e in entries:
data := archive.ZipExtractByName(z.data, z.len, e.name, ok)
good := compress.CRC32Buf(data) == e.crc32
println e.name, "extracted:", ok, "checksum matches:", good
end
archive.FreeEntries(entries)
mut rar_files := Vector {} asVector<archive.RarFileInput>
rar_files.Push(
archive.MakeRarFileInput("a.txt", a.data, a.len, false)
)
r := archive.RarCreate(rar_files)
q := r.data asu8*
q[r.len - 12] = ((q[r.len - 12] asint) ^ 1) asu8mut status := 0
_ := archive.RarExtract(r.data, r.len, "a.txt", 0, status)
println"rar:", archive.RarStatusText(status)
end
In the example one byte of the first zip entry was changed. Extraction still reports success for it, but its checksum no longer matches, while the second entry is fine. The RAR entry with the same damage is refused by the extractor itself.
The verified calls do that comparison for you. ZipVerify sets checked to the number of files it inflated and returns how many were bad, or -1 when the data is not a zip; ZipStatusText turns a status into a sentence.
archive-guide-zipverify.salam
import archive
import compress
funcmain:
text := "hello archive, hello archive, hello archive"
b := compress.BufFromStr(text)
mut files := Vector {} asVector<archive.ZipFileInput>
files.Push(
archive.MakeZipFileInput("a.txt", b.data, b.len, 0)
)
files.Push(archive.MakeZipFileInput("b.txt", b.data, 5, 8))
z := archive.ZipCreate(files)
mut status := 0mut checked := 0
good := archive.ZipExtractVerified(z.data, z.len, "a.txt", 0, status)
println status == archive.ZIP_OK, good.len, archive.ZipVerify(z.data, z.len, checked), checked
bp := z.data asu8*
bp[35] = 0x58 asu8
bad := archive.ZipExtractVerified(z.data, z.len, "a.txt", 0, status)
println status == archive.ZIP_BAD_CRC, bad.len, archive.ZipStatusText(status)
mut ok := false
plain := archive.ZipExtractByName(z.data, z.len, "a.txt", ok)
println ok, plain.len
println archive.ZipVerify(z.data, z.len, checked), checked
mut broken := archive.ZipBadEntries(z.data, z.len)
each e in broken:
println"damaged:", e.name
end
archive.FreeEntries(broken)
_ := archive.ZipExtractVerified(z.data, z.len, "missing.txt", 0, status)
println status == archive.ZIP_NOT_FOUND, archive.ZipStatusText(status)
end
Output
true 43 0 2
true 0 checksum mismatch
true 43
1 2
damaged: a.txt
true entry not found
The next example covers dates and zip64: ZipFileInput.mtime sets an entry's date, Entry.mtime reads it back, and ZipCreate64 writes the zip64 form.
archive-guide-ziptime.salam
import archive
import compress
funcmain:
text := "salam, salam, salam"
b := compress.BufFromStr(text)
mut named := archive.MakeZipFileInput("سلام.txt", b.data, b.len, 8)
named.mtime = 1700000000asi64mut files := Vector {} asVector<archive.ZipFileInput>
files.Push(named)
files.Push(archive.MakeZipFileInput("plain.txt", b.data, b.len, 8))
z := archive.ZipCreate(files)
mut entries := archive.ZipList(z.data, z.len)
each e in entries:
println e.name, e.mtime
end
archive.FreeEntries(entries)
println"utf8 flag:", (archive.LEGet16(z.data, 6) & 0x800) != 0
wide := archive.ZipCreate64(files)
mut again := archive.ZipList(wide.data, wide.len)
println again.Len(), wide.len > z.len, again.Get(0).size
archive.FreeEntries(again)
mut checked := 0println archive.ZipVerify(wide.data, wide.len, checked), checked
end
Reading goes through RarList and RarExtract, the same way as zip. The sample below is a small solid RAR 5 made by WinRAR: the second file is packed against the first, so RarExtract unpacks both to reach it. Every entry is checked against its CRC-32, and the status says what went wrong instead of returning silent garbage.
archive-guide-rar.salam
import archive
import compress
const SAMPLE := """
526172211a07010009efc86f0b0105070406010180808000f59006092302030bc300049c04a48302dcdfaa7280050105
612e7478740a031341fff06a00f2f92ec4de405543522f75043ded152822995a044050507ca7cbf17ff3c348ad0d2727
655f99a332a4c1ac1c51cad46312e761917434a437902ab22276d9e9f3b0b2cd03eb58c5ad77d22302030b9c0004b104
a4830220abc84ec0050105622e7478740a031341fff06ae4e7fa2e440719f5c9e63f0caebd274804c54d756dc27b960e
6971f66c94d170988ae3092702030b8000048000a4830200000000c0050109656d7074792e7478740a031341fff06ae4
e7fa2e1d77565103050400
"""funcdigit(c: int): int:
if c >= 48and c <= 57:
ret c - 48endif c >= 97and c <= 102:
ret c - 87endret -1endfuncfrom_hex(text: str): compress.ByteBuf:
mut out := compress.NewByteBuf()
bytes := text asu8*
mut high := -1repeat text.Len() in i:
v := digit(bytes[i] asint)
if v < 0:
continueendif high < 0:
high = v
else:
compress.BufPush(out, high * 16 + v)
high = -1endendret out
endfuncmain:
rar := from_hex(SAMPLE)
println archive.IsRar(rar.data, rar.len)
mut entries := archive.RarList(rar.data, rar.len)
each e in entries:
println e.name, e.size, e.compressed_size
end
archive.FreeEntries(entries)
mut status := 0
b := archive.RarExtract(rar.data, rar.len, "b.txt", 0, status)
println archive.RarStatusText(status), b.len
end
Making a RAR is simpler: build RarFileInput values and call RarCreate or RarCreateFile. The result is a valid RAR 5 archive with the files stored uncompressed. If you need real compression, use zip, or .tar.gz as shown above.
Open a password-protected RAR
A RarArchive remembers the data and the password, so the list, a single read and a full extraction share them. With the wrong password the archive does not open (when the names are encrypted) or each read fails with RAR_BAD_PASSWORD; with none at all you get RAR_ENCRYPTED. RAR 5 checks the password up front. RAR 3 and 4 have no check value, so for them a wrong password shows up as a failed checksum, and it is reported the same way.
archive-guide-rar-password.salam
import archive
import compress
const SAMPLE := """
526172211a07010038d1db9021040000010fe945634161dc2d7b3e8b87f362874d32048313f00358e483efb2886b026e
590dc3a9e23be3b7cfa25ea1dd6a6c4c4af8702cb2d51eb2f7798221661b321c792f762b84ffbf221b7100d324e8f848
ca96b61074ef27c65ba5a760024e3e27ccd59be9d8047bce7da160956a1dd1ca59d5970613a92c4582b076eb3398a1b7
8ef8151f33cf3665dea91210eaede8f1c08f503c488358eb375bf44e0a7a90b81067d6a6637ae589c00f19b387d3d769
f8cfa327f9b218211f11fe63a7a1783e45656fcc9573869eaf359485c0e2d4d5066ddc9981c7b13390c6d78d0ea24a04
386db10eb6d52567002ad55ebee7f1a172080c6a7bd1c72ac29e74d0480d38934012ee49a5fcd5111ad5899223384b67
b8491eed31431d7295083a332114430d96e4004c3426c450dbd1985644df90632ebb89c94cb9f9000fe8b54e0335aa44
fe401e45aad3727aee1392db44b09094b58754bc5ae4fd2186aeb6790bbf2dc3a4225dd37203f3eeecf02fcfb2f150a1
4293fbab1e38425c7bdd1909e3b24e6882fb38344a28184e8d993d93616aeb3eb57e60b5681280a7d138329a4cda
"""funcdigit(c: int): int:
if c >= 48and c <= 57:
ret c - 48endif c >= 97and c <= 102:
ret c - 87endret -1endfuncfrom_hex(text: str): compress.ByteBuf:
mut out := compress.NewByteBuf()
bytes := text asu8*
mut high := -1repeat text.Len() in i:
v := digit(bytes[i] asint)
if v < 0:
continueendif high < 0:
high = v
else:
compress.BufPush(out, high * 16 + v)
high = -1endendret out
endfuncmain:
rar := from_hex(SAMPLE)
mut locked := archive.RarOpen(rar.data, rar.len, "")
mut names := archive.RarEntries(locked)
println names.Len(), archive.RarStatusText(locked.status)
archive.FreeEntries(names)
mut wrong := archive.RarOpen(rar.data, rar.len, "guess")
names = archive.RarEntries(wrong)
println names.Len(), wrong.status == archive.RAR_BAD_PASSWORD
archive.FreeEntries(names)
mut arc := archive.RarOpen(rar.data, rar.len, "secret")
names = archive.RarEntries(arc)
each e in names:
data := archive.RarRead(arc, e.name, 0)
println e.name, e.size, data.len, archive.RarStatusText(arc.status)
end
archive.FreeEntries(names)
println archive.RarReadAll(arc, "unlocked"), archive.RarStatusText(arc.status)
end
Output
0 encrypted data and no password
0 true
n1.txt 352 352 ok
n2.txt 348 348 ok
2 ok
Read a multi-volume RAR
A big archive is often cut into volumes: name.part01.rar, name.part02.rar, and so on, or the older name.rar, name.r00, name.r01. RarReadVolumes takes the first file name, finds the rest by that naming and loads them; RarOpenVolumes turns the list into a RarArchive, and the usual RarEntries and RarRead work across the cuts. Close the archive with RarClose and free the volumes with RarFreeVolumes.
archive-guide-rar-volumes.salam
import archive
import compress
const VOL1 := """
526172211a0701006de128270b01050701060101808080002c64b9412502130be78600048df500a48302c8ac493d8005
0105762e7478740a0313c60bf16a7ed75827cc5dc30845643444334f3565365536a7751848c413a8c1d5a93ff0a349ca
1575f48991859458af2f2ba874489a028b323f09e3cf1e32ffefaf9f97e7e9e9e3edf6f1fa579a7a7d7c3dbdfcbebe3e
7f9f0fa7dbc3d3d3c16d1eff9ff7f7f2fd6ec95fb7f5e3f169de0de1e7efea4f755ebf7e5fe7d7f1fbe5efdec9ebf7f0
ac07f5f8f3ffbd1b1bddbb560ebd7589d7ab57eac6759257a2be6b5bff06db5907b7bfa37e2771d6115e7d65b5b1ead5
bc777f89dd96b66f649d2dbd8d5fb76ce932faf6d637ab6eb48ebc7784ebcd71b4dd9bcee1bd1377eeefbd11bfaa6188
98a280bb6b6d5a47dab204c174a6eafdbcad94b9666ab7f59414fe5a1970fab96f34eaf29a0edb711056b5ceb7494e85
b83775441bc51f56f4dc85bb03667c21d75603e2af914e76c0fb7fb5f428b8fa03bdef2854a35f5ad1273344a09bfacf
3d171047a766aa0d8c11896e85ced5a3fffcee5af7d608ba9d99ccb1aa2be1aa1342ae8b3ae9744562654f1606e38681
3201eddbf1afb701e2c1bbbff9efe02d2e17480a0962ab9b850cc218ea736bad10726ac6adab51e376d55ac13a036d61
8ec3818e63acd5ef5acb6746eae8ead37a7bb694b7a4c144844a6fa10afaf9603e08b482196964dcc8844ab4e013abdb
b4253899ef86069c3bb146ea4f805c057d5d03e101866c22c2413f3901cef35cb2faa62459ed943aafe3345d4aac4411
bb6c32bd58e376ad8393fda6c9ab56441546a250934dc2b662a55ca908f1fb6b12556784fd827901ad81c55caab012f8
0a35c584d114212450a1a8e1dc067711a39dda12659ce0d861a2ce3fafba224e2122c97cd0f5f92425ddfb9c36c9c953
6714555e7051d7dc252c7646f0df25808c58e78b266f0b92b8b917e2c910c0c3af16037381d7939fc090853cdd8df086
7370634378a2c228d4a8c0273788104daf093c9e6dccb0410dceb0215c0610eebaf222187780368a3dd2303226e99cf9
803e25ff56547e270704384ae9021fbc501683ba7cb179dcc0ae76d5600476f4c2ac82942f313d6213c24edbc0445454
d01f0c5e11d1f2ab6437eeafd08d1a3e40e3b6028205a2d7c3383a23c3fad885328bc65c5718d8d8630cfafc1b88c05c
bceb14fad1feaf94c163c9738d355824356adba1ca97a4a959832f630220a908b8b815c8b9acf685c0a255b735826132
911a80b69b45726abd4f52371614f9e267fe5840155b011bb28b47512603050401000000000000000000000000000000
000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000
"""const VOL2 := """
526172211a070100a9c2c4ca0c01050703010601018080800064563ccc25021b0be68600048df500a483020002230880
050105762e7478740a0313c60bf16a7ed75827046840f3834518cedc2f96f6bbb89190a3b6d9c514aacde7ee173d264c
b8f0692c7ca6b792780ee2bef245c9c3c949222294d2e016d242cd89838cbfa39edcb53775439267227627f0b5b02174
c9d22a0f2190336159b20695cd4c7113ce32dd1e2151829ab73dfea0950912309fda1d69b59f9b6e641be295e0f1f394
d8fd5c492fccc8a69b3426b4ab0d12b035942a0dfc9ca3a2401d11d385b8a8f007ead59f3e1694e6f315576c22200f28
4cd5c4a56653424b06d609611a293322bceeaf554095f3cca784064d2e5174624dd2a741e712bf8435f0a0c44458c205
f0a4b74671b82bb9635e6e10b24863da4475cc8f5a03b84b084d8bbf0834706da6cc792f4240b267a1fc20e04f3f4633
546acb84c2681dee1c00a436b6d9cccfdbffa1cad473bc7540b0002c96977021c9e8b443db4c80abd793bb7bd0d3305d
44416d504a0a9b699832d700ad111e6478d7e798ad90f492f8d248b49cd1380d23cfce8c4692ca5f5c3e8958a9b66477
c2a7d91522b7672ec51114c161dd650d948da76149d23cccc99308d707fc1db5100c4eba404d61309b83bb3c7503742b
a94a7440bae2115fd613fe21302d369c8b24bbe9c988d4eec33d9fbdc7c9038887de34419353b1bf82c0855b6c2277d6
8a8492d6e708fc7de0fa008c102633d12552fcaae7639fa3a0de6daf2336895df8a09169ed7e67ba0af51a4119c1daaa
71d7c254c05abbdab89669a40940e34742bf31a601545cdd2010c7e55016edb49cd2d7073b67d5f9ab38d9c418e76a44
f8394a65a783ab16db093629a33bd2ac91bdfb948263c08a0e4c9a4cae7af9eff46102d7bc913b5cb84a12b623a0a09f
ed11700ead0b984e3e78193ce0190ede74759a8ba34e3cee326dab0b3482d69a9af4f2e125c2d5c85cebccceb0e79c72
862a1f4252f2a18c33614af63cd523378109df9c8408d09c7e1dcc497bf400d082d87505f29af8f49a2034d6e575af9c
0e0ae77f857693f8e11cebcfb19f40a6fd1856362b74b7821b726242542b9c1c9c0add41783d931149e85a37418dba78
73ad990a9a4c7ac61b257db791159b11375886f2655cf56469191a5f853dba4f909dd88210d27dae6335450e4886e1a9
0976178ef226e03a38f420961ab7e866c8e252e62d8d1cd7eaaa2634d2747769c7a19d0e2d6c3c119034c1092a54ae36
4d4888b31633aa9473da001976173bcb5422b5d9d1922d044d8b47512603050401000000000000000000000000000000
000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000
"""const VOL3 := """
526172211a0701004ac54b440c0105070302060101808080004fb1c48c25020b0bfa8300048df500a4830287c98a0f80
050105762e7478740a0313c60bf16a7ed758276e9e2146d09044d59e5c974bc9e14a51910cdd41b71dcbf4f11fafd98d
16fb2d0683270a40c720ef76ab99b90b355e72e2acf71ea3c079e458b57797a810a9320e5b8f495efd066558b0974909
e6a8cff09481c66b5ee1259c3d36f48eeb7ec0f22fe946721c718c317a6058d161bf759e1c1e9a5b9427d538a4a3aade
0c326b40453c6b35b52d882e9413dbc82c5dc62160fc39a3b92dce54ac533a592fd09af8aba6fc2578f5cf12048671b2
740297a0d81a2d8f87113e877a3d3645e68034c922e7f1af8d0a794f3061e8696f2def1b8d951da84ce31a294f7c79a9
d784db31486ab60718767955e357868e61a57a866f4d072a16d82b283f8370e98ca225dd77dcf92004ea79317d93484a
62722811c2d2124f214fa55393d104c65b346abd4c63ecea91ca5bab74f0eb6af2378bc9734b8ad6ad79745d233884a7
6f99443c8e46eceeb4b9bc79895e4dedea7a4a4a2f1126126cadd27c20d3f24c0fbff173f03b3e4974014f428733869b
7f1b1916980a4335126a67984987f010b31fdcfd33edf46177414ac7a83f0909729736c70710e4bd7fb79cbf1924248d
8a2111eb874fa21f667084ea442d5950d187fd9eb2f066e09224441fbc9f32719e36176cab4d67b74200ba2c55397958
d35451df7777b403b0a724d22cb9a9a5ce7a7d72484b628b53c8b1cb893c158e558776fe1fc54ecdbc7cbe36e87c047d
942702030b8500048500a4830210c43fd080000109736d616c6c2e7478740a0313bd0bf16a5abb310b74696e790a1d77
565103050400
"""funcdigit(c: int): int:
if c >= 48and c <= 57:
ret c - 48endif c >= 97and c <= 102:
ret c - 87endret -1endfuncfrom_hex(text: str): compress.ByteBuf:
mut out := compress.NewByteBuf()
bytes := text asu8*
mut high := -1repeat text.Len() in i:
v := digit(bytes[i] asint)
if v < 0:
continueendif high < 0:
high = v
else:
compress.BufPush(out, high * 16 + v)
high = -1endendret out
endfuncmain:
mut volumes := Vector {} asVector<compress.ByteBuf>
volumes.Push(from_hex(VOL1))
volumes.Push(from_hex(VOL2))
volumes.Push(from_hex(VOL3))
mut arc := archive.RarOpenVolumes(volumes, "")
mut names := archive.RarEntries(arc)
each e in names:
data := archive.RarRead(arc, e.name, 0)
println e.name, e.size, data.len, archive.RarStatusText(arc.status)
end
archive.FreeEntries(names)
archive.RarClose(arc)
mut partial := Vector {} asVector<compress.ByteBuf>
partial.Push(volumes.Get(0))
partial.Push(volumes.Get(1))
mut broken := archive.RarOpenVolumes(partial, "")
gone := archive.RarRead(broken, "v.txt", 0)
println gone.len, broken.status == archive.RAR_MULTIVOLUME
archive.RarClose(broken)
archive.WriteBinaryFileBuf("set.part01.rar", volumes.Get(0))
archive.WriteBinaryFileBuf("set.part02.rar", volumes.Get(1))
archive.WriteBinaryFileBuf("set.part03.rar", volumes.Get(2))
mut ok := falsemut found := archive.RarReadVolumes("set.part01.rar", ok)
println ok, found.Len()
archive.RarFreeVolumes(found)
end
Output
v.txt 14989 14989 ok
small.txt 5 5 ok
0 true
true 3
Read a split 7z archive
7z cuts a big archive into plain pieces named name.7z.001, name.7z.002 and so on. SevenZipReadVolumes takes the first file name and loads the rest by counting up; SevenZipOpenVolumes joins the pieces and opens them as one archive. A self-extracting 7z (an executable with the archive appended) opens with SevenZipOpen directly: the signature is searched for in the first 8 MiB.
archive-guide-sevenzip-volumes.salam
import archive
import compress
const PART1 := """
377abcaf271c000457983ded16000000000000004a00000000000000429e4011766f6c756d65206f6e650a766f6c756d6520
74776f0a0104060001091600070b
"""const PART2 := """
01000101000c1600080a014e6ded24000005011113006e006f00740065002e0074007800740000001900140a0100d9199ec4
1c70dd01150601002080a4810000
"""funcnib(c: int): int:
if c >= 48and c <= 57:
ret c - 48endif c >= 97and c <= 102:
ret c - 87endret -1endfuncunhex(s: str): compress.ByteBuf:
mut b := compress.NewByteBuf()
bp := s asu8*
mut hi := -1repeat s.Len() in i:
v := nib(bp[i] asint)
if v < 0:
continueendif hi < 0:
hi = v
else:
compress.BufPush(b, hi * 16 + v)
hi = -1endendret b
endfuncmain:
mut volumes := Vector {} asVector<compress.ByteBuf>
volumes.Push(unhex(PART1))
volumes.Push(unhex(PART2))
mut arc := archive.SevenZipOpenVolumes(volumes, "")
names := archive.SevenZipEntries(arc)
println"entries:", names.Len(), arc.status
data := archive.SevenZipRead(arc, "note.txt", 0)
print compress.BufToStrUnsafe(data)
archive.SevenZipClose(arc)
archive.SevenZipFreeVolumes(volumes)
end
Output
entries: 1 0
volume one
volume two
One door for every format
Open reads the first bytes, finds the format and returns an Archive; List, Read, Extract and ExtractAll then work the same for every format and report one set of ARCHIVE_ codes. OpenFile, ListFile and ExtractFile do the same from a path. Gzip, bzip2 and xz files are unpacked on opening, and a tar inside is recognized, so a .tar.gz, .tar.bz2 and .tar.xz open like any other archive. The per-format functions below keep working unchanged.
Format
Open, list, read, extract
Make
zip
yes
Create (.zip)
tar, tar.gz, tar.bz2, tar.zst
yes
Create (.tar, .tar.gz, .tgz, .tar.bz2, .tar.zst)
tar.xz
yes
no encoder
gzip, bzip2, xz, zstd (one file)
yes, as an archive of one entry
Create for .gz, .bz2 and .zst with exactly one file
RAR
yes, with a password
Create (.rar, stored)
7z
yes, with a password
Create (.7z, stored)
ExtractAll is the safe way to unpack data you do not trust. Before it writes anything it checks every entry against Limits (entry count, size of one entry, size of all, and the ratio of unpacked size to archive size, which catches compression bombs) and refuses names that are absolute or climb out with .., links that leave the folder, files written through a link, and device entries. If a check fails nothing is written and status says why. The limits use the sizes the archive declares; each format still verifies the real sizes while unpacking. Multi-volume RAR and 7z sets, and large-file streaming, stay with the per-format functions.
The same calls work from a path. ListFile and ExtractFile open the file, do the job and close it; OpenFile gives you an Archive to reuse, and SetLimits tightens the Limits of one archive. Every call reports a status code, and ArchiveStatusText turns it into words. The example makes a .tar.gz, lists and extracts it, hits a limit and a missing file on purpose, and prints all fifteen codes.
create: ok
list: ok
notes/ 0
notes/a.txt 17
b.txt 5
extract: 2 ok
tar.gz 0
tight: 0 too many entries
missing: 0 file could not be read or written
0 ok
1 entry not found
2 unsupported format, method or feature
3 encrypted data and no password
4 corrupt data
5 checksum mismatch
6 entry too large
7 entry path or link leaves the target folder
8 wrong password
9 a volume of the multi-volume archive is missing
10 file could not be read or written
11 unknown archive format
12 too many entries
13 compression ratio too high
14 dictionary or window larger than the memory limit
Code
Constant
Meaning
0
ARCHIVE_OK
Done.
1
ARCHIVE_NOT_FOUND
No entry with that name.
2
ARCHIVE_UNSUPPORTED
A format, method or feature the package does not read.
3
ARCHIVE_ENCRYPTED
Encrypted and no password was given.
4
ARCHIVE_CORRUPT
The structure is damaged or cut short.
5
ARCHIVE_BAD_CRC
The checksum of the data does not match.
6
ARCHIVE_TOO_BIG
An entry is over max_entry, or over what fits in memory.
7
ARCHIVE_UNSAFE_PATH
A name or link would leave the target folder.
8
ARCHIVE_BAD_PASSWORD
The password is wrong.
9
ARCHIVE_MULTIVOLUME
A volume of the set is missing.
10
ARCHIVE_IO_ERROR
A file could not be read or written.
11
ARCHIVE_UNKNOWN_FORMAT
The first bytes match no format.
12
ARCHIVE_TOO_MANY_ENTRIES
More entries than max_entries.
13
ARCHIVE_BOMB
Unpacked size over max_ratio times the archive size.
14
ARCHIVE_WINDOW_TOO_BIG
The dictionary of the compressed data is over the memory limit.
Make a 7z archive
Build SevenZipFileInput values and call SevenZipCreate or SevenZipCreateFile. Files, folders, empty files and symbolic links (set link) are written, with modes and times when you give them. SevenZipCreate stores the data; SevenZipCreateLevel and SevenZipCreateFileLevel compress each file with LZMA at levels 1 to 9. The result opens in 7-Zip and in SevenZipExtractAll.
archive-guide-sevenzip-create.salam
import archive
import compress
funcmain:
a := compress.BufFromStr("first file\n")
b := compress.BufFromStr("second file, a little longer\n")
mut files := Vector {} asVector<archive.SevenZipFileInput>
files.Push(
archive.MakeSevenZipFileInput("docs/", null, 0, true)
)
files.Push(archive.MakeSevenZipFileInput("docs/a.txt", a.data, a.len, false))
mut second := archive.MakeSevenZipFileInput("docs/b.txt", b.data, b.len, false)
second.mtime = 1700000000asi64
second.mode = 420
files.Push(second)
mut link := archive.MakeSevenZipFileInput("docs/latest", null, 0, false)
link.link = "b.txt"
files.Push(link)
packed := archive.SevenZipCreate(files)
println archive.IsSevenZip(packed.data, packed.len),
archive.SevenZipCreateFile(files, "made.7z")
mut entries := archive.SevenZipList(packed.data, packed.len)
each e in entries:
println e.name, e.size, e.is_dir, e.kind, e.mtime
end
archive.FreeEntries(entries)
mut ok := false
got := archive.SevenZipExtractByName(packed.data, packed.len, "docs/b.txt", ok)
println ok, compress.BufToStrUnsafe(got) == "second file, a little longer\n"end
The functions that take a pointer and a length hold the whole archive in memory, which stops at 2 GiB. For a zip of any size use ZipOpenFile: it reads only the end of the file and the list of entries, and ZipFileExtractTo and ZipFileExtractAll unpack straight to disk in 64 KiB pieces, checking the CRC-32 and the size as they go. Sizes and offsets are 64 bit, zip64 included, so an entry of several gigabytes works and memory use stays small. The only limit is the one you pass: cap is a number of bytes (0 for none), and an entry over it returns ZIP_TOO_BIG before anything is written.
archive-guide-zipfile.salam
import archive
import compress
import os
funcmain:
body := "a large file would be unpacked straight to disk\n"mut files := Vector {} asVector<archive.ZipFileInput>
files.Push(
archive.MakeZipFileInput("data/log.txt", body asvoid*, body.Len(), 8)
)
mut made := archive.ZipCreate(files)
files.Free()
archive.WriteBinaryFileBuf("made.zip", made)
compress.BufFree(made)
mut zf := archive.ZipOpenFile("made.zip")
mut entries := archive.ZipFileEntries(zf)
each e in entries:
println e.name, e.sizeend
archive.FreeEntries(entries)
println archive.ZipFileExtractTo(zf, "data/log.txt", "log.out", 0asi64) == archive.ZIP_OK
println archive.ZipFileExtractTo(
zf, "data/log.txt", "log.out", 10asi64
) == archive.ZIP_TOO_BIG
archive.ZipFileClose(zf)
os.Remove("made.zip")
os.Remove("log.out")
end
Output
data/log.txt 48
true
true
Big 7z files on disk
SevenZipOpenFile opens a 7z on disk and keeps only its header in memory. SevenZipFileExtractTo and SevenZipFileExtractAll unpack straight to disk: LZMA and LZMA2 data goes through a window as big as the dictionary and out in 64 KiB pieces, so an entry of several gigabytes needs only a few megabytes of memory. Every method streams the same way: PPMd needs only the model memory its properties ask for, bzip2 holds one block, the branch filters (BCJ, ARM, ARM64, RISC-V and the rest), delta, byte swap and BCJ2 are stages that pass each piece on, and AES-256 decrypts in pieces in front of the next coder, so AES then LZMA2 then BCJ reads straight from the file with no temporary file. Encrypted headers go through the same path. BCJ2 reads its range coder stream from the file; its call and jump streams are read from the file when stored and decoded into small buffers (at most memcap) when compressed. A solid block is read once for all of its files. cap limits the bytes of one entry and memcap the memory the decoder may use; both take 0 for no limit.
archive-guide-sevenzipfile.salam
import archive
import os
funcmain:
body := "a large file would be unpacked straight to disk\n"mut files := Vector {} asVector<archive.SevenZipFileInput>
files.Push(
archive.MakeSevenZipFileInput("data/log.txt", body asvoid*, body.Len(), false)
)
archive.SevenZipCreateFile(files, "made.7z")
files.Free()
mut sf := archive.SevenZipOpenFile("made.7z", "")
mut entries := archive.SevenZipFileEntries(sf)
each e in entries:
println e.name, e.sizeend
archive.FreeEntries(entries)
println archive.SevenZipFileExtractTo(
sf, "data/log.txt", "log.out", 0asi64, 0asi64
) == archive.SEVENZIP_OK
println archive.SevenZipFileExtractTo(
sf, "data/log.txt", "log.out", 10asi64, 0asi64
) == archive.SEVENZIP_TOO_BIG
archive.SevenZipFileClose(sf)
os.Remove("made.7z")
os.Remove("log.out")
end
Output
data/log.txt 48
true
true
Big tar files on disk
TarFileOpen reads a tar from disk one entry at a time: TarFileNext fills an Entry (sizes and offsets are 64 bit, so a 5 GB entry is just an entry), TarFileExtractTo copies its data to a file in 64 KiB pieces, and TarFileExtractAll unpacks the whole archive with the same safety rules as TarExtractAll. Nothing but one entry's header is ever in memory. cap is the only size limit and it is yours (0 for none): TAR_TOO_BIG means an entry is over it. TarGzFileExtractAll and GzipDecompressFileCap do the same for gzip, which is read as a stream. A compressed tar needs no temporary file: TarGzFileExtractAll, TarBz2FileExtractAll, TarXzFileExtractAll and TarZstdFileExtractAll (or TarCompressedFileExtractAll) feed the decoder's output into the tar reader piece by piece, and TarCompressedFileEntries lists one the same way.
archive-guide-tarfile.salam
import archive
import compress
import os
funcmain:
body := "streamed straight from the archive\n"mut files := Vector {} asVector<archive.TarFileInput>
files.Push(
archive.MakeTarFileInput("logs/a.txt", body asvoid*, body.Len(), false)
)
files.Push(archive.MakeTarFileInput("logs/b.txt", body asvoid*, 10, false))
mut made := archive.TarCreate(files)
files.Free()
archive.WriteBinaryFileBuf("made.tar", made)
compress.BufFree(made)
mut tf := archive.TarFileOpen("made.tar")
mut e := archive.Entry {}
until archive.TarFileNext(tf, e):
println e.name, e.sizeif e.name == "logs/a.txt":
println archive.TarFileExtractTo(tf, e, "a.out", 0asi64) == archive.TAR_OK
println archive.TarFileExtractTo(tf, e, "a.out", 5asi64) == archive.TAR_TOO_BIG
end
archive.FreeEntry(e)
end
archive.TarFileClose(tf)
println archive.TarFileExtractAll("made.tar", "out", 0asi64) == archive.TAR_OK
os.RemoveAll("out")
os.Remove("made.tar")
os.Remove("a.out")
end
Output
logs/a.txt 35
true
true
logs/b.txt 10
true
Big RAR files on disk
RarFileOpen reads a RAR 5 file's headers only (offsets and sizes are 64 bit); RarFileExtractTo and RarFileExtractAll then unpack to disk, writing the result in pieces and checking the CRC-32 as they go. Compressed entries, solid archives and the built-in filters work. What is held in memory is the packed data of one entry and the decoder window, so the unpacked size can be anything: an entry of several gigabytes is unpacked from a file of a few megabytes in constant memory. The size limits are yours: cap for the unpacked bytes and memcap for that memory, and RAR_TOO_BIG means one of them was passed, and RAR_WINDOW_TOO_BIG that the data needs a window bigger than memcap or 1 GiB. Passwords and volumes work from disk too: RarFileOpenWith opens archives with encrypted files or file names, and RarFileOpenVolumes a set of volumes, decrypting and joining pieces as it unpacks. RarFileExtractAll restores permissions and times and creates safe symbolic links. Older RAR formats (1.5 to 4.x) work the same way, passwords, encrypted file names and volumes included: the packed data is read from the file in pieces and the unpacked data goes to disk in pieces (the VM filters are applied on the way), so an entry or a volume over 4 GiB works with a few megabytes of memory (sizes and offsets are 64 bit, including the high size fields of the headers). Only entries encrypted with the very old RAR 1.5 or 2.0 ciphers still need their packed data in memory, up to 256 MiB.
archive-guide-rarfile.salam
import archive
import os
funcmain:
body := "read from a RAR on disk, entry by entry\n"mut files := Vector {} asVector<archive.RarFileInput>
files.Push(
archive.MakeRarFileInput("notes/a.txt", body asvoid*, body.Len(), false)
)
archive.RarCreateFile(files, "made.rar")
files.Free()
mut rf := archive.RarFileOpen("made.rar")
println rf.status == archive.RAR_OK, rf.streaming
mut entries := archive.RarFileEntries(rf)
each e in entries:
println e.name, e.sizeend
archive.FreeEntries(entries)
println archive.RarFileExtractTo(
rf, "notes/a.txt", "a.out", 0asi64, 0asi64
) == archive.RAR_OK
println archive.RarFileExtractTo(
rf, "notes/a.txt", "a.out", 5asi64, 0asi64
) == archive.RAR_TOO_BIG
archive.RarFileClose(rf)
os.Remove("made.rar")
os.Remove("a.out")
end
Output
true true
notes/a.txt 40
true
true
Which function for which jobJobUseFiles for Windows users or a browser downloadZipCreate, ZipCreateFileA backup or a release bundle on UnixTarGzCreate or TarGzCreateFileSee what is insideZipList, TarList, RarList, then FreeEntriesOne file out of an archiveZipExtractByName, TarExtractByNameEverything into a folderZipExtractAll, TarExtractAllA RAR fileRarList, RarExtract (or RarExtractByName), RarExtractAll; RarCreate to make oneA 7z archive, with or without a password; SevenZipCreate makes one (stored)SevenZipList, SevenZipExtractByName, SevenZipExtractAll; SevenZipOpen, SevenZipEntries, SevenZipRead, SevenZipReadAll when it has a passwordA single compressed file (.gz)GzipCompressFile, GzipDecompressFileA single .xz or .lzma fileXzCompressFile, XzDecompressFile, LzmaCompressFile, LzmaDecompressFileA single file, smaller than gzip (.bz2)Bzip2CompressFile, Bzip2DecompressFileA single file in Zstandard (.zst)ZstdCompressFile, ZstdDecompressFileCheck that an archive is intactzip: ZipVerify, ZipExtractVerified; gzip and RAR check themselves; see "Check an archive"A password-protected or multi-volume RARRarOpen, RarOpenVolumes, then RarEntries, RarRead, RarReadAllBytes from and to diskReadBinaryFile, WriteBinaryFileBuf
A 7z file on disk opened with SevenZipOpenFile. Only the list of entries (the header) is kept in memory; the data stays in the file.
Name
Type
Description
path
str = ""
The path the archive was opened from.
size
i64 = 0
The size of the file in bytes.
start
i64 = 0
Where the archive begins in the file; not zero for a self-extracting program.
password
str = ""
The password, or an empty string.
status
int = 0
SEVENZIP_OK when the archive opened, otherwise the reason.
hdr
compress.ByteBuf
The decoded header. Treat it as private; it is only public so the package can fill it.
volumes
Vector<str>
The paths of the pieces when the archive was opened from name.7z.001; one entry otherwise. The pieces are read in place and never joined in memory.
memp
void*
The archive bytes when the package reads an in-memory archive from SevenZipOpen through this type; null for an archive opened from a file. Treat it as private; it is only public so the package can fill it.
memn
i64 = 0
The length of those bytes, 0 for a file. Private like memp.
archive-SevenZipFile.salam
import archive
import os
funcmain:
body := "a large file would be unpacked straight to disk\n"mut files := Vector {} asVector<archive.SevenZipFileInput>
files.Push(
archive.MakeSevenZipFileInput("data/log.txt", body asvoid*, body.Len(), false)
)
archive.SevenZipCreateFile(files, "made.7z")
files.Free()
mut sf := archive.SevenZipOpenFile("made.7z", "")
mut entries := archive.SevenZipFileEntries(sf)
each e in entries:
println e.name, e.sizeend
archive.FreeEntries(entries)
println archive.SevenZipFileExtractTo(
sf, "data/log.txt", "log.out", 0asi64, 0asi64
) == archive.SEVENZIP_OK
println archive.SevenZipFileExtractTo(
sf, "data/log.txt", "log.out", 10asi64, 0asi64
) == archive.SEVENZIP_TOO_BIG
archive.SevenZipFileClose(sf)
os.Remove("made.7z")
os.Remove("log.out")
end
An opened archive of any format, made by Open, OpenWith or OpenFile. Free it with Close.
Name
Type
Description
data
void*
The address of the archive bytes (not owned, unless read whole by OpenFile). Null for an archive that OpenFile keeps on disk.
len
int = 0
The length of the archive bytes.
password
str = ""
The password given to Open.
format
int = 0
The detected format, one of the Format codes.
status
int = 0
The ARCHIVE_ code of the last call.
limits
Limits
The caps in force.
name
str = "data"
The name of the one entry in a single-stream file (.gz, .bz2, .xz), taken from the file name by OpenFile; otherwise data.
fail
int = 0
Why the archive could not be opened, or 0.
inner
compress.ByteBuf
The unpacked bytes of a gzip, bzip2 or xz file, owned by the archive.
file
compress.ByteBuf
The file contents, when a file was read whole by OpenFile.
disk
int = 0
Which on-disk reader an archive opened by OpenFile uses: 1 zip, 2 7z, 3 tar, 4 RAR, 5 tar.gz (decoded as a stream), and 0 when the archive is in memory.
tmp
str = ""
Always empty now; a tar.gz opened by OpenFile is read as a stream and no temporary file is made.
gzpath
str = ""
The path of a tar.gz that OpenFile reads as a stream (disk 5); empty otherwise.
zf
ZipFile
The ZipFile behind a zip opened by OpenFile.
sz
SevenZipFile
The SevenZipFile behind a 7z opened by OpenFile.
tf
TarFile
The TarFile behind a tar or tar.gz opened by OpenFile.
rf
RarFile
The RarFile behind a RAR opened by OpenFile.
The example for this item is shown with the ones above: archive-Open.salam
A RAR file on disk opened with RarFileOpen. Only the headers are read; the data stays in the file and is unpacked straight to disk, for RAR 5 and for the older formats (1.5 to 4.x) alike. The older formats are read in pieces too; only entries encrypted with the RAR 1.5 or 2.0 ciphers hold their packed data in memory (up to 256 MiB).
Name
Type
Description
path
str = ""
The path the archive was opened from.
size
i64 = 0
The size of the file in bytes.
status
int = 0
RAR_OK when the archive opened, otherwise the reason.
streaming
bool = false
True when the archive is read from disk without loading it (every RAR format).
arc
_RarArc
The parsed entry table. Treat it as private.
offs
Vector<i64>
The 64 bit offset of each entry's data in the file. Treat it as private.
whole
compress.ByteBuf
The file contents of an archive that had to be loaded whole (a self-extracting file with a stub in front, under 2 GB). Empty otherwise.
password
str = ""
The password given to RarFileOpenWith or RarFileOpenVolumes. Treat it as private.
vols
_RarVols
The list of volume paths and the pieces of each entry. Treat it as private.
archive-RarFile.salam
import archive
import os
funcmain:
body := "read from a RAR on disk, entry by entry\n"mut files := Vector {} asVector<archive.RarFileInput>
files.Push(
archive.MakeRarFileInput("notes/a.txt", body asvoid*, body.Len(), false)
)
archive.RarCreateFile(files, "made.rar")
files.Free()
mut rf := archive.RarFileOpen("made.rar")
println rf.status == archive.RAR_OK, rf.streaming
mut entries := archive.RarFileEntries(rf)
each e in entries:
println e.name, e.sizeend
archive.FreeEntries(entries)
println archive.RarFileExtractTo(
rf, "notes/a.txt", "a.out", 0asi64, 0asi64
) == archive.RAR_OK
println archive.RarFileExtractTo(
rf, "notes/a.txt", "a.out", 5asi64, 0asi64
) == archive.RAR_TOO_BIG
archive.RarFileClose(rf)
os.Remove("made.rar")
os.Remove("a.out")
end
The name inside the archive; a directory name ends with /. Use / between folders, never \.
data_ptr
void*
The address of the contents.
data_len
int = 0
The length of the contents.
method
int = 8
Zero for plain storage, 8 for DEFLATE, 12 for bzip2, 14 for LZMA or 93 for zstd. Any other number stores the data, and so does an empty entry.
mtime
i64 = 0
The modification time in seconds since the Unix epoch. Zero, or a time before 1980, writes 1 January 1980; the DOS format cannot go past 2107 and keeps two second steps.
level
int = 0
The compression level for bzip2 (1 to 9, default 9), LZMA (1 to 9, default 6) or zstd (1 to 22, default 3). Zero picks the default; DEFLATE and plain storage ignore it.
path
str = ""
A file to read the contents from when the archive is written; used instead of data_ptr. With ZipCreateFile the entry is read and compressed in pieces, so it is never held whole in memory: DEFLATE uses a data descriptor, and bzip2, LZMA and zstd entries are written with a placeholder size that is patched in the local header once the entry is done.
password
str = ""
The password for this entry; empty means no encryption.
encryption
int = 0
ZipEncAuto (0, AES-256 when there is a password), ZipEncZipCrypto (1, the old weak zip encryption), ZipEncAes128, ZipEncAes192 or ZipEncAes256 (WinZip AES, version AE-2). unzip reads only ZipCrypto; 7-Zip and WinZip read both.
archive-Fields.salam
import archive
import compress
funcmain:
text := "fields of an entry, fields of an entry"
b := compress.BufFromStr(text)
mut zin := Vector {} asVector<archive.ZipFileInput>
zin.Push(archive.ZipFileInput {
name = "docs/",
data_ptr = null,
data_len = 0,
method = 0
})
zin.Push(archive.ZipFileInput {
name = "docs/a.txt",
data_ptr = b.data,
data_len = b.len,
method = 8
})
z := archive.ZipCreate(zin)
mut zentries := archive.ZipList(z.data, z.len)
each e in zentries:
println e.name,
e.size,
e.compressed_size <= e.size,
e.is_dir,
e.method,
e.crc32 == compress.CRC32Buf(b) or e.is_dir,
e.mtime >= 0end
archive.FreeEntries(zentries)
mut tin := Vector {} asVector<archive.TarFileInput>
tin.Push(archive.TarFileInput {
name = "notes/",
data_ptr = null,
data_len = 0,
is_dir = true
})
tin.Push(archive.TarFileInput {
name = "notes/a.txt",
data_ptr = b.data,
data_len = b.len,
is_dir = false
})
t := archive.TarCreate(tin)
mut tentries := archive.TarList(t.data, t.len)
each e in tentries:
println e.name, e.size, e.is_dir
end
archive.FreeEntries(tentries)
end
The compression method: zero for plain storage, 8 for DEFLATE, 12 for bzip2, 14 for LZMA, 93 for zstd.
crc32
u32 = 0
The checksum of the contents.
mtime
i64 = 0
The modification time in seconds since the Unix epoch: from a tar header, a RAR header, or for zip the DOS date and time read as UTC (0 when the entry has none).
link
str = ""
The target of a tar symbolic or hard link, otherwise empty.
kind
int = 0
What a tar entry is: 0 file, 1 folder, 2 symbolic link, 3 hard link, 4 other (device, pipe). Zip and RAR entries are 0 or 1; 7z entries are 0, 1 or 2 (a symbolic link, with its target in link).
mode
int = 0
The permission bits of a tar entry, such as 0o644; 0 for zip and RAR entries.
The entry is bigger than the max given, or, in the in-memory calls, an entry over the cap given or over what one buffer holds (about 2 GiB); in the SevenZipFile calls, an entry over cap or a decoder that would need more than memcap.
Status: the data points back farther than the window the decoder can hold (the smaller of memcap and 1 GiB), or memcap is under 128 KiB. Raise memcap up to 1 GiB, or unpack with another tool.
archive-RarFileSet.salam
import archive
import compress
import filepath
import mem
import os
const PART1 := """
526172211a0701006de128270b01050701060101808080000cf698c92602130b950804b60ea48302e3916346800001086e6f
74652e7478740a03132f8df16a8b8429036469736b2073747265616d20766f6c756d6520766f6c756d652072617220726172
2062797465207061636b2073616c616d20766f6c756d652073747265616d206469736b2073747265616d2073616c616d2076
6f6c756d652073747265616d2073747265616d207061636b2073616c616d207061636b206469736b20726172207061636b20
6469736b207061636b206469736b2072617220726172207261722073616c616d2073616c616d207261722073747265616d20
627974652073747265616d2073616c616d2062797465207061636b206469736b2062797465207061636b2073616c616d2076
6f6c756d6520726172207061636b20726172207061636b2062797465206469736b2073616c616d206279746520766f6c756d
6520726172206469736b20766f6c756d6520766f6c756d6520627974652073616c616d20726172207061636b2073616c616d
206469736b2073616c616d206469736b2062797465206469736b207261722073616c616d20726172206469736b207061636b
20627974652073616c616d2073747265616d2073616c616d2073616c616d20627974652073747265616d2072617220627974
652072617220766f6c756d652073616c616d2073616c616d207261722073747265616d2073616c616d2073747265616d2073
616c616d2073747265616d206469736b2073747265616d20726172206469736b2073616c616d206469736b20726172207061
636b20627974652073616c616d207061636b2073747265616d20726172207261722073747265616d207061636b2073616c61
6d2073616c616d20766f6c756d652073747265616d207061636b2073747265616d206279746520726172207061636b20766f
6c756d65207061636b2073747265616d2073616c616d2073616c616d2062797465207061636b206469736b20726172207261
722073747265616d2073747265616d2073616c616d207061636b20766f6c756d652062797465207061636b2073747265616d
2073747265616d2073747265616d2062797465207061636b2073616c616d206279746520766f6c756d652073616c616d2062
7974652073747265616d20766f6c756d65206469736b20726172206469736b207261722073747265616d207061636b207361
6c616d206469736b20627974652073747265616d20766f6c756d652073616c616d206469736b2073616c616d207261722072
6172206469736b20766f6c756d6520766f6c756d6520627974652073616c616d20766f6c756d65207061636b20766f6c756d
652073616c616d2073747265616d20627974652073616c616d2073616c616d206469736b20627974652073747265616d2062
797465206469736b206469736b20766f6c756d65207061636b2062797465207261722073747265616d207061636b2073616c
616d206469736b207061636b8b47512603050401000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
"""const PART2 := """
526172211a070100a9c2c4ca0c0105070301060101808080004b1a525026020b0ba10604b60ea483023a674699800001086e
6f74652e7478740a03132f8df16a8b842903207261722073616c616d2062797465206469736b2073747265616d20766f6c75
6d6520627974652073747265616d20766f6c756d6520726172207061636b20766f6c756d6520726172207061636b20627974
6520766f6c756d652073616c616d2073616c616d20627974652072617220766f6c756d65207261722073616c616d20737472
65616d207261722073616c616d2073616c616d2073616c616d206469736b207261722062797465206469736b20766f6c756d
652073747265616d2073616c616d206279746520766f6c756d6520766f6c756d6520726172206469736b2073747265616d20
766f6c756d65207261722073616c616d207061636b2073747265616d20766f6c756d65207061636b2073616c616d2073616c
616d206469736b206469736b20726172207061636b2073747265616d20627974652062797465206469736b2073747265616d
206279746520726172207061636b207061636b20766f6c756d6520627974652073747265616d2073747265616d206469736b
20766f6c756d652073616c616d2073616c616d20766f6c756d652073747265616d2072617220726172206279746520706163
6b20627974652073616c616d20726172206279746520627974652072617220627974652073616c616d207261722072617220
6469736b206469736b2073747265616d2073747265616d2073747265616d206469736b2073616c616d207261722073747265
616d206469736b2073747265616d20766f6c756d652073747265616d2073616c616d207061636b2073616c616d2073747265
616d2073747265616d206469736b2073747265616d20766f6c756d652073747265616d2073747265616d2073747265616d20
7061636b20627974652073616c616d207061636b2073747265616d2073747265616d20766f6c756d652073616c616d207061
636b20726172207061636b207261722073616c616d2073616c616d207061636b206469736b206469736b2073616c616d2064
69736b207061636b207061636b2073616c616d207061636b2073747265616d2073616c616d20766f6c756d65207261722064
69736b2072617220766f6c756d6520627974651d77565103050400
"""const LOCKED := """
526172211a0701004f8ffa8e21040000010f34559f3b7ae69dc2dd40a39e1c528aafc2c96a316ce4aff36be1173ec9d29470
b91550fbe3c4c45f4dd2fbfe14bccb7ab7ba028efddec80ddb621bbad04a170343323b71489ad58a5039dca2da3bdf435da5
f52ff84b14deed319e54d2a76603c336bfcac6faecf773864d5f7fcaa7c05eb4570fd2f2e39533c099d50b6d39ce1f5bb90f
3c010d404ad0ec9b31003f9c4892bd41af252ddb3c27a2dbb488f20e33f188f4d2e89a9ae2e4afe566fdbb028936a17201d0
2ec33fba353ca4cb04f7a17ee62a89f3d2b3f20824fe874a07a6def01b09b32f4ce51efc7987f310d37ab074af4521d30448
555e46f5958a3c180c407decc1356d2300731e1e29668ce289433a4dc3700e2f1d9160a523a3f00e3aa545c6ff6f2f1f7e41
c4da6ce567f92813032b06e580080be55bd7002ce6ebe526789958b3173e5cf40ff4e383659b3c9af42ed3da8dfc7ecb32ef
cd6e0a09077414475679bd1465caa15710d438cd48bca4d174b4f58c7ee4d54019ece9757f310d13bed92d2301a1cbf4bcb0
3832dbad74df26f38fabb873e2c1d2a4b05d69fe74e31f6d1b43a1674ef75186a699bb8cfe47b06dd238ef625185e182f8d2
14679ae6739f403a6feae65452d8d3f29a2624ba021af6827e1c07a25af6b70bf82085777474c4ad54bf2d4591b8ecdcabc2
651228c42c7a6b39c36052531820b61b5c12d22b26c884611ecdfb1edc9f4a5b4e0ed808746e0b37817ed8fff2c61b1a1399
99190d89e32a8838
"""funcnib(c: int): int:
if c >= 48and c <= 57:
ret c - 48endif c >= 97and c <= 102:
ret c - 87endret -1endfuncsave(dir: str, name: str, hex: str): str:
mut b := compress.NewByteBuf()
bp := hex asu8*
mut hi := -1repeat hex.Len() in i:
v := nib(bp[i] asint)
if v < 0:
continueendif hi < 0:
hi = v
else:
compress.BufPush(b, hi * 16 + v)
hi = -1endend
path := filepath.Join(dir, name)
_ := archive.WriteBinaryFileBuf(path, b)
compress.BufFree(b)
ret path
endfuncmain:
dir := filepath.Join(os.TempDir(), "salam_rarfile_set")
os.MkdirAll(dir)
first := save(dir, "set.part1.rar", PART1)
second := save(dir, "set.part2.rar", PART2)
locked := save(dir, "locked.rar", LOCKED)
mut rf := archive.RarFileOpenVolumes(first, "")
println"volumes", rf.vols.paths.Len(), archive.RarStatusText(rf.status)
out := filepath.Join(dir, "out")
println archive.RarStatusText(archive.RarFileExtractAll(rf, out, 0asi64, 0asi64))
archive.RarFileClose(rf)
mut bad := archive.RarFileOpenWith(locked, "wrong")
println"wrong password", archive.RarStatusText(bad.status)
archive.RarFileClose(bad)
mut good := archive.RarFileOpenWith(locked, "secret")
mut es := archive.RarFileEntries(good)
println"right password", es.Len(), es.Get(0).name, es.Get(0).size
archive.FreeEntries(es)
archive.RarFileClose(good)
mem.Free(out asvoid*)
mem.Free(first asvoid*)
mem.Free(second asvoid*)
mem.Free(locked asvoid*)
os.RemoveAll(dir)
mem.Free(dir asvoid*)
end
Output
volumes 2 ok
ok
wrong password wrong password
right password 1 note.txt 1846
import archive
import compress
importstrfuncmain:
mut text := compress.NewByteBuf()
repeat80in i:
line := "line " + str.FromInt(i * 7 % 23) + ": zip entries can use several methods\n"
compress.BufPushBytes(text, line asvoid*, line.Len())
endprintln"text:", text.len, "bytes"
names := ["store", "deflate", "bzip2", "lzma", "zstd"]
methods := [
archive.ZipMethodStore, archive.ZipMethodDeflate, archive.ZipMethodBzip2, archive.ZipMethodLzma,
archive.ZipMethodZstd
]
repeat5in i:
mut files := Vector {} asVector<archive.ZipFileInput>
files.Push(
archive.MakeZipFileInput("notes.txt", text.data, text.len, methods[i])
)
z := archive.ZipCreate(files)
mut checked := 0mut status := 0
back := archive.ZipExtractVerified(z.data, z.len, "notes.txt", 0, status)
println names[i],
"method",
methods[i],
"zip",
z.len,
"bytes, verify",
archive.ZipVerify(z.data, z.len, checked),
"same text",
status == 0and compress.BufEquals(back, text)
endmut files := Vector {} asVector<archive.ZipFileInput>
files.Push(
archive.MakeZipFileInputLevel("fast.txt", text.data, text.len, archive.ZipMethodZstd, 1)
)
mut best := archive.MakeZipFileInput("best.txt", text.data, text.len, archive.ZipMethodZstd)
best.level = 19
files.Push(best)
files.Push(archive.MakeZipFileInput("empty.txt", null, 0, archive.ZipMethodBzip2))
z := archive.ZipCreate(files)
mut entries := archive.ZipList(z.data, z.len)
each e in entries:
println e.name, "method", e.method, e.size, "->", e.compressed_size
end
archive.FreeEntries(entries)
end
Output
text: 3565 bytes
store method 0 zip 3681 bytes, verify 0 same text true
deflate method 8 zip 251 bytes, verify 0 same text true
bzip2 method 12 zip 297 bytes, verify 0 same text true
lzma method 14 zip 252 bytes, verify 0 same text true
zstd method 93 zip 270 bytes, verify 0 same text true
fast.txt method 93 3565 -> 154
best.txt method 93 3565 -> 142
empty.txt method 0 0 -> 0
Zip method 9: Deflate64, the DEFLATE variant of 7-Zip and Windows with a 64 KiB window and longer matches. Zip files with it are read, but not written: asking for method 9 in ZipCreate stores the file.
archive-ZipDeflate64.salam
import archive
import compress
funchex_val(c: int): int:
if c >= 97: ret c - 87endret c - 48endfuncunhex(h: str): compress.ByteBuf:
mut b := compress.NewByteBuf()
bp := h asu8*
mut i := 0until i + 1 < h.Len():
compress.BufPush(b, hex_val(bp[i] asint) * 16 + hex_val(bp[i + 1] asint))
i += 2endret b
endfuncmain:
z := unhex(
"504b03041500000009008044695dba1f6638250000003200000005000000742e74787485c8b10d00400405d0fea630c095621f89af1285686c6f04af7c060f6d0813b26b3e5df116504b01023f031500000009008044695dba1f66382500000032000000050024000000000000002080a48100000000742e7478740a00200000000000010018007dda1d582570dd0100000000000000000000000000000000504b0506000000000100010057000000480000000000"
)
mut entries := archive.ZipList(z.data, z.len)
each e in entries:
println e.name, e.size, e.compressed_size, e.method == archive.ZipMethodDeflate64
end
archive.FreeEntries(entries)
mut status := 0
text := archive.ZipExtractVerified(z.data, z.len, "t.txt", 0, status)
println status == archive.ZIP_OK, compress.BufToStrUnsafe(text)
mut files := Vector {} asVector<archive.ZipFileInput>
files.Push(
archive.MakeZipFileInput("t.txt", text.data, text.len, archive.ZipMethodDeflate64)
)
written := archive.ZipCreate(files)
mut back := archive.ZipList(written.data, written.len)
println"written with method", back.Get(0).method
archive.FreeEntries(back)
end
Output
t.txt 50 37 true
true deflate64 entry, deflate64 entry, deflate64 entry
written with method 0
funcSevenZipReadVolumes(first: str, ok &: bool): Vector<compress.ByteBuf>
Reads the first volume (name.7z.001) and every following one by counting up the number at the end of the name. ok is false when the first file cannot be read.
archive-SevenZipVolumes.salam
import archive
import compress
const PART1 := """
377abcaf271c000457983ded16000000000000004a00000000000000429e4011766f6c756d65206f6e650a766f6c756d6520
74776f0a0104060001091600070b
"""const PART2 := """
01000101000c1600080a014e6ded24000005011113006e006f00740065002e0074007800740000001900140a0100d9199ec4
1c70dd01150601002080a4810000
"""funcnib(c: int): int:
if c >= 48and c <= 57:
ret c - 48endif c >= 97and c <= 102:
ret c - 87endret -1endfuncunhex(s: str): compress.ByteBuf:
mut b := compress.NewByteBuf()
bp := s asu8*
mut hi := -1repeat s.Len() in i:
v := nib(bp[i] asint)
if v < 0:
continueendif hi < 0:
hi = v
else:
compress.BufPush(b, hi * 16 + v)
hi = -1endendret b
endfuncmain:
mut volumes := Vector {} asVector<compress.ByteBuf>
volumes.Push(unhex(PART1))
volumes.Push(unhex(PART2))
mut arc := archive.SevenZipOpenVolumes(volumes, "")
names := archive.SevenZipEntries(arc)
println"entries:", names.Len(), arc.status
data := archive.SevenZipRead(arc, "note.txt", 0)
print compress.BufToStrUnsafe(data)
archive.SevenZipClose(arc)
archive.SevenZipFreeVolumes(volumes)
end
Like SevenZipOpen for the volumes of a split archive, in order. The pieces are joined into one buffer that the archive owns; release it with SevenZipClose.
Lists the entries (files and folders; anti items are left out) and sets status. compressed_size is the packed size of the solid block, given on its first file; method is 0 copy, 1 LZMA, 2 LZMA2, 3 PPMd, 4 BZip2, 5 Deflate, 9 other; mode holds the Unix permission bits when the archive has them. Free the list with FreeEntries.
Unpacks one entry by name, checks its CRC-32 and sets status. max refuses entries bigger than that many bytes (0 means no limit, which is the 2 GiB a buffer holds). The data is unpacked in pieces, so as a rule only the entry is held in memory, not its whole solid block. Use SevenZipReadAll to read many files.
funcSevenZipReadAll(arc &: SevenZipArchive, destdir: str): int
Extracts every entry under destdir and returns how many were written; status reports the first problem. Names that would leave the folder are skipped, symbolic links are created only when their target stays inside the folder, and each solid block is unpacked once. Entries are written to disk in pieces, so there is no limit on their size.
Opens a 7z on disk by reading its end header only (an encrypted or compressed header is unpacked first), so a file of any size opens at once. Pass the first piece (name.7z.001) of a split archive and the pieces that follow are found and read as one file, without being joined in memory. Check status before using the result.
Unpacks one entry to a file. LZMA, LZMA2, DEFLATE, DEFLATE64, copy, delta and byte swap run as a stream with a dictionary sized window, so memory stays small and an entry can pass 2 GiB (a solid block is decoded from its start, then stops after the entry). cap is the most bytes an entry may have (0 for none) and gives SEVENZIP_TOO_BIG before anything is written. memcap is the most memory the decoder may use (0 for none). The branch filters, BCJ2, PPMd, bzip2 and AES-256 stream too, alone or in chains such as AES, LZMA2 and BCJ. Past cap or memcap the result is SEVENZIP_TOO_BIG; a PPMd model over 2 GiB is refused with the same status.
funcSevenZipFileExtractAll(sf &: SevenZipFile, destdir: str, cap: i64, memcap: i64): int
Unpacks every entry under a folder, one pass per solid block, with the same checks, cap and memcap. Unsafe names are skipped and reported as SEVENZIP_UNSAFE_PATH; symbolic links are made when their target stays inside the folder. Returns the first problem, or SEVENZIP_OK.
Like SevenZipRead with a 64-bit cap in bytes (0 means the 2 GiB a buffer holds). An entry over it returns an empty buffer and sets status to SEVENZIP_TOO_BIG.doc.SevenZipListStatus = Like SevenZipList, and status says why the list is empty when the archive could not be read.
funcSevenZipReadAllCap(arc &: SevenZipArchive, destdir: str, cap: i64, memcap: i64): int
Like SevenZipReadAll with limits: cap is the most bytes one entry may unpack to (0 for none; an entry over it is skipped and status becomes SEVENZIP_TOO_BIG) and memcap the most memory a decoder may use (0 for none).
Makes a tar input that is read from a file on disk when the archive is written, so TarCreateFile, TarGzCreateFile and TarZstdCreateFile never hold it in memory. Entries of 8 GiB or more get a PAX size record.
Writes a .tar.bz2 file at bzip2 level 9. The tar stream is compressed as one bzip2 stream while it is written, one block at a time, so memory stays at a block whatever the size of the inputs; the result is what Bzip2CompressLevel gives on the whole tar. Returns false, and leaves no file, when an input cannot be read.
Writes every tar entry into the target directory, with links, modes and times. Returns false when it had to skip an unsafe name or link, or an entry it cannot make.
The example for this item is shown with the ones above: archive-Tar.salam
Like TarZstdCreateFile, at the zstd level you give (1 to 22; the default is 3). The whole tar stream is compressed as one zstd frame while it is written, so memory stays at the encoder window and the size is that of ZstdCompressLevel on the whole tar. Older files made in 4 MB pieces are still read.
The contents of one RAR entry, found by its exact name. status is set to one of the RAR_ codes: RAR_OK on success, otherwise the reason, and the buffer is empty. max caps the unpacked size, or 0 for no cap.
The example for this item is shown with the ones above: archive-Rar.salam
funcRarExtractByName(p: void*, n: int, name: str, ok &: bool): compress.ByteBuf
The contents of one RAR entry, found by its exact name. ok is false when it is missing, unsupported, encrypted or damaged; use RarExtract to learn which.
The example for this item is shown with the ones above: archive-Zip.salam
funcRarExtractAllStatus(p: void*, n: int, destdir: str, status &: int): int
Same as RarExtractAll, but returns how many entries were written and sets status to the first problem it met. An entry whose name would leave the target directory, or a symbolic link whose target would, is skipped and reported as RAR_UNSAFE_PATH.
The example for this item is shown with the ones above: archive-Rar.salam
funcRarReadVolumes(first: str, ok &: bool): Vector<compress.ByteBuf>
Reads the first volume and every following one it can find by the usual names (name.part01.rar, name.part02.rar, or name.rar, name.r00, name.r01). ok is false when the first file cannot be read.
archive-RarVolumes.salam
import archive
import compress
const VOL1 := """
526172211a0701006de128270b01050701060101808080002c64b9412502130be78600048df500a48302c8ac493d8005
0105762e7478740a0313c60bf16a7ed75827cc5dc30845643444334f3565365536a7751848c413a8c1d5a93ff0a349ca
1575f48991859458af2f2ba874489a028b323f09e3cf1e32ffefaf9f97e7e9e9e3edf6f1fa579a7a7d7c3dbdfcbebe3e
7f9f0fa7dbc3d3d3c16d1eff9ff7f7f2fd6ec95fb7f5e3f169de0de1e7efea4f755ebf7e5fe7d7f1fbe5efdec9ebf7f0
ac07f5f8f3ffbd1b1bddbb560ebd7589d7ab57eac6759257a2be6b5bff06db5907b7bfa37e2771d6115e7d65b5b1ead5
bc777f89dd96b66f649d2dbd8d5fb76ce932faf6d637ab6eb48ebc7784ebcd71b4dd9bcee1bd1377eeefbd11bfaa6188
98a280bb6b6d5a47dab204c174a6eafdbcad94b9666ab7f59414fe5a1970fab96f34eaf29a0edb711056b5ceb7494e85
b83775441bc51f56f4dc85bb03667c21d75603e2af914e76c0fb7fb5f428b8fa03bdef2854a35f5ad1273344a09bfacf
3d171047a766aa0d8c11896e85ced5a3fffcee5af7d608ba9d99ccb1aa2be1aa1342ae8b3ae9744562654f1606e38681
3201eddbf1afb701e2c1bbbff9efe02d2e17480a0962ab9b850cc218ea736bad10726ac6adab51e376d55ac13a036d61
8ec3818e63acd5ef5acb6746eae8ead37a7bb694b7a4c144844a6fa10afaf9603e08b482196964dcc8844ab4e013abdb
b4253899ef86069c3bb146ea4f805c057d5d03e101866c22c2413f3901cef35cb2faa62459ed943aafe3345d4aac4411
bb6c32bd58e376ad8393fda6c9ab56441546a250934dc2b662a55ca908f1fb6b12556784fd827901ad81c55caab012f8
0a35c584d114212450a1a8e1dc067711a39dda12659ce0d861a2ce3fafba224e2122c97cd0f5f92425ddfb9c36c9c953
6714555e7051d7dc252c7646f0df25808c58e78b266f0b92b8b917e2c910c0c3af16037381d7939fc090853cdd8df086
7370634378a2c228d4a8c0273788104daf093c9e6dccb0410dceb0215c0610eebaf222187780368a3dd2303226e99cf9
803e25ff56547e270704384ae9021fbc501683ba7cb179dcc0ae76d5600476f4c2ac82942f313d6213c24edbc0445454
d01f0c5e11d1f2ab6437eeafd08d1a3e40e3b6028205a2d7c3383a23c3fad885328bc65c5718d8d8630cfafc1b88c05c
bceb14fad1feaf94c163c9738d355824356adba1ca97a4a959832f630220a908b8b815c8b9acf685c0a255b735826132
911a80b69b45726abd4f52371614f9e267fe5840155b011bb28b47512603050401000000000000000000000000000000
000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000
"""const VOL2 := """
526172211a070100a9c2c4ca0c01050703010601018080800064563ccc25021b0be68600048df500a483020002230880
050105762e7478740a0313c60bf16a7ed75827046840f3834518cedc2f96f6bbb89190a3b6d9c514aacde7ee173d264c
b8f0692c7ca6b792780ee2bef245c9c3c949222294d2e016d242cd89838cbfa39edcb53775439267227627f0b5b02174
c9d22a0f2190336159b20695cd4c7113ce32dd1e2151829ab73dfea0950912309fda1d69b59f9b6e641be295e0f1f394
d8fd5c492fccc8a69b3426b4ab0d12b035942a0dfc9ca3a2401d11d385b8a8f007ead59f3e1694e6f315576c22200f28
4cd5c4a56653424b06d609611a293322bceeaf554095f3cca784064d2e5174624dd2a741e712bf8435f0a0c44458c205
f0a4b74671b82bb9635e6e10b24863da4475cc8f5a03b84b084d8bbf0834706da6cc792f4240b267a1fc20e04f3f4633
546acb84c2681dee1c00a436b6d9cccfdbffa1cad473bc7540b0002c96977021c9e8b443db4c80abd793bb7bd0d3305d
44416d504a0a9b699832d700ad111e6478d7e798ad90f492f8d248b49cd1380d23cfce8c4692ca5f5c3e8958a9b66477
c2a7d91522b7672ec51114c161dd650d948da76149d23cccc99308d707fc1db5100c4eba404d61309b83bb3c7503742b
a94a7440bae2115fd613fe21302d369c8b24bbe9c988d4eec33d9fbdc7c9038887de34419353b1bf82c0855b6c2277d6
8a8492d6e708fc7de0fa008c102633d12552fcaae7639fa3a0de6daf2336895df8a09169ed7e67ba0af51a4119c1daaa
71d7c254c05abbdab89669a40940e34742bf31a601545cdd2010c7e55016edb49cd2d7073b67d5f9ab38d9c418e76a44
f8394a65a783ab16db093629a33bd2ac91bdfb948263c08a0e4c9a4cae7af9eff46102d7bc913b5cb84a12b623a0a09f
ed11700ead0b984e3e78193ce0190ede74759a8ba34e3cee326dab0b3482d69a9af4f2e125c2d5c85cebccceb0e79c72
862a1f4252f2a18c33614af63cd523378109df9c8408d09c7e1dcc497bf400d082d87505f29af8f49a2034d6e575af9c
0e0ae77f857693f8e11cebcfb19f40a6fd1856362b74b7821b726242542b9c1c9c0add41783d931149e85a37418dba78
73ad990a9a4c7ac61b257db791159b11375886f2655cf56469191a5f853dba4f909dd88210d27dae6335450e4886e1a9
0976178ef226e03a38f420961ab7e866c8e252e62d8d1cd7eaaa2634d2747769c7a19d0e2d6c3c119034c1092a54ae36
4d4888b31633aa9473da001976173bcb5422b5d9d1922d044d8b47512603050401000000000000000000000000000000
000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000
"""const VOL3 := """
526172211a0701004ac54b440c0105070302060101808080004fb1c48c25020b0bfa8300048df500a4830287c98a0f80
050105762e7478740a0313c60bf16a7ed758276e9e2146d09044d59e5c974bc9e14a51910cdd41b71dcbf4f11fafd98d
16fb2d0683270a40c720ef76ab99b90b355e72e2acf71ea3c079e458b57797a810a9320e5b8f495efd066558b0974909
e6a8cff09481c66b5ee1259c3d36f48eeb7ec0f22fe946721c718c317a6058d161bf759e1c1e9a5b9427d538a4a3aade
0c326b40453c6b35b52d882e9413dbc82c5dc62160fc39a3b92dce54ac533a592fd09af8aba6fc2578f5cf12048671b2
740297a0d81a2d8f87113e877a3d3645e68034c922e7f1af8d0a794f3061e8696f2def1b8d951da84ce31a294f7c79a9
d784db31486ab60718767955e357868e61a57a866f4d072a16d82b283f8370e98ca225dd77dcf92004ea79317d93484a
62722811c2d2124f214fa55393d104c65b346abd4c63ecea91ca5bab74f0eb6af2378bc9734b8ad6ad79745d233884a7
6f99443c8e46eceeb4b9bc79895e4dedea7a4a4a2f1126126cadd27c20d3f24c0fbff173f03b3e4974014f428733869b
7f1b1916980a4335126a67984987f010b31fdcfd33edf46177414ac7a83f0909729736c70710e4bd7fb79cbf1924248d
8a2111eb874fa21f667084ea442d5950d187fd9eb2f066e09224441fbc9f32719e36176cab4d67b74200ba2c55397958
d35451df7777b403b0a724d22cb9a9a5ce7a7d72484b628b53c8b1cb893c158e558776fe1fc54ecdbc7cbe36e87c047d
942702030b8500048500a4830210c43fd080000109736d616c6c2e7478740a0313bd0bf16a5abb310b74696e790a1d77
565103050400
"""funcdigit(c: int): int:
if c >= 48and c <= 57:
ret c - 48endif c >= 97and c <= 102:
ret c - 87endret -1endfuncfrom_hex(text: str): compress.ByteBuf:
mut out := compress.NewByteBuf()
bytes := text asu8*
mut high := -1repeat text.Len() in i:
v := digit(bytes[i] asint)
if v < 0:
continueendif high < 0:
high = v
else:
compress.BufPush(out, high * 16 + v)
high = -1endendret out
endfuncmain:
mut volumes := Vector {} asVector<compress.ByteBuf>
volumes.Push(from_hex(VOL1))
volumes.Push(from_hex(VOL2))
volumes.Push(from_hex(VOL3))
mut arc := archive.RarOpenVolumes(volumes, "")
mut names := archive.RarEntries(arc)
each e in names:
data := archive.RarRead(arc, e.name, 0)
println e.name, e.size, data.len, archive.RarStatusText(arc.status)
end
archive.FreeEntries(names)
archive.RarClose(arc)
mut partial := Vector {} asVector<compress.ByteBuf>
partial.Push(volumes.Get(0))
partial.Push(volumes.Get(1))
mut broken := archive.RarOpenVolumes(partial, "")
gone := archive.RarRead(broken, "v.txt", 0)
println gone.len, broken.status == archive.RAR_MULTIVOLUME
archive.RarClose(broken)
archive.WriteBinaryFileBuf("set.part01.rar", volumes.Get(0))
archive.WriteBinaryFileBuf("set.part02.rar", volumes.Get(1))
archive.WriteBinaryFileBuf("set.part03.rar", volumes.Get(2))
mut ok := falsemut found := archive.RarReadVolumes("set.part01.rar", ok)
println ok, found.Len()
archive.RarFreeVolumes(found)
end
Output
v.txt 14989 14989 ok
small.txt 5 5 ok
0 true
true 3
funcGzipDecompressFileCap(inpath: str, outpath: str, cap: i64): int
Gunzips a file to another file in 64 KiB pieces, checking the CRC-32 and the size at the end, and returns an ARCHIVE_... status. cap limits the output in bytes (0 for none); a partly written output is removed on failure.
Opens a zip on disk by reading only its end and its central directory, so a file of any size opens at once. Sizes and offsets are 64 bit, zip64 included. Entries stored, DEFLATE, DEFLATE64, LZMA, bzip2 and zstd all unpack as a stream. A split archive opens through any of its pieces (.zip, .z01 or .zip.001) and is read in place as one file, zip64 end records and disk numbers included; a missing piece gives ZIP_MULTIVOLUME. Check status before using the result.
Sets the password used for encrypted entries (the old zip encryption and WinZip AES-128, AES-192 and AES-256). A wrong password gives ZIP_BAD_PASSWORD and no password gives ZIP_ENCRYPTED. Encrypted data is decrypted into a temporary file first, so the disk needs room for the packed size of one entry.
funcZipFileExtractTo(zf &: ZipFile, name: str, destpath: str, cap: i64): int
Unpacks one entry to a file, reading the archive and writing the result in small pieces, so memory use does not depend on the size of the entry. The CRC-32 and the size are checked at the end; on a failure the partly written file is removed. cap is a limit in bytes for the unpacked data (0 for none): an entry over it returns ZIP_TOO_BIG and writes nothing.
funcZipFileExtractAll(zf &: ZipFile, destdir: str, cap: i64): int
Unpacks every entry under a folder the same way, with the same checks and the same cap for each entry. Unsafe names are skipped. The result is ZIP_OK or the status of the first entry that failed.
funcZstdDecompressFileCap(inpath: str, outpath: str, cap: i64): int
Decodes a .zst file to another file without loading either; the window is held in memory. cap limits the output. Returns ARCHIVE_OK, ARCHIVE_TOO_BIG, ARCHIVE_CORRUPT or ARCHIVE_IO_ERROR and removes the output on failure.
archive-TarStream.salam
import archive
import os
funcmain:
body := "streamed straight from the compressed file\n"mut files := Vector {} asVector<archive.TarFileInput>
files.Push(
archive.MakeTarFileInput("notes/a.txt", body asvoid*, body.Len(), false)
)
files.Push(archive.MakeTarFileInput("notes/b.txt", body asvoid*, 10, false))
println archive.TarGzCreateFile(files, "pack.tar.gz")
files.Free()
mut st := 0mut entries := archive.TarCompressedFileEntries("pack.tar.gz", archive.FormatTarGz, st)
each e in entries:
println e.name, e.sizeendprintln st
archive.FreeEntries(entries)
println archive.TarGzFileExtractAll("pack.tar.gz", "unpacked", 0asi64)
println os.FileSize("unpacked/notes/a.txt"), os.FileSize("unpacked/notes/b.txt")
println archive.TarCompressedFileExtractAll(
"pack.tar.gz", archive.FormatTarGz, "capped", 20asi64
)
println os.Exists("capped/notes/a.txt"), os.Exists("capped/notes/b.txt")
os.RemoveAll("unpacked")
os.RemoveAll("capped")
os.Remove("pack.tar.gz")
end
Decompresses a Zstandard file; false when it cannot read or write or the data is not valid Zstandard. The checksum is verified and the whole result is held in memory first.
The example for this item is shown with the ones above: archive-Zstd.salam
Finds the format from the first bytes (and the header checksum for tar). A gzip, bzip2 or xz file is reported as itself even when a tar is inside; Open looks inside.
The example for this item is shown with the ones above: archive-Open.salam
Opens archive bytes of any format, found by its first bytes, with the default limits. A gzip, bzip2 or xz file is unpacked at once and a tar inside is recognized. Check status afterwards.
The example for this item is shown with the ones above: archive-Open.salam
Opens a file on disk. A zip, tar, tar.gz, 7z or RAR 5 file is not read into memory: only its list of entries is, and every call after that reads and writes in pieces, so the file can be of any size and data stays null. A tar.gz is read as a stream through gzip with no temporary file: listing and extracting each decode it again from the start, stopping as soon as the entry wanted is done, and max_total limits the unpacked bytes of one pass. Other formats (bzip2, xz, zstd, a single gzip file, a RAR with a password) are read whole and opened like Open. Limits apply either way.
The example for this item is shown with the ones above: archive-Open.salam
Extracts everything into a folder. First every entry is checked against the limits and for unsafe names, absolute paths, .., links that leave the folder, writes through links and device entries; if any check fails nothing is written and status says why. Returns the number of files, or 0 on failure.
The example for this item is shown with the ones above: archive-Open.salam
funcCreateBuf(format: int, files: Vector<FileInput>, status &: int): compress.ByteBuf
Builds an archive of the given Format in memory: zip, tar, tar.gz, tar.bz2, rar and 7z (stored), or gzip and bzip2 with exactly one file. Other formats give ARCHIVE_UNSUPPORTED; an unsafe name gives ARCHIVE_UNSAFE_PATH.
The example for this item is shown with the ones above: archive-Open.salam
Writes a zip archive as pieces of at most part_size bytes (at least 65536, under 4 GiB). With ZipSplitDisk the pieces are name.z01, name.z02 and so on, with the last one named name.zip, the way Info-ZIP and 7-Zip write them: every piece has its disk number in the central directory, no header is split across two pieces, and the first piece starts with the spanning marker. With ZipSplitByte the finished archive is cut into name.zip.001, name.zip.002 and so on, which 7-Zip opens as one file. Entries are streamed as in ZipCreateFile and can use any method and password. The archive is first written whole to name.salamsplit.tmp next to the output, so the disk needs room for it twice. Returns false and removes the pieces when something fails.
Sets how many bytes the gz, bz2, xz, zstd, tar.gz and zip file writers hold and compress at a time (default 4 MiB, anything under 4096 restores the default). The tar.zst and tar.bz2 writers ignore it: they compress the whole tar as one stream. Smaller chunks use less memory and compress a little worse.
Compresses a file into an .xz file (level 6, CRC-64 check); false when it cannot read or write. The file is read and compressed in chunks of SetWriteChunk bytes, written as one .xz stream per chunk.
archive-Xz.salam
import archive
import compress
import filepath
import os
const XZ := """
fd377a585a000004e6d6b44604c05fcf022101160000000000000000bb0ccf6ce0014e00575d0029984986240afb8a34e4
81db780958f3d0144436dd3bb4ca45234051fa042e5be576402f66b72801fb0f3b421e32e3eba071037cdd425338444ede
4cb8c8c742828bc92c1b53df8ea763015d902c3d49f9c602aacf40a000005b35fff6fd65c4e300017bcf020000005e9efc
63b1c467fb020000000004595a
"""const LZMA := """
5d00008000ffffffffffffffff0029984986240afb8a34e481db780958f3d0144436dd3bb4ca45234051fa042e5be57640
2f66b72801fb0f3b421e32e3eba071037cdd425338444ede4cb8c8c742828bc92c1b53df8ea763015d902c3d49f9c602ad
8d68e0bcfed44dc0
"""funcnib(c: int): int:
if c >= 48and c <= 57:
ret c - 48endif c >= 97and c <= 102:
ret c - 87endret -1endfuncunhex(s: str): compress.ByteBuf:
mut b := compress.NewByteBuf()
bp := s asu8*
mut hi := -1repeat s.Len() in i:
v := nib(bp[i] asint)
if v < 0:
continueendif hi < 0:
hi = v
else:
compress.BufPush(b, hi * 16 + v)
hi = -1endendret b
endfuncmain:
dir := filepath.Join(os.TempDir(), "salam_xz_example")
os.RemoveAll(dir)
os.MkdirAll(dir)
xz_path := filepath.Join(dir, "notes.txt.xz")
lzma_path := filepath.Join(dir, "notes.txt.lzma")
out_path := filepath.Join(dir, "notes.txt")
archive.WriteBinaryFileBuf(xz_path, unhex(XZ))
archive.WriteBinaryFileBuf(lzma_path, unhex(LZMA))
println"xz:", archive.XzDecompressFile(xz_path, out_path)
mut ok := false
text := archive.ReadBinaryFile(out_path, ok)
println ok, text.len, "bytes"println"lzma:", archive.LzmaDecompressFile(lzma_path, out_path)
println"wrong format:", archive.XzDecompressFile(lzma_path, out_path)
println"missing:", archive.XzDecompressFile(filepath.Join(dir, "none.xz"), out_path)
plain := filepath.Join(dir, "plain.txt")
archive.WriteBinaryFileBuf(plain, text)
packed := filepath.Join(dir, "plain.txt.xz")
println"pack xz:",
archive.XzCompressFile(plain, packed),
archive.XzDecompressFile(packed, out_path)
println"pack lzma:",
archive.LzmaCompressFile(plain, lzma_path),
archive.LzmaDecompressFile(lzma_path, out_path)
println"pack missing:", archive.XzCompressFile(filepath.Join(dir, "none.txt"), packed)
os.RemoveAll(dir)
end
Decompresses an .xz file into a new file and returns true on success. The checks inside the file are verified; nothing is written when one fails. The whole result is held in memory first.
The example for this item is shown with the ones above: archive-Xz.salam
funcXzDecompressFileCap(inpath: str, outpath: str, cap: i64): int
Decompresses an .xz file into a new file and returns an ARCHIVE_... status. cap limits the output in bytes (0 for none); ARCHIVE_TOO_BIG means the data would pass it and ARCHIVE_CORRUPT that the file is damaged. Nothing is left behind on failure.
Opens a RAR file by reading only its headers when it is RAR 5. Offsets and sizes are 64 bit. An archive with encrypted files or names needs RarFileOpenWith; a set of volumes needs RarFileOpenVolumes. Without them such files report RAR_ENCRYPTED or RAR_MULTIVOLUME.
Like RarFileOpen, with a password for archives whose files or file names are encrypted. A wrong password reports RAR_BAD_PASSWORD. Encrypted RAR 5 data is decrypted while it is unpacked, so it never has to fit in memory.
Opens a multi-volume RAR 5 set from its first volume (name.part1.rar, name.part01.rar or name.rar with .r00, .r01...) and finds the rest by name. A file split across volumes is read through all of them in turn, encrypted or not, without joining them in memory; password may be empty. A missing volume reports RAR_MULTIVOLUME for the files that cross it.
Unpacks one entry to a file. The unpacked data is written in pieces and checked against the CRC-32 on the way, so it never has to fit in memory; only the decoder window and one block of packed data are held, whatever the size of the entry. cap is a limit in bytes for the unpacked data (0 for none), RAR_TOO_BIG is returned for an entry over it. memcap limits the window and block memory (0 for the 1 GiB default, which is also the most the decoder window can be): a dictionary bigger than that is not refused up front: the entry is unpacked with a window of that size, and RAR_WINDOW_TOO_BIG is returned only if the data really points farther back than the window (a RAR 7 archive made with a 6 GiB dictionary unpacks when its matches stay near).
funcRarFileExtractAll(rf &: RarFile, destdir: str, cap: i64, memcap: i64): int
Unpacks every entry under a folder, solid archives included, with the same limits. The result is RAR_OK or the status of the first entry that failed. Permissions and modification times are restored, symbolic links whose target stays inside the folder are created, and hard links and file copies become plain copies, as in RarReadAll; unsafe names and links are skipped and reported as RAR_UNSAFE_PATH.
Reads the next entry into e and returns false at the end. Sizes and offsets are 64 bit, so entries over 2 GB are listed normally. Free each entry with FreeEntry.
funcTarFileExtractTo(tf &: TarFile, e: Entry, destpath: str, cap: i64): int
Copies the data of the entry returned by the last TarFileNext to a file, in 64 KiB pieces, so memory use does not depend on its size. cap is a limit in bytes (0 for none): a bigger entry returns TAR_TOO_BIG and writes nothing.
funcTarFileExtractAll(path: str, destdir: str, cap: i64): int
Unpacks a tar on disk under a folder with the same safety rules as TarExtractAll and the same cap for each file. The result is TAR_OK or the status of the first entry that failed.
funcTarGzFileExtractAll(path: str, destdir: str, cap: i64): int
Unpacks a .tar.gz on disk without writing the tar anywhere: the gzip data is inflated in pieces and fed straight to the tar reader. Same checks as TarFileExtractAll; cap limits each file. A cut or damaged stream gives TAR_CORRUPT and removes the file that was being written.
Builds a zip archive in memory. Names with non-ASCII bytes get the UTF-8 flag. Any method other than 8 stores the file. The result is empty when an input is invalid (negative length, null address with a length, name over 65535 bytes) or the total is over about 1.9 GB. The archive is written by the same entry writer as ZipCreateFile, into a growing buffer, so every entry is compressed in pieces by the encoders and no second copy of the archive body is built; what stays in memory is the returned archive and your input buffers. Encrypted entries take the same path.
The example for this item is shown with the ones above: archive-Zip.salam
Like ZipCreate, but always writes the zip64 form: 64-bit sizes and offsets in extra fields, the zip64 end record and its locator. unzip and 7-Zip read it; use it to test zip64 readers. Empty on invalid input.
archive-ZipTime.salam
import archive
import compress
funcmain:
text := "salam, salam, salam"
b := compress.BufFromStr(text)
mut named := archive.MakeZipFileInput("سلام.txt", b.data, b.len, 8)
named.mtime = 1700000000asi64mut files := Vector {} asVector<archive.ZipFileInput>
files.Push(named)
files.Push(archive.MakeZipFileInput("plain.txt", b.data, b.len, 8))
z := archive.ZipCreate(files)
mut entries := archive.ZipList(z.data, z.len)
each e in entries:
println e.name, e.mtime
end
archive.FreeEntries(entries)
println"utf8 flag:", (archive.LEGet16(z.data, 6) & 0x800) != 0
wide := archive.ZipCreate64(files)
mut again := archive.ZipList(wide.data, wide.len)
println again.Len(), wide.len > z.len, again.Get(0).size
archive.FreeEntries(again)
mut checked := 0println archive.ZipVerify(wide.data, wide.len, checked), checked
end
Like ZipCreateFile, but always writes the zip64 form. Without it ZipCreateFile still switches to zip64 for entries of 4 GB or more, for offsets past 4 GB and for 65535 entries or more.
Makes a zip input that is read from a file on disk when the archive is written. Stored, DEFLATE, bzip2, LZMA and zstd entries are all read and compressed in pieces.
funcZipExtractByName(p: void*, n: int, name: str, ok &: bool): compress.ByteBuf
Pulls out the contents of one entry; ok becomes false when the name is not found, the entry is encrypted, or its method is not 0 or 8. The CRC is not checked.
The example for this item is shown with the ones above: archive-Zip.salam
Same as ZipExtractByName, but ok becomes false when the entry would be larger than max bytes, checked while it is inflated. Use it on archives from other people, where a few kilobytes can claim to hold gigabytes.
The example for this item is shown with the ones above: archive-Zip.salam
Pulls out one entry and checks its size and CRC-32. status is set to one of the ZIP_ codes, ZIP_OK on success, otherwise the reason, and the buffer is empty. max caps the unpacked size, or 0 for no cap.
funcZipVerify(p: void*, n: int, checked &: int): int
Inflates every file entry and checks its size and CRC-32. Sets checked to how many it looked at and returns how many failed (encrypted, unsupported or too big count as failed), 0 when all are good, or -1 when the data is not a zip.
Writes every entry into the target directory, after checking its CRC-32. An entry with an unsafe name or a failed check is not written and the result is false. When the data is not a zip it has no entries and nothing is written.
The example for this item is shown with the ones above: archive-Zip.salam
funcTarCompressedFileExtractAll(path: str, format: int, destdir: str, cap: i64): int
Unpacks a compressed tar on disk in one pass. format is FormatTarGz, FormatTarBz2, FormatTarXz or FormatTarZstd. Nothing but the current file is held; the checks and the cap are those of TarFileExtractAll.
funcTarCompressedFileEntries(path: str, format: int, status &: int): Vector<Entry>
Lists a compressed tar on disk in one pass without unpacking the contents. status is set to TAR_OK or why the stream ended early. Free the result with FreeEntries.
Unpacks one file of a compressed tar on disk to destpath in one pass, stopping as soon as it is written. cap limits that file and total the whole unpacked stream (0 for none). Returns TAR_NOT_FOUND when no file has that name.
Builds a 7z archive in memory. At level 0 (the default) the files are stored without compression in one solid block, with a CRC-32 each; SevenZipCreateLevel compresses each file with LZMA instead. 7-Zip opens the result. A name that is empty or a negative length gives an empty buffer.
Writes a 7z archive to disk at a level like SevenZipCreateLevel. Level 0 copies the files from disk without loading them; with a level only one file is in memory at a time.