cozip

C

  • Header core/cozip.h, C11
  • Writer with libzip 1.11.4 and zlib 1.3.1, reader with Karu 0.4.0

libcozip writes archives and reads Flat manifests for Python, R and Julia. The writer plans the ZIP layout and verifies its output; the reader fetches and checks the index through Karu.

libcozip handles payloads as bytes. Bindings use Arrow or DuckDB to write and decode the Parquet manifest.

A complete program

The program writes two buffers as an archive of profile 0 that indexes both, then reads the manifest of the public cities.zip and the location base of its files.

#include <cozip.h>
#include <stdio.h>

int main(void) {
    /* 1. Write two buffers as a profile 0 archive that indexes both. */
    static const char notes[] = "first file\n";
    static const char table[] = "id,label\n1,lima\n";
    cozip_entry_t entries[3] = {0};          /* n + 1: room for padding */
    const char *names[] = {"notes.txt", "data/table.csv"};
    const char *data[] = {notes, table};
    size_t sizes[] = {sizeof notes - 1, sizeof table - 1};

    for (int i = 0; i < 2; i++) {
        entries[i].arc_name = names[i];
        entries[i].payload_size = sizes[i];
        entries[i].in_index = true;
        entries[i].source.kind = COZIP_SOURCE_BUFFER;
        entries[i].source.u.buffer.data = (const uint8_t *)data[i];
        entries[i].source.u.buffer.size = sizes[i];
    }

    cozip_error_t err = {0};
    cozip_status_t s = cozip_finalize("small.zip", entries, 2, 3,
                                      COZIP_PROFILE_NONE, &err);
    if (s != COZIP_OK) {
        fprintf(stderr, "%s: %s\n", cozip_status_string(s), err.message);
        return 1;
    }
    for (int i = 0; i < 2; i++)
        printf("%s at %llu, %llu bytes\n", entries[i].arc_name,
               (unsigned long long)entries[i].payload_offset,
               (unsigned long long)entries[i].payload_size);

    /* 2. Read the manifest of a public Flat archive. */
    const char *url = "https://huggingface.co/datasets/asterisk-labs/"
                      "cozip-api-fixtures/resolve/v0.1.0/data/cities.zip";
    uint8_t *parquet = NULL;
    size_t parquet_size = 0;
    char *message = NULL;
    char *base = NULL;
    int failed = 1;

    s = cozip_read_flat_metadata(url, &parquet, &parquet_size, &message);
    if (s != COZIP_OK) {
        fprintf(stderr, "%s: %s\n", cozip_status_string(s),
                message ? message : "out of memory");
        goto cleanup;
    }
    s = cozip_location_base(url, &base, &message);
    if (s != COZIP_OK) {
        fprintf(stderr, "%s: %s\n", cozip_status_string(s),
                message ? message : "out of memory");
        goto cleanup;
    }
    printf("manifest: %zu bytes of Parquet, magic %.4s\n", parquet_size,
           (const char *)parquet);
    printf("location: /vsisubfile/141_181,%s\n", base);
    failed = 0;

cleanup:
    cozip_free(parquet);
    cozip_free(base);
    cozip_free(message);
    return failed;
}
notes.txt at 160, 11 bytes
data/table.csv at 215, 16 bytes
manifest: 11868 bytes of Parquet, magic PAR1
location: /vsisubfile/141_181,/vsicurl/https://huggingface.co/datasets/asterisk-labs/cozip-api-fixtures/resolve/v0.1.0/data/cities.zip

The index of two entries is 11 + (18 + 9) + (18 + 14) = 70 bytes, so the first local header is at byte 121 and notes.txt starts 30 + 9 bytes later, at 160. The planned archive is smaller than 32,819 bytes, so cozip_finalize used the spare slot for a __cozip_padding__ entry, and unzip -l small.zip lists four entries. The manifest bytes start with the Parquet magic PAR1, and any Parquet reader decodes them.

Build and link

libcozip builds with CMake from core/. libzip, zlib and Karu are vendored, so nothing is downloaded at build time.

cmake -S core -B core/build -G Ninja
cmake --build core/build
cc -std=c11 app.c -Icore -Icore/build core/build/cozip.dylib -Wl,-rpath,$PWD/core/build -o app
OptionDefaultEffect
COZIP_READERONBuilds the Flat reader. It links Karu, so it needs a C++23 compiler, libcurl 7.83 or newer and OpenSSL 3. With OFF the library is C11 with no system dependency, cozip_location_base still works and cozip_read_flat_metadata returns COZIP_ERR_UNSUPPORTED. Emscripten builds force it off.
COZIP_BUILD_TESTSOFFBuilds the test suite in core/tests, run with ctest.
COZIP_INSTALLONGenerates install rules and a cozipConfig.cmake that exports cozip::cozip. Emscripten builds force it off.
  • The shared library has no lib prefix, so it is cozip.dylib, cozip.so or cozip.dll and -lcozip does not find it. Link the file itself and set an rpath, as above.
  • Inside a CMake project, add_subdirectory(core) and link cozip::cozip.
  • The version comes from the repository's VERSION file into the generated version.h, and cozip_version_string returns it, such as "2026.11.23".
  • The build type defaults to Release and the macOS deployment target to 11.0.
  • Only cozip_* symbols are exported. libzip, zlib, Karu, curl and OpenSSL stay hidden inside the library, so a host with its own copies does not bind to them.

