galra galra - 5 months ago 28
R Question

How to determine gzfile size OR reading it raw in R?

Following this answer for reading a whole file, I need to determine the uncompressed file size of a gzfile.
It's saved at the last 4 bytes of the gzfile, but I couldn't find how to open the file without r will wrap it with an uncompressing layer, so I have no access to the raw gz file. I haven't found a method that provides this information as well.


Provided you are sure this is a complete gzip'd file with a single stream and <2GB uncompressed:

gz_size <- function(path) {

  path <- path.expand(path)

  f <- file(path, open="rb", raw=TRUE)
  seek(f, -4L, "end", "read")
  ret <- readBin(f, "integer", 1)