1

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.

Community
  • 1
  • 1
galra
  • 353
  • 2
  • 12
  • This doesn't seem to be possible in a reliable way without unzipping the stream, q.v. [here](http://stackoverflow.com/questions/9715046/find-the-size-of-the-file-inside-a-gzip-file). – Tim Biegeleisen Oct 08 '16 at 11:16

1 Answers1

2

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)
  close(f)

  return(ret)

}
hrbrmstr
  • 77,368
  • 11
  • 139
  • 205