Each GitHub release carries a libcozip-<version>-<platform> archive with include/cozip.h, include/version.h and the shared library, for Linux x86-64 and aarch64, macOS and Windows x86-64.

Conventions

Every writer function returns a cozip_status_t and fills an optional error. The reader reports errors as heap strings instead, because its messages can name long URLs.

  • Writer functions take a last argument cozip_error_t *err, which may be NULL. On failure it holds the status in err->code and a NUL-terminated UTF-8 message of at most 192 bytes (COZIP_ERROR_MESSAGE_SIZE) in err->message. On success err->code is COZIP_OK and the message is empty.
  • Outputs are unspecified after a failure.
  • Archive names are NUL-terminated ASCII. File system paths are UTF-8; the Windows build converts them to UTF-16.
  • The caller owns every string, entry array and buffer it passes, and they must stay valid for the whole call. libcozip keeps no pointer to them after it returns.
  • cozip_status_string returns the stable name of a status, such as "INVALID_ARGUMENT". It and the other string accessors return static storage, never NULL.

The status names match the errors on the API overview, which gives the cause and the fix of each.

StatusValueReturned by
COZIP_OK0Every call, on success.
COZIP_ERR_INVALID_LFH1The reader. Also cozip_patch_integrity_hash when byte 0 holds no __cozip__ header, and the writer when the file on disk does not match the plan in the post-write check, after removing it.
COZIP_ERR_ARCHIVE_TOO_SMALL2The reader, and cozip_patch_integrity_hash.
COZIP_ERR_INVALID_INDEX3The reader, for a Flat index of the wrong shape, and cozip_verify, for an index with bytes after its last section. Section 9 of the specification has no name for it.
COZIP_ERR_PROFILE4cozip_read_flat_metadata, for any profile but Flat. The specification calls it UNKNOWN_PROFILE.
COZIP_ERR_INVALID_MAGIC5The reader.
COZIP_ERR_UNSUPPORTED_VERSION6The reader, for a version of 0 or above 1.
COZIP_ERR_HASH_MISMATCH7The reader.
COZIP_ERR_TRUNCATED_INDEX8The reader.
COZIP_ERR_DUPLICATE_NAME, COZIP_ERR_INVALID_NAME9, 10cozip_verify, for an indexed name that repeats or breaks the name rules. The writer reports a bad name as COZIP_ERR_INVALID_ARGUMENT.
COZIP_ERR_MISSING_ENTRY11cozip_read_flat_metadata, when the manifest's local header does not match the index.
COZIP_ERR_INVALID_OFFSET12The reader.
COZIP_ERR_INVALID_ZIP_STRUCTURE13The reader.
COZIP_ERR_INVALID_ARGUMENT100The writer, for a bad name, size, source, capacity, path, profile value or profile rule, or a layout libzip cannot record. The reader, for a malformed URI or an hf:// path it cannot map.
COZIP_ERR_BUFFER_TOO_SMALL101cozip_build_index_payload, when out_size is short.
COZIP_ERR_IO102Any call, for an open, stat, read, write, libzip, allocation or cleanup failure. The reader, for a transport failure, a missing local file included.
COZIP_ERR_NOT_FOUND103The reader, for a remote object that does not exist.
COZIP_ERR_UNSUPPORTED104The reader, when the library was built without COZIP_READER or Karu does not read the source.

Entries and sources

The writer works on an array of cozip_entry_t in archive order. The caller fills the input fields and the planner fills the offsets in place.

FieldSet byMeaning
arc_namecallerThe ASCII name in the archive, 1 to 65,535 bytes.
payload_sizecallerThe payload length in bytes. It must match the file or the buffer.
in_indexcallerLists the entry in the byte-0 index, as a priority file.
sourcecallerWhere the payload comes from, a cozip_source_t.
lfh_offsetplannerWhere the entry's local header starts.
lfh_sizeplanner30 bytes plus the name, plus 20 for a ZIP64 extra when the payload is larger than 0xFFFFFFFF bytes.
payload_offsetplannerlfh_offset + lfh_size, the first payload byte from byte 0. This is the offset of a manifest row.

