R
- Package
cozipon r-universe, with binaries for Linux, macOS and Windows, on R 4.3 or newer.
install.packages("cozip", repos = "https://asterisk-labs.r-universe.dev")
R compiles libcozip and its vendored dependencies. Arrow writes manifests and DuckDB decodes them. A source install needs GNU make, a C++23 compiler, pkg-config, libcurl 7.83 or newer and OpenSSL 3.
The exported functions are create and its alias write, stage_metadata, stage_create and read. library(cozip) masks base::write, so call cozip::write, or base::write when you need both. The rules every binding shares are on the API overview.
Writing
create
create(out_path, table, temp_dir = NULL)
Writes a Flat archive from a table, as Write describes. write is the same function. It returns the absolute path of the archive, from normalizePath() after the write, with a leading ~ expanded first.
| out_path | The destination archive. An existing file is replaced. |
|---|---|
| table | An arrow::Table with name, path and your columns. Convert a data frame with arrow::as_arrow_table(df). |
| temp_dir | Where the temporary manifest Parquet goes, created if missing. The default is tempdir(). |
library(cozip)
tbl <- arrow::arrow_table(
name = c("a.txt", "data/b.bin"),
path = c("a.txt", "b.bin"),
split = c("train", "val")
)
create("out.zip", tbl)
create() uses zstd when Arrow provides it and chooses dictionary encoding per column from the first 10,000 rows. name and path may be character or factor columns, with no missing values.
stage_metadata
stage_metadata(table)
Plans the archive without writing anything, the first of the two stages. It returns a list with metadata, the manifest as an arrow::Table, and paths, a data frame of name and path in the same order. table is as for create.
res <- stage_metadata(tbl)
as.data.frame(res$metadata)
name offset size split
1 a.txt 127 96 train
2 data/b.bin 263 40960 val
stage_create
stage_create(out_path, paths, metadata_parquet, validate = TRUE)
Writes the archive around a manifest Parquet you wrote and returns its path, as create does.
| out_path | The destination archive, a single non-empty string. |
|---|---|
| paths | A data frame with character name and path columns and no NA, in the manifest's row order. |
| metadata_parquet | The Parquet file, stored byte for byte as __metadata__. |
| validate | A single TRUE or FALSE. |
res <- stage_metadata(tbl)
arrow::write_parquet(res$metadata, "metadata.parquet", compression = "zstd")
stage_create("out.zip", res$paths, "metadata.parquet")
Reading
read
read(source, columns = NULL, location = TRUE)
Reads the manifest of a Flat archive into a tibble, one row per file. The arguments are described under Read.
| source | A single string, in one of the source forms. |
|---|---|
| columns | NULL, or a character vector. With a vector, the columns are name, offset, size, cozip:location, then the listed ones. An unknown name stops with an error. |
| location | TRUE or FALSE. |
url <- "https://huggingface.co/datasets/asterisk-labs/cozip-api-fixtures/resolve/v0.1.0/data/cities.zip"
manifest <- cozip::read(url, columns = c("label", "continent"))
manifest[manifest$continent == "Europe", c("name", "offset", "size")]
# A tibble: 2 x 3
name offset size
<chr> <dbl> <dbl>
1 cities/paris.geojson 1092 172
2 cities/oslo.geojson 1313 171
offset and size come back as doubles, because DuckDB maps unsigned 64-bit integers to doubles in R. They are exact up to 253 bytes.
read writes the manifest to a tempfile() and decodes it in a new DuckDB database, disconnected before the file is removed. Where the installed duckdb allows it, that database refuses extensions and stays out of ~/.duckdb.
verify
verify(source)
Checks an archive of any profile, as described under Verify. Returns NULL invisibly, or stops with an error such as [cozip:HASH_MISMATCH] cozip integrity hash mismatch: dataset.zip.
Opening the files
terra and sf open cozip:location through GDAL. A local archive also opens with a seek and a read of the byte range.
m <- cozip::read(normalizePath("tiles.zip"))
r <- terra::rast(m[["cozip:location"]][1])
con <- file("tiles.zip", "rb")
seek(con, m$offset[1])
bytes <- readBin(con, "raw", m$size[1])
close(con)
GeoParquet manifests
Store the geometry as WKB in a binary column and attach the geo key to the table. stage_metadata copies the schema metadata, so create writes a GeoParquet manifest.
geo <- '{"version": "1.1.0", "primary_column": "geometry",
"columns": {"geometry": {"encoding": "WKB", "geometry_types": ["Point"]}}}'
tbl <- arrow::arrow_table(
name = names,
path = files,
geometry = arrow::Array$create(unclass(sf::st_as_binary(geometries)), type = arrow::binary())
)
tbl$metadata$geo <- geo
cozip::create("dataset.zip", tbl)
read returns the geometry as a list of raw WKB vectors, which sf turns back into geometries.
m <- cozip::read("dataset.zip")
g <- sf::st_as_sfc(structure(m$geometry, class = "WKB"), crs = "OGC:CRS84")
An sf data frame written with arrow::write_parquet and geoarrow is not a valid manifest. The Parquet has GeoArrow columns but no geo key, and offset and size become 32-bit integers.
Errors
A check made in R stops with a message that starts with cozip: and counts rows from one. A failure inside libcozip carries its status name and counts entries from zero. The API overview lists the causes.
cozip: duplicate name 'a' at row 2
[cozip:INVALID_ARGUMENT] entry 0 ('e') has a zero-byte payload; FLAT user entries must be non-empty
[cozip:HASH_MISMATCH] cozip integrity hash mismatch: bad.zip
Use a UTF-8 locale. Under LC_ALL=C, R turns a non-ASCII name such as niño into the ASCII text ni<c3><b1>o before it reaches C, so it passes the ASCII rule and is stored under that name. In a UTF-8 locale the same name is refused. Sys.getlocale("LC_CTYPE") shows the current one.