archive
archive
Section titled “archive”The archive module provides in-memory creation and extraction of zip and tar
archives. Archives are represented as Array<int> byte arrays. Individual entries
are objects with name and data string fields.
Import
Section titled “Import”use std::core::archiveEntry Format
Section titled “Entry Format”Archive entries are HashMaps with two fields:
| Field | Type | Description |
|---|---|---|
name | string | File path within the archive |
data | string | File contents as text |
Zip Functions
Section titled “Zip Functions”archive::zip_create(entries: Array<{name, data}>) -> Array<int>
Section titled “archive::zip_create(entries: Array<{name, data}>) -> Array<int>”Create a zip archive in memory from an array of entries. Files are compressed using deflate.
let zip_bytes = archive::zip_create([ { name: "hello.txt", data: "Hello, World!" }, { name: "data/numbers.txt", data: "1 2 3 4 5" }])archive::zip_extract(data: Array<int>) -> Array<{name, data}>
Section titled “archive::zip_extract(data: Array<int>) -> Array<{name, data}>”Extract a zip archive from a byte array. Returns an array of entry objects. Directory entries are skipped.
let entries = archive::zip_extract(zip_bytes)for entry in entries { print(f"{entry.name}: {entry.data.length} bytes")}Tar Functions
Section titled “Tar Functions”archive::tar_create(entries: Array<{name, data}>) -> Array<int>
Section titled “archive::tar_create(entries: Array<{name, data}>) -> Array<int>”Create a tar archive in memory from an array of entries.
let tar_bytes = archive::tar_create([ { name: "readme.txt", data: "Hello from tar" }])archive::tar_extract(data: Array<int>) -> Array<{name, data}>
Section titled “archive::tar_extract(data: Array<int>) -> Array<{name, data}>”Extract a tar archive from a byte array. Returns an array of entry objects. Directory entries are skipped.
let entries = archive::tar_extract(tar_bytes)Example: Roundtrip
Section titled “Example: Roundtrip”use std::core::archive
let entries = [ { name: "src/main.shape", data: "fn main() { print(\"hello\") }" }, { name: "README.txt", data: "My project" }]
let zipped = archive::zip_create(entries)let extracted = archive::zip_extract(zipped)assert extracted.length == 2