Quickstart
Look inside an archive, read one member and extract it, from the command line, from Rust and from C.
The same three steps on each surface: look inside an archive, read one member, extract it. Every example here was run against a small ZIP before it was published.
Command line
Make a test archive, then look inside it without extracting:
mkdir -p src && echo hello > src/a.txt && echo world > src/b.md
peel create demo.zip src
peel inspect demo.zip -jinspect -j prints the manifest as JSON (trimmed here):
{
"format": "zip",
"format_family": "zip",
"has_encrypted_entries": false,
"total_uncompressed_bytes": 12,
"total_compressed_bytes": 16,
"entries": [
{ "name": "src/a.txt", "kind": "file", "size": 6, "compressed_size": 8,
"compression_method": "Deflated", "crc32": 909783072,
"mode": 33188, "modified_unix": 1794121084, "is_encrypted": false }
]
}Read one member to standard output, then extract the lot:
peel cat demo.zip src/a.txt
peel extract demo.zip -o out -j{
"format": "zip",
"destination": "out",
"extracted": ["out/src", "out/src/a.txt", "out/src/b.md"],
"skipped": [],
"sources": ["demo.zip"]
}Scripts should branch on the exit code and read the JSON error from stderr. See the CLI reference.
Rust
Add peel-core as described in Install, then:
use peel_core::{
detect_format, inspect_archive, peel, EnclosingDirectory, ExtractPolicy, ResourceBudget,
};
use std::path::Path;
fn main() -> anyhow::Result<()> {
let archive = Path::new("upload.zip");
// What is it? Cheap: reads the index, not the contents.
let format = detect_format(archive)?;
println!("format: {format:?}");
// The manifest, without extracting.
let summary = inspect_archive(archive, &ResourceBudget::default())?;
println!("{} entries, {} bytes uncompressed",
summary.entry_count(), summary.total_uncompressed_bytes);
// Extract under a policy. Containment, bomb limits and cleanup on failure apply.
let policy = ExtractPolicy {
destination: Some("out".into()),
enclosing_directory: EnclosingDirectory::Always,
..ExtractPolicy::default()
};
let result = peel(archive, &policy)?;
println!("wrote {} paths to {}", result.extracted.len(), result.destination.display());
Ok(())
}Output for the demo archive:
format: Zip
3 entries, 12 bytes uncompressed
wrote 3 paths to out/upload-peeledMore in the library guide.
C
Build the library (see Install), then list an archive and read a member through a session. Every string you get back is freed with peel_string_free, and member bytes with peel_bytes_free.
#include <stdio.h>
#include "peel.h"
int main(int argc, char **argv) {
if (argc < 3) { fprintf(stderr, "usage: demo ARCHIVE MEMBER\n"); return 2; }
/* 1. List: heap UTF-8 JSON, either the entries or {"error": ...}. */
char *json = peel_list(argv[1]);
if (!json) return 1;
puts(json);
peel_string_free(json);
/* 2. Open once, read a member into memory. */
char *err = NULL;
PeelSession *s = peel_session_open(argv[1], NULL, &err);
if (!s) { fprintf(stderr, "%s\n", err); peel_string_free(err); return 1; }
size_t len = 0;
unsigned char *buf = peel_session_read_member(s, argv[2], 64u << 20, NULL, &len, &err);
if (buf) {
fwrite(buf, 1, len, stdout);
peel_bytes_free(buf, len);
} else {
fprintf(stderr, "%s\n", err);
peel_string_free(err);
}
peel_session_close(s);
return buf ? 0 : 1;
}cc demo.c -I include -L target/release -lpeel_ffi -Wl,-rpath,$PWD/target/release -o demo
./demo demo.zip src/a.txtThe C library guide covers ownership, errors, cancellation, and Swift, Python, Go and Node.
void peel_bytes_free(unsigned char *ptr, size_t len) Free a buffer returned by peel_read_member or a session read.char *peel_list(const char *archive_path) List every entry as JSON.void peel_session_close(PeelSession *session) Close and free a session; NULL is ignored.PeelSession *peel_session_open(const char *archive_path, const char *password, char **error_json) Open an archive once and keep its index, for many reads. NULL with an error JSON on failure.unsigned char *peel_session_read_member(const PeelSession *session, const char *member, uint64_t max_bytes, const PeelCancelToken *token, size_t *out_len, char **error_json) Read one member through an open session into a heap buffer.void peel_string_free(char *s) Free a JSON string returned by any peel_* call.detect_format(&Path) Format: sniffed from content, not the extension.inspect_archive(&Path, &ResourceBudget) ArchiveSummary: format, format_family, zip_kind, entries: Vec<ArchiveEntryInfo>, total_uncompressed_bytes, total_compressed_bytes, has_encrypted_entries, comment, is_solid.