英文:
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 <- 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)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论