Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/r-lib/archive

R bindings to libarchive, supporting a large variety of archive formats
https://github.com/r-lib/archive

compression connections libarchive r

Last synced: about 2 months ago
JSON representation

R bindings to libarchive, supporting a large variety of archive formats

Awesome Lists containing this project

README

        

---
output: github_document
---

```{r, echo = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "README-"
)
dir <- tempfile()
dir.create(dir)
knitr::opts_knit$set(root.dir = dir)
options(tibble.print_min = 4L)
library(archive)
```
# archive

[![R-CMD-check](https://github.com/r-lib/archive/workflows/R-CMD-check/badge.svg)](https://github.com/r-lib/archive/actions)
[![Coverage Status](https://img.shields.io/codecov/c/github/r-lib/archive/main)](https://app.codecov.io/github/r-lib/archive?branch=main)
[![CRAN status](https://www.r-pkg.org/badges/version/archive)](https://CRAN.R-project.org/package=archive)

R bindings to libarchive .
Supports many archives formats, including tar, ZIP, 7-zip, RAR, CAB.
Also supports many filters such as gzip, bzip2, compress, lzma, xz and uuencoded files, among others.

archive provides interfaces to read and write connections into archives, as
well as efficiently reading and writing archives directly to disk.

## Installation

You can install archive from CRAN with:

``` r
# install.packages("archive")
```

## Example

### Single file archives

Use `archive_read()` and `archive_write()` to read and write single files to an archive.
These files return connections, which can be passed to any R interface which can take a connection.
Most base R file system functions use connections, as well as some packages like [readr](https://readr.tidyverse.org/).

```{r}
library(readr) # read_csv(), write_csv(), cols()

# Write a single dataset to zip
write_csv(mtcars, archive_write("mtcars.zip", "mtcars.csv"))

# Read the data back, by default the first file is read from the archive.
read_csv(archive_read("mtcars.zip"), col_types = cols())

# Also supports things like archiving and compression together
# Write a single dataset to (gzip compressed) tar
write_csv(mtcars, archive_write("mtcars.tar.gz", "mtcars.csv", options = "compression-level=9"))

# Read the data back
read_csv(archive_read("mtcars.tar.gz"), col_types = cols())

# Archive file sizes
file.size(c("mtcars.zip", "mtcars.tar.gz"))
```

### Multi file archives

`archive_write_files()` is used to create a new archive from multiple files on disk.

```{r}
# Write a few files to the temp directory
write_csv(iris, "iris.csv")
write_csv(mtcars, "mtcars.csv")
write_csv(airquality, "airquality.csv")

# Add them to a new archive
archive_write_files("data.tar.xz", c("iris.csv", "mtcars.csv", "airquality.csv"))

# View archive contents
a <- archive("data.tar.xz")
a

# By default `archive_read()` will read the first file from a multi-file archive.
read_csv(archive_read("data.tar.xz"), col_types = cols())

# Use a number to read a different file
read_csv(archive_read("data.tar.xz", file = 2), col_types = cols())

# Or a filename to read a specific file
read_csv(archive_read("data.tar.xz", file = "mtcars.csv"), col_types = cols())
```

### Regular files (with compression)
`file_write()` returns a connection to filtered by one or more compressions or
encodings. `file_read()` reads a compressed file, automatically detecting the
compression used.

```{r}
# Write bzip2, uuencoded data
write_csv(mtcars, file_write("mtcars.bz2", filter = c("uuencode", "bzip2")))

# Read it back, the formats are automatically detected
read_csv(file_read("mtcars.bz2"), col_types = cols())
```