Quickstart

Look inside an archive, read one member and extract it, from the command line, from Rust and from C.

Peel 1.4.0 · Snack Studio · Updated

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:

TerminalShell
mkdir -p src && echo hello > src/a.txt && echo world > src/b.md
peel create demo.zip src
peel inspect demo.zip -j

inspect -j prints the manifest as JSON (trimmed here):

peel inspect demo.zip -jJSON
{
  "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:

TerminalShell
peel cat demo.zip src/a.txt
peel extract demo.zip -o out -j
peel extract demo.zip -o out -jJSON
{
  "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:

src/main.rsRust
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:

OutputText
format: Zip
3 entries, 12 bytes uncompressed
wrote 3 paths to out/upload-peeled

More 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.

demo.cC
#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;
}
TerminalShell
cc demo.c -I include -L target/release -lpeel_ffi -Wl,-rpath,$PWD/target/release -o demo
./demo demo.zip src/a.txt

The C library guide covers ownership, errors, cancellation, and Swift, Python, Go and Node.