gzcon                  package:base                  R Documentation

(_D_e)_c_o_m_p_r_e_s_s _I/_O _T_h_r_o_u_g_h _C_o_n_n_e_c_t_i_o_n_s

_D_e_s_c_r_i_p_t_i_o_n:

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

_U_s_a_g_e:

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

_A_r_g_u_m_e_n_t_s:

     con: a connection.

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

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

_D_e_t_a_i_l_s:

     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.

     The original connection becomes unusable: any object pointing to
     it will now refer to the modified connection.

     When the connection is opened for reading, the input is expected
     to start with the 'gzip' magic header.  If it does not and if
     'allowNonCompressed = TRUE' (the default) the input is read as-is.

_V_a_l_u_e:

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

_S_e_e _A_l_s_o:

     'gzfile'

_E_x_a_m_p_l_e_s:

     ## 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, ]


     ## gzfile and gzcon can inter-work.
     ## Of course here one would used gzfile, but file() can be replaced by
     ## any other connection generator.
     zz <- gzfile("ex.gz", "w")
     cat("TITLE extra line", "2 3 5 7", "", "11 13 17", file = zz, sep = "\n")
     close(zz)
     readLines(zz <- gzcon(file("ex.gz", "rb")))
     close(zz)
     unlink("ex.gz")

     zz <- gzcon(file("ex2.gz", "wb"))
     cat("TITLE extra line", "2 3 5 7", "", "11 13 17", file = zz, sep = "\n")
     close(zz)
     readLines(zz <- gzfile("ex2.gz"))
     close(zz)
     unlink("ex2.gz")

