Compress or decompress using gzip, bzip2, or xz compression.

compress(
  file,
  ext = c("gz", "bz2", "xz", "zip"),
  remove = getOption("acid.compress.remove", default = TRUE),
  overwrite = getOption("acid.overwrite", default = TRUE)
)

decompress(
  file,
  remove = getOption("acid.compress.remove", default = TRUE),
  overwrite = getOption("acid.overwrite", default = TRUE)
)

Arguments

file

character(1). File path.

ext

character(1). Compression file format extension. Uses match.arg() internally and defaults to the first argument in the character vector.

Supported formats:

  • gz: gzip compression; calls gzfile() internally.

  • bz: bzip2 (lzma) compression; calls bzfile() internally.

  • xz: xz compression; calls xzfile() internally.

  • zip: zip compression; calls zip() or unzip() internally.

remove

logical(1). Remove the input file once the output file is fully created and the connection is closed.

overwrite

logical(1). Overwrite existing file on disk.

Note

For ZIP files, refer to zip and unzip in the utils package.

Updated 2020-01-19.

See also

Examples

## Create an example text file. text <- c("hello", "world") file <- "test.txt" writeLines(text = text, con = file) readLines(con = file)
#> [1] "hello" "world"
## Apply gzip compression. gzfile <- compress(file = file, ext = "gz", remove = TRUE, overwrite = TRUE) gzfile
#> [1] "/Users/mike/git/monorepo/r-packages/acidbase/docs/reference/test.txt.gz" #> attr(,"bytes") #> [1] 12
readLines(con = gzfile)
#> [1] "hello" "world"
## When `remove = TRUE`, the original input file will be removed. file.exists(file)
#> [1] FALSE
## Decompress the gzipped file. file <- decompress(file = gzfile, remove = TRUE, overwrite = TRUE) file
#> [1] "/Users/mike/git/monorepo/r-packages/acidbase/docs/reference/test.txt" #> attr(,"bytes") #> [1] 12
unlink(file)