Learn R Programming

R.utils (version 2.0.2)

compressFile: Compressing and decompressing files

Description

Compressing and decompressing files such as gzip:ed and bzip2:ed files.

Usage

## S3 method for class 'default':
compressFile(filename, destname=sprintf("\%s.\%s", filename, ext), ext, FUN,
  temporary=FALSE, skip=FALSE, overwrite=FALSE, remove=TRUE, BFR.SIZE=1e+07, ...)
 ## S3 method for class 'default':
decompressFile(filename, destname=gsub(sprintf("[.]\%s$", ext), "", filename,
  ignore.case = TRUE), ext, FUN, temporary=FALSE, skip=FALSE, overwrite=FALSE,
  remove=TRUE, BFR.SIZE=1e+07, ...)
 ## S3 method for class 'default':
isCompressedFile(filename, method=c("extension", "content"), ext, fileClass, ...)
 ## S3 method for class 'default':
bzip2(filename, ..., ext="bz2", FUN=bzfile)
 ## S3 method for class 'default':
bunzip2(filename, ..., ext="bz2", FUN=bzfile)
 ## S3 method for class 'default':
gzip(filename, ..., ext="gz", FUN=gzfile)
 ## S3 method for class 'default':
gunzip(filename, ..., ext="gz", FUN=gzfile)

Arguments

filename
Pathname of input file.
destname
Pathname of output file.
temporary
If TRUE, the output file is created in a temporary directory.
skip
If TRUE and the output file already exists, the output file is returned as is.
overwrite
If TRUE and the output file already exists, the file is silently overwritting, otherwise an exception is thrown (unless skip is TRU
remove
If TRUE, the input file is removed afterward, otherwise not.
BFR.SIZE
The number of bytes read in each chunk.
...
Passed to the underlying function or alternatively not used.
method
A character string specifying how to infer whether a file is compressed or not.
ext, fileClass, FUN
(internal) Filename extension, file class, and a connection function used to read from/write to file.

Value

  • Returns the pathname of the output file. The number of bytes processed is returned as an attribute.

    isCompressedFile() etc. return a logical.

Details

Internally bzfile() and gzfile() (see connections) are used to read (write) files. If the process is interrupted before completed, the partially written output file is automatically removed.

Examples

Run this code
## bzip2
  cat(file="foo.txt", "Hello world!")
  print(isBzipped("foo.txt"))
  print(isBzipped("foo.txt.bz2"))

  bzip2("foo.txt")
  print(file.info("foo.txt.bz2"))
  print(isBzipped("foo.txt"))
  print(isBzipped("foo.txt.bz2"))

  bunzip2("foo.txt.bz2")
  print(file.info("foo.txt"))

  ## gzip
  cat(file="foo.txt", "Hello world!")
  print(isGzipped("foo.txt"))
  print(isGzipped("foo.txt.gz"))

  gzip("foo.txt")
  print(file.info("foo.txt.gz"))
  print(isGzipped("foo.txt"))
  print(isGzipped("foo.txt.gz"))

  gunzip("foo.txt.gz")
  print(file.info("foo.txt"))

  ## Cleanup
  file.remove("foo.txt")

Run the code above in your browser using DataLab