A cozip_source_t has a kind and a union. COZIP_SOURCE_PATH reads a regular file from u.path, and COZIP_SOURCE_BUFFER reads u.buffer.size bytes from u.buffer.data, which must equal payload_size. COZIP_SOURCE_NONE is refused by every write.

The high-level calls use the array as scratch space, so it needs spare slots.

CallArray lengthWhat it changes
cozip_finalizecapacity >= n_entries + 1Fills the offsets. When padding is needed it writes a __cozip_padding__ entry into a spare slot, whose buffer is freed before the call returns. A padding entry placed before a final priority block moves that block one slot to the right.
cozip_plan_flatat least n_users + 1Sets in_index = false on the user entries and turns entries[n_users] into a __metadata__ placeholder of size 0.
cozip_write_flatcapacity >= n_users + 2As cozip_plan_flat, with the real manifest in entries[n_users] and room for padding after it.

Writing

Three calls cover the common cases. cozip_finalize and cozip_write_flat plan, pad, build the index, write under a temporary name, check, patch the hash and rename the finished archive over the output.

FunctionBehaviour
cozip_finalize
(out_path, entries, n_entries, capacity, profile, err)
Writes every entry in array order with the given profile byte and enforces that profile's priority files: exactly __metadata__ for Flat (spec 13.2), and COLLECTION.json with every METADATA/*.parquet as one final block for TACO (spec 14.2). Profile 0 has no such rule, so you choose its priority files with in_index. Profiles 3 to 255 are refused. For Flat archives cozip_write_flat also sets up the manifest slot.
cozip_plan_flat
(entries, n_users, err)
Plans the user files of a Flat archive with a __metadata__ placeholder after them, and fills every payload_offset. Refuses zero-byte files and the names __cozip__, __metadata__ and __cozip_padding__.
cozip_write_flat
(out_path, entries, n_users, capacity, metadata_path, err)
Writes the Flat archive with the Parquet file at metadata_path as __metadata__, byte for byte. It does not open the Parquet, so the rows must already hold the offsets cozip_plan_flat returned.

A Flat archive therefore takes two stages, with the manifest written in between.

cozip_entry_t entries[N_USERS + 2] = {0};
/* fill arc_name, payload_size and a source for each user file */
cozip_error_t err = {0};
if (cozip_plan_flat(entries, N_USERS, &err) != COZIP_OK) { /* report err.message */ }

/* entries[i].payload_offset and payload_size are final. Write metadata.parquet with
   the columns name (string), offset (uint64) and size (uint64), one row per user
   file in array order, plus any columns of your own. */

if (cozip_write_flat("dataset.zip", entries, N_USERS, N_USERS + 2,
                     "metadata.parquet", &err) != COZIP_OK) { /* report err.message */ }

The user offsets do not depend on the size of the manifest, because __metadata__ comes after every user file and a Flat index is always 41 bytes. Leave the source files untouched between the two calls. The second call checks each file's size again, but not its contents.

Primitives

cozip_finalize is built from these functions, which a binding can call on its own to drive a custom pipeline.

FunctionBehaviour
cozip_planValidates the names and fills lfh_offset, lfh_size and payload_offset with checked 64-bit arithmetic. A size of zero is accepted as a planning placeholder. A payload of exactly 0xFFFFFFFF bytes is refused, because libzip cannot write its header.
cozip_index_payload_sizeReturns in *out_size the size of the index for the entries with in_index: 11 bytes plus 18 and the name length per entry. At most UINT32_MAX entries, and the payload stays below 4 GiB.
cozip_build_index_payloadSerializes the index into out in entry order, with the profile byte. Returns COZIP_ERR_BUFFER_TOO_SMALL when out_size is short.
cozip_build_extra_fieldWrites the 12-byte 0xCA0C extra field with the hash set to zero.
cozip_write_archiveWrites __cozip__ and every planned entry as STORE through libzip, then reads the file back and compares it with the plan. The hash stays zero. Refuses the layouts libzip records wrongly: a local header or the Central Directory starting at byte 0xFFFFFFFF, and exactly 65,535 entries.
cozip_patch_integrity_hashComputes FNV-1a 64 over the index and the last 32 KiB and writes it to bytes 43 to 50. The file must start with the 51-byte __cozip__ header for an index of that size, or nothing is written and the call returns COZIP_ERR_INVALID_LFH. Returns COZIP_ERR_ARCHIVE_TOO_SMALL below 32,819 bytes.
cozip_predict_zip32_archive_sizeReturns the size a planned archive will have without ZIP64, or UINT64_MAX on overflow.
cozip_required_padding_payloadReturns the __cozip_padding__ payload size that lifts a predicted size to 32,819 bytes, or 0 when none is needed.
cozip_index_name, cozip_padding_name, cozip_flat_metadata_nameReturn the reserved names "__cozip__", "__cozip_padding__" and "__metadata__", for bindings that do not read macros.

What the writer checks

Everything that can be checked from the inputs is checked before the output path is touched.

  • Names. ASCII, 1 to 65,535 bytes, unique in the archive, relative, with / as the separator and no backslash, drive letter, trailing /, or . or .. component. __cozip__ is reserved. __cozip_padding__ may not be indexed, and cozip_finalize refuses it in any entry of yours.
  • Sizes. No zero-byte payload in a written archive, no 64-bit overflow anywhere in the layout.
  • Sources. A path must be a regular file whose current size equals payload_size, and a buffer must have exactly that size.
  • Output. The output may not be one of the sources, compared by spelling and, when the output exists, by file identity.
  • Layout. libzip 1.11 writes a ZIP32 sentinel without its ZIP64 value for a local header or a Central Directory that starts at byte 0xFFFFFFFF, and for exactly 65,535 entries. cozip_finalize steps around these with the padding entry, placing it before a final indexed entry when that is what moves the Central Directory. A local header at that byte that nothing can move is refused.

cozip_finalize writes beside the output, verifies the headers and ZIP end records, patches the hash, then renames the finished file into place. On failure it removes the temporary file and leaves the existing output untouched. If cleanup fails, it returns COZIP_ERR_IO and reports that an invalid temporary file may remain.

Reading

The reader verifies the access path of an archive: cozip_verify for any profile, and cozip_read_flat_metadata for a Flat archive, whose manifest it then returns. In the status table above, "the reader" is both. The location base turns the source into the GDAL path that a manifest row's location starts with.

FunctionBehaviour
cozip_read_flat_metadata
(source, &data, &size, &error)
Reads and verifies the byte-0 index, requires profile 1 with __metadata__ as the only priority file, checks the manifest's local header against the index, and returns the manifest bytes in *out_data and *out_size.
cozip_verify
(source, &error)
Checks the access path of any profile (spec 8.5 and 7): header, index, indexed names and ranges, hash and end record. Returns COZIP_OK when these checks pass. It reads both ends and any missing index bytes, but does not parse the Central Directory or check payloads.
cozip_location_base
(source, &base, &error)
Returns in *out_base the part of cozip:location after /vsisubfile/{offset}_{size},. It reads nothing, and works in a build without the reader.
cozip_free
(ptr)
Releases memory the reader returned. NULL is ignored.

The reader functions set *out_error to NULL first and, on failure, to a message the caller releases with cozip_free. It is NULL after a failure only when the message itself could not be allocated. Credentials in URL user info and query strings are replaced by <redacted> in messages.

The sources the reader takes, and the base cozip_location_base returns for each, are listed under Sources. A leading ~/ is expanded with HOME, or USERPROFILE on Windows. The requests the reader makes are in How it works.

TACO

cozip_plan_taco, cozip_plan_taco_sized, cozip_write_taco, cozip_path_entry_t and cozip_taco_plan_t are the contract between libcozip and the TACO package, which validates a dataset, writes COLLECTION.json and the METADATA/ Parquet files, and asks libcozip to lay them out as the final priority block of a profile 2 archive. Applications write TACO datasets through TACO. The contract is described in docs/taco-writer-internal.md.

Constants

MacroValue
COZIP_FORMAT_VERSION1, the binary format version written into the index
COZIP_INDEX_OFFSET51, where the index starts
COZIP_HASH_WINDOW_SIZE32768, the hashed suffix
COZIP_MIN_ARCHIVE_SIZE32819
COZIP_INDEX_HEADER_SIZE, COZIP_INDEX_PER_ENTRY_OVERHEAD11 and 18
COZIP_INDEX_NAME, COZIP_PADDING_NAME, COZIP_FLAT_METADATA_NAME"__cozip__", "__cozip_padding__", "__metadata__", with _LEN macros 9, 17 and 12
COZIP_MAGIC"CZIP"
COZIP_EXTRA_HEADER_ID, COZIP_EXTRA_DATA_SIZE, COZIP_EXTRA_FIELD_SIZE0xCA0C, 8 and 12
COZIP_FNV_OFFSET_BASIS, COZIP_FNV_PRIME0xCBF29CE484222325 and 0x100000001B3
COZIP_ERROR_MESSAGE_SIZE192

Threads and bindings

libcozip keeps no state between calls; each read creates its own Karu client. Concurrent writers to one output race, and the last rename wins. The library does not fsync the output.

On 64-bit targets cozip_entry_t is 72 bytes, with source at 24 and lfh_offset at 48, and cozip_error_t is 196 bytes. Enums cross the ABI as int. Python mirrors the structs in the cffi definitions of python/cozip/_core.py, Julia in julia/src/LibCozip.jl and R in its C glue, and nothing checks them against the header, so a change to a struct means changing all three.