- Notifications
You must be signed in to change notification settings - Fork19
R bindings to libarchive, supporting a large variety of archive formats
License
Unknown, MIT licenses found
Licenses found
r-lib/archive
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
R bindings to libarchivehttp://www.libarchive.org. Supports manyarchives formats, including tar, ZIP, 7-zip, RAR, CAB. Also supportsmany filters such as gzip, bzip2, compress, lzma, xz and uuencodedfiles, among others.
archive provides interfaces to read and write connections into archives,as well as efficiently reading and writing archives directly to disk.
You can install archive from CRAN with:
# install.packages("archive")Usearchive_read() andarchive_write() to read and write singlefiles to an archive. These files return connections, which can be passedto any R interface which can take a connection. Most base R file systemfunctions use connections, as well as some packages likereadr.
library(readr)# read_csv(), write_csv(), cols()# Write a single dataset to zipwrite_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())#> # A tibble: 32 × 11#> mpg cyl disp hp drat wt qsec vs am gear carb#> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>#> 1 21 6 160 110 3.9 2.62 16.5 0 1 4 4#> 2 21 6 160 110 3.9 2.88 17.0 0 1 4 4#> 3 22.8 4 108 93 3.85 2.32 18.6 1 1 4 1#> 4 21.4 6 258 110 3.08 3.22 19.4 1 0 3 1#> # ℹ 28 more rows# Also supports things like archiving and compression together# Write a single dataset to (gzip compressed) tarwrite_csv(mtcars, archive_write("mtcars.tar.gz","mtcars.csv",options="compression-level=9"))# Read the data backread_csv(archive_read("mtcars.tar.gz"),col_types= cols())#> # A tibble: 32 × 11#> mpg cyl disp hp drat wt qsec vs am gear carb#> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>#> 1 21 6 160 110 3.9 2.62 16.5 0 1 4 4#> 2 21 6 160 110 3.9 2.88 17.0 0 1 4 4#> 3 22.8 4 108 93 3.85 2.32 18.6 1 1 4 1#> 4 21.4 6 258 110 3.08 3.22 19.4 1 0 3 1#> # ℹ 28 more rows# Archive file sizesfile.size(c("mtcars.zip","mtcars.tar.gz"))#> [1] 742 808
archive_write_files() is used to create a new archive from multiplefiles on disk.
# Write a few files to the temp directorywrite_csv(iris,"iris.csv")write_csv(mtcars,"mtcars.csv")write_csv(airquality,"airquality.csv")# Add them to a new archivearchive_write_files("data.tar.xz", c("iris.csv","mtcars.csv","airquality.csv"))# View archive contentsa<- archive("data.tar.xz")a#> # A tibble: 3 × 3#> path size date#> <chr> <int> <dttm>#> 1 iris.csv 3716 2023-12-11 12:18:04#> 2 mtcars.csv 1281 2023-12-11 12:18:04#> 3 airquality.csv 2890 2023-12-11 12:18:04# By default `archive_read()` will read the first file from a multi-file archive.read_csv(archive_read("data.tar.xz"),col_types= cols())#> # A tibble: 150 × 5#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species#> <dbl> <dbl> <dbl> <dbl> <chr>#> 1 5.1 3.5 1.4 0.2 setosa#> 2 4.9 3 1.4 0.2 setosa#> 3 4.7 3.2 1.3 0.2 setosa#> 4 4.6 3.1 1.5 0.2 setosa#> # ℹ 146 more rows# Use a number to read a different fileread_csv(archive_read("data.tar.xz",file=2),col_types= cols())#> # A tibble: 32 × 11#> mpg cyl disp hp drat wt qsec vs am gear carb#> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>#> 1 21 6 160 110 3.9 2.62 16.5 0 1 4 4#> 2 21 6 160 110 3.9 2.88 17.0 0 1 4 4#> 3 22.8 4 108 93 3.85 2.32 18.6 1 1 4 1#> 4 21.4 6 258 110 3.08 3.22 19.4 1 0 3 1#> # ℹ 28 more rows# Or a filename to read a specific fileread_csv(archive_read("data.tar.xz",file="mtcars.csv"),col_types= cols())#> # A tibble: 32 × 11#> mpg cyl disp hp drat wt qsec vs am gear carb#> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>#> 1 21 6 160 110 3.9 2.62 16.5 0 1 4 4#> 2 21 6 160 110 3.9 2.88 17.0 0 1 4 4#> 3 22.8 4 108 93 3.85 2.32 18.6 1 1 4 1#> 4 21.4 6 258 110 3.08 3.22 19.4 1 0 3 1#> # ℹ 28 more rows
file_write() returns a connection to filtered by one or morecompressions or encodings.file_read() reads a compressed file,automatically detecting the compression used.
# Write bzip2, uuencoded datawrite_csv(mtcars, file_write("mtcars.bz2",filter= c("uuencode","bzip2")))# Read it back, the formats are automatically detectedread_csv(file_read("mtcars.bz2"),col_types= cols())#> # A tibble: 32 × 11#> mpg cyl disp hp drat wt qsec vs am gear carb#> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>#> 1 21 6 160 110 3.9 2.62 16.5 0 1 4 4#> 2 21 6 160 110 3.9 2.88 17.0 0 1 4 4#> 3 22.8 4 108 93 3.85 2.32 18.6 1 1 4 1#> 4 21.4 6 258 110 3.08 3.22 19.4 1 0 3 1#> # ℹ 28 more rows
About
R bindings to libarchive, supporting a large variety of archive formats
Topics
Resources
License
Unknown, MIT licenses found
Licenses found
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors12
Uh oh!
There was an error while loading.Please reload this page.