Create a writable connection to a file in an archive.
archive_write( archive, file, mode = "w", format = NULL, filter = NULL, options = character() )
An 'archive_write' connection to the file within the archive to be written.
NULL, they will be set automatically based on
the file extension given in
file when writing and automatically detected
Robust automatic format detection
For traditional zip archives
archive_write() creates a connection which
writes the data to the specified file directly. For other archive formats
the file size must be known when the archive is created, so the data is
first written to a scratch file on disk and then added to the archive. This
scratch file is automatically removed when writing is complete.
# Archive format and filters can be set automatically from the file extensions. f1 <- tempfile(fileext = ".tar.gz") write.csv(mtcars, archive_write(f1, "mtcars.csv")) archive(f1)#> # A tibble: 1 × 3 #> path size date #> <chr> <int> <dttm> #> 1 mtcars.csv 1783 2021-10-15 13:25:34unlink(f1) # They can also be specified explicitly f2 <- tempfile() write.csv(mtcars, archive_write(f2, "mtcars.csv", format = "tar", filter = "bzip2")) archive(f2)#> # A tibble: 1 × 3 #> path size date #> <chr> <int> <dttm> #> 1 mtcars.csv 1783 2021-10-15 13:25:34unlink(f2) # You can also pass additional options to control things like compression level f3 <- tempfile(fileext = ".tar.gz") write.csv(mtcars, archive_write(f3, "mtcars.csv", options = "compression-level=2")) archive(f3)#> # A tibble: 1 × 3 #> path size date #> <chr> <int> <dttm> #> 1 mtcars.csv 1783 2021-10-15 13:25:34unlink(f3)