如何解压存放在不同文件夹中的各种 .gz 文件?

huangapple go评论69阅读模式
英文:

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

问题

I can help with the translation:

我有各种.gz文件存放在不同的文件夹中。下面提到了文件的顺序示例。我想要将它们解压缩,并希望它们保留在之前的同一文件夹中(例如,Folder 1a的.gz文件应该保持在Folder 1a中),然后删除.gz文件。

Folder1
Folder 1a > 许多.gz文件
Folder 2a > 许多.gz文件
Folder 3a > 许多.gz文件
.
.
.

我知道如何解压缩.zip文件,但似乎不适用于.gz文件。我想在R中完成这个任务,请问有人能帮助吗?我在Windows上工作。

英文:

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.

答案1

得分: 0

如果你在Linux上,可以在顶层文件夹使用find命令查找所有的.gz文件,然后将所有结果重定向到gzip命令(使用-d选项表示你要解压缩),如下所示:

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

在Windows上尝试:

gzip -r -d Folder1

在R中,使用system函数执行上述两个命令之一。

英文:

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.

答案2

得分: 0

请为单个文件编写一个解压缩函数。gzfile() 函数可用于打开一个连接,用于解压缩 gzip 文件。

decompress <- function(file, dest = sub("\\.gz$", "", file)) {
  # 设置源和目标连接
  src <- gzfile(file, "rb")
  on.exit(close(src), add = TRUE)
  dst <- file(dest, "wb")
  on.exit(close(dst), add = TRUE)
  
  # 从源复制解压缩内容到目标
  BATCH_SIZE <- 10 * 1024^2
  repeat {
    bytes <- readBin(src, raw(), BATCH_SIZE)
    if (length(bytes) != 0) {
      writeBin(bytes, dst)
    } else {
      break
    }
  }
  
  invisible(dest)
}

然后,将其应用于要解压缩的文件:

files <- list.files(pattern = "*.gz", full.names = TRUE, recursive = TRUE)
for (file in files) {
  decompress(file)
}
英文:

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 &lt;- function(file, dest = sub(&quot;\\.gz$&quot;, &quot;&quot;, file)) {
  # Set up source and destination connections
  src &lt;- gzfile(file, &quot;rb&quot;)
  on.exit(close(src), add = TRUE)
  dst &lt;- file(dest, &quot;wb&quot;)
  on.exit(close(dst), add = TRUE)
  
  # Copy decompressed contents from source to destination
  BATCH_SIZE &lt;- 10 * 1024^2
  repeat {
    bytes &lt;- 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 &lt;- list.files(pattern = &quot;*.gz&quot;, full.names = TRUE, recursive = TRUE)
for (file in files) {
  decompress(file)
}

huangapple
  • 本文由 发表于 2023年5月13日 21:45:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76243040.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定