Skip to contents

Decompress the full contents of a file (or connection) into another file (or connection). This can be useful if you need to access a compressed file in some external software that doesn't support decompression, or for compatibility with a function that doesn't accept connections and doesn't handle decompression.

Usage

decompress(file, dest = sub("\\.(gz|bz2|xz)$", "", file), force = FALSE)

Arguments

file

Path to a compressed file, or a connection. See Details.

dest

Path to the destination file, or a connection. Must be given explicitly if file is not a path that exists.

force

Set to TRUE to overwrite file with dest, if applicable.

Value

The destination dest, invisibly.

Details

If file is a connection, it will be wrapped with gzcon(), automatically opened if necessary, and is always closed (and destroyed) afterwards.

See the underlying gzfile() for an authoritative list of supported compression types. At time of writing at least gzip, bzip2, xz and lzma are supported.

The batch size used for I/O operations can be configured by setting the mire.decompress.batch_size option (in bytes). The default batch size is 50 MiB.

See also

gzfile() and gzcon() to read compressed files directly.

Examples

decompress(url("https://www.stats.ox.ac.uk/pub/datasets/csb/ch12.dat.gz"), "ch12.dat")
#> The source `file` connection will be closed and destroyed.
#> Decompressing `file` into ch12.dat
head(read.table("ch12.dat"))
#>     V1 V2 V3 V4 V5 V6 V7
#> 1   37 86  1  0  0  2  0
#> 2   61 77  1  0  0  4  0
#> 3 1084 75  1  0  0  3  1
#> 4 1092 77  1  0  1  2  1
#> 5   23 86  1  0  0  4  0
#> 6 1091 71  1  1  0  3  1
unlink("ch12.dat") # Tidy up