我有一个 370MB 的 zip 文件,内容是一个 4.2GB 的 csv 文件。
我做了:
unzip("year2015.zip", exdir = "csv_folder")
我收到了这条消息:
1: In unzip("year2015.zip", exdir = "csv_folder") :
possible truncation of >= 4GB file
你以前有过这种经历吗?你是怎么解决的?
最佳答案
我同意@Sixiang.Hu 的回答,R 的 unzip() 不能可靠地处理大于 4GB 的文件。
到 你是怎么解决的? :我已经尝试了一些不同的技巧,根据我的经验,使用 R 内置函数的任何结果(几乎)总是在实际结束之前错误地识别文件结束(EOF)标记文件。
我在每晚处理的一组文件中处理这个问题,为了始终如一地以自动化方式处理它,我将下面的函数写入 包装 UNIX 解压。 这基本上就是您对 system(unzip()) 所做的事情,但在其行为上为您提供了更大的灵活性,并允许您更系统地检查错误。
decompress_file <- function(directory, file, .file_cache = FALSE) {
if (.file_cache == TRUE) {
print("decompression skipped")
} else {
# Set working directory for decompression
# simplifies unzip directory location behavior
wd <- getwd()
setwd(directory)
# Run decompression
decompression <-
system2("unzip",
args = c("-o", # include override flag
file),
stdout = TRUE)
# uncomment to delete archive once decompressed
# file.remove(file)
# Reset working directory
setwd(wd); rm(wd)
# Test for success criteria
# change the search depending on
# your implementation
if (grepl("Warning message", tail(decompression, 1))) {
print(decompression)
}
}
}
笔记:
该函数做了一些我喜欢和推荐的事情:
system2
超过系统因为 the documentation说“system2 是一个比系统更便携、更灵活的界面”directory
和 file
参数,并将工作目录移动到 directory
争论;根据您的系统,解压缩(或您选择的解压缩工具)对于在工作目录外解压缩文件非常挑剔.file_cache
允许您跳过解压缩的参数if
+ grepl
最后检查在标准输出中查找警告,如果找到表达式 关于R:可能截断 >= 4GB 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42740206/