How to ungz various .gz files kept in different folders?

262 Views Asked by At

I have various .gz files kept in different folders. A sample of the order of files is mentioned below. I want to ungz them and want them to be in the same folder as before (e.g. .gz files of Folder 1a should be remain in Folder 1a upon ungz-ing) and delete the .gz files later.

Folder1
 Folder 1a > many .gz files
 Folder 2a > many .gz files
 Folder 3a > many .gz files
 .
 .
 .

I know how to unzip .zip files but that does not seem to work for .gz files. I want to do it in R, can anyone please help. I am working in Windows.

2

There are 2 best solutions below

0
Mikko Marttila On BEST ANSWER

First write a function to decompress a single file. The gzfile() function can be used to open a connection that decompresses a gzip file.

decompress <- function(file, dest = sub("\\.gz$", "", file)) {
  # Set up source and destination connections
  src <- gzfile(file, "rb")
  on.exit(close(src), add = TRUE)
  dst <- file(dest, "wb")
  on.exit(close(dst), add = TRUE)
  
  # Copy decompressed contents from source to destination
  BATCH_SIZE <- 10 * 1024^2
  repeat {
    bytes <- readBin(src, raw(), BATCH_SIZE)
    if (length(bytes) != 0) {
      writeBin(bytes, dst)
    } else {
      break
    }
  }
  
  invisible(dest)
}

Then, apply it to the files you want to decompress:

files <- list.files(pattern = "*.gz", full.names = TRUE, recursive = TRUE)
for (file in files) {
  decompress(file)
}
1
alt164 On

If you are in Linux, you can use find command in the top folder to find all the .gz files, and them redirect all the results to gzip command (with -d option indicating that you want to decompress) as follows:

find . -name "*.gz" -exec gzip -d {} \;

In Windows try:

gzip -r -d Folder1

To do it in R, use system function to execute one of the two above commands.