[ RDocumentation ](/)

Moon  [ ](https://github.com/datacamp/rdocumentation-2.0) [ Learn R
](https://www.datacamp.com/learn/r)

Search all packages and functions

[ base  (version  3.6.2  ) ](/packages/base/versions/3.6.2)

#  gzcon: (De)compress I/O Through Connections

##  Description

` gzcon ` provides a modified connection that wraps an existing connection,
and decompresses reads or compresses writes through that connection. Standard
` gzip ` headers are assumed.

##  Usage

    
    
    gzcon(con, level = 6, allowNonCompressed = TRUE, text = FALSE)

##  Arguments

con

a connection.

level

integer between 0 and 9, the compression level when writing.

allowNonCompressed

logical. When reading, should non-compressed input be allowed?

text

logical. Should the connection be text-oriented? This is distinct from the
mode of the connection (must always be binary). If ` TRUE ` , ` [ pushBack
](/link/pushBack?package=base&version=3.6.2) ` works on the connection,
otherwise ` [ readBin ](/link/readBin?package=base&version=3.6.2) ` and
friends apply.

##  Value

An object inheriting from class ` "connection" ` . This is the same connection
_number_ as supplied, but with a modified internal structure. It has binary
mode.

##  Details

If ` con ` is open then the modified connection is opened. Closing the wrapper
connection will also close the underlying connection.

Reading from a connection which does not supply a ` gzip ` magic header is
equivalent to reading from the original connection if ` allowNonCompressed `
is true, otherwise an error.

Compressed output will contain embedded NUL bytes, and so ` con ` is not
permitted to be a ` [ textConnection
](/link/textConnection?package=base&version=3.6.2) ` opened with ` open = "w"
` . Use a writable ` [ rawConnection
](/link/rawConnection?package=base&version=3.6.2) ` to compress data into a
variable.

The original connection becomes unusable: any object pointing to it will now
refer to the modified connection. For this reason, the new connection needs to
be closed explicitly.

##  See Also

` [ gzfile ](/link/gzfile?package=base&version=3.6.2) `

##  Examples

[ Run this code
](https://app.datacamp.com/workspace/preview?_tag=rdocs&rdocsPath=packages%2Fbase%2Fversions%2F3.6.2%2Ftopics%2Fgzcon&utm_source=r-docs&utm_medium=docs&utm_term=gzcon&utm_content=run_example_in_workspace)

    
    
    # NOT RUN {
    ## Uncompress a data file from a URL
    z <- gzcon(url("http://www.stats.ox.ac.uk/pub/datasets/csb/ch12.dat.gz"))
    # read.table can only read from a text-mode connection.
    raw <- textConnection(readLines(z))
    close(z)
    dat <- read.table(raw)
    close(raw)
    dat[1:4, ]
    # }
    # NOT RUN {
    ## gzfile and gzcon can inter-work.
    ## Of course here one would use gzfile, but file() can be replaced by
    ## any other connection generator.
    zzfil <- tempfile(fileext = ".gz")
    zz <- gzfile(zzfil, "w")
    cat("TITLE extra line", "2 3 5 7", "", "11 13 17", file = zz, sep = "\n")
    close(zz)
    readLines(zz <- gzcon(file(zzfil, "rb")))
    close(zz)
    unlink(zzfil)
    
    # }
    # NOT RUN {
    <!-- % Cygwin seems to require a different name -->
    # }
    # NOT RUN {
    zzfil2 <- tempfile(fileext = ".gz")
    zz <- gzcon(file(zzfil2, "wb"))
    cat("TITLE extra line", "2 3 5 7", "", "11 13 17", file = zz, sep = "\n")
    close(zz)
    readLines(zz <- gzfile(zzfil2))
    close(zz)
    unlink(zzfil2)
    # }
    

Run the code above in your browser using  [ DataCamp Workspace
](https://app.datacamp.com/workspace/preview?_tag=rdocs&rdocsPath=packages%2Fbase%2Fversions%2F3.6.2%2Ftopics%2Fgzcon&utm_source=r-docs&utm_medium=docs&utm_term=gzcon&utm_content=run_example_in_workspace)

Powered by  [ DataCamp ](https://www.datacamp.com/)