zip
stableCreate, read, and extract ZIP archives. Provides free functions for one-shot operations and ZipReader/ZipWriter classes for streaming access.
use plugin zip::{extract, create, extract_entry, …} Functions (16)
- extract Extract all entries from a ZIP to a directory
- create Create a ZIP archive from a list of file paths
- extract_entry Extract a single named entry to a file
- list_entries List all entries in a ZIP without opening a reader
- ZipReader.new Open a ZIP file for random-access reading
- ZipReader.list List all entries with metadata
- ZipReader.read Read a named entry as raw bytes
- ZipReader.read_text Read a named entry as a UTF-8 string
- ZipReader.len Return the number of entries
- ZipReader.entry_info Get detailed metadata for a named entry
- ZipWriter.new Create an in-memory ZIP writer
- ZipWriter.add_file Add a string or bytes entry to the archive
- ZipWriter.add_directory Add a directory entry to the archive
- ZipWriter.add_file_from_disk Add a file by reading it from disk
- ZipWriter.finish Finalize and return archive bytes
- ZipWriter.save Finalize and write the archive to disk
Extract all entries from a ZIP to a directory
Extracts all file entries from the ZIP at zip_path into the directory dest_path, creating subdirectories as needed. Returns the number of files extracted.
use plugin zip::{extract}
let count = extract("archive.zip", "output/")
print("Extracted {count} files")
Create a ZIP archive from a list of file paths
Creates a ZIP archive at zip_path containing all files listed in the files table. Each value in the table must be a file path string; only the filename (not the full path) is used as the archive entry name. Returns the number of files added.
use plugin zip::{create}
let files = ["src/main.zolo", "src/lib.zolo", "README.md"]
let count = create("release.zip", files)
print("Packed {count} files")
Extract a single named entry to a file
Extracts a single named entry from the ZIP at zip_path and writes it to dest_path. Parent directories of dest_path are created automatically.
use plugin zip::{extract_entry}
extract_entry("data.zip", "config/settings.json", "out/settings.json")
print("Extracted settings.json")
List all entries in a ZIP without opening a reader
Opens the ZIP at zip_path and returns a 1-indexed table of entry metadata. Each entry is a table with keys name, size, compressed_size, and is_dir.
use plugin zip::{list_entries}
let entries = list_entries("bundle.zip")
for entry in entries {
print("{entry["name"]} ({entry["size"]} bytes)")
}
Open a ZIP file for random-access reading
Opens the ZIP file at path and returns a handle for random-access reading. Use ZipReader when you need to inspect or read multiple entries without re-opening the file each time.
use plugin zip::{ZipReader}
let reader = ZipReader.new("archive.zip")
print("Entries: {reader.len()}")
List all entries with metadata
Returns a 1-indexed table of all entries in the archive, each containing name, size, compressed_size, and is_dir.
use plugin zip::{ZipReader}
let reader = ZipReader.new("archive.zip")
let entries = reader.list()
for entry in entries {
if !entry["is_dir"] {
print(entry["name"])
}
}
Read a named entry as raw bytes
Reads the raw bytes of the entry named name from the archive. Use read_text instead when the content is known to be UTF-8 text.
use plugin zip::{ZipReader}
let reader = ZipReader.new("assets.zip")
let data = reader.read("images/logo.png")
print("Read {data.len()} bytes")
Read a named entry as a UTF-8 string
Reads the entry named name and decodes it as a UTF-8 string. Errors if the bytes are not valid UTF-8.
use plugin zip::{ZipReader}
let reader = ZipReader.new("project.zip")
let src = reader.read_text("src/main.zolo")
print(src)
Return the number of entries
Returns the total number of entries (files and directories) in the archive.
use plugin zip::{ZipReader}
let reader = ZipReader.new("archive.zip")
print("Total entries: {reader.len()}")
Get detailed metadata for a named entry
Returns detailed metadata for the named entry: name, size, compressed_size, is_dir, is_file, compression ("stored" or "deflated"), and crc32.
use plugin zip::{ZipReader}
let reader = ZipReader.new("archive.zip")
let info = reader.entry_info("data/config.yaml")
print("Compression: {info["compression"]}, CRC32: {info["crc32"]}")
Create an in-memory ZIP writer
Creates a new in-memory ZIP writer. Add files and directories with the add_* methods, then call finish to get the bytes or save to write to disk.
use plugin zip::{ZipWriter}
let writer = ZipWriter.new()
writer.add_file("hello.txt", "Hello, world!")
writer.save("output.zip")
Add a string or bytes entry to the archive
Adds an entry named name to the archive with data as its content. data may be a string or raw bytes.
use plugin zip::{ZipWriter}
let writer = ZipWriter.new()
writer.add_file("readme.txt", "This is a readme.")
writer.add_file("data.json", '{"version": 1}')
writer.save("bundle.zip")
Add a directory entry to the archive
Adds a directory entry named name to the archive. Use this to preserve directory structure before adding files inside it.
use plugin zip::{ZipWriter}
let writer = ZipWriter.new()
writer.add_directory("src/")
writer.add_file("src/main.zolo", "print(\"hello\")")
writer.save("project.zip")
Add a file by reading it from disk
Reads the file at path from disk and adds it to the archive under the entry name name. Useful when you want to control the archive path independently of the source path.
use plugin zip::{ZipWriter}
let writer = ZipWriter.new()
writer.add_file_from_disk("dist/app.zolo", "/build/output/app.zolo")
writer.add_file_from_disk("dist/lib.zolo", "/build/output/lib.zolo")
writer.save("release.zip")
Finalize and return archive bytes
Finalizes the archive and returns its contents as raw bytes. The writer cannot be used after calling finish. Use save if you want to write directly to disk instead.
use plugin zip::{ZipWriter}
let writer = ZipWriter.new()
writer.add_file("message.txt", "packed content")
let bytes = writer.finish()
print("Archive size: {bytes.len()} bytes")
Finalize and write the archive to disk
Finalizes the archive and writes it to the file at path. The writer cannot be used after calling save.
use plugin zip::{ZipWriter}
let writer = ZipWriter.new()
writer.add_file("config.yaml", "debug: true\n")
writer.save("config-backup.zip")
print("Saved archive")