【问题标题】:How to zip multiple CSV files in R?如何在 R 中压缩多个 CSV 文件?
【发布时间】:2018-05-02 09:55:06
【问题描述】:

我正在尝试在 R 中压缩多个 CSV 文件。下面是供参考的代码。

# Create two dataframes using inbuilt datasets for reproducible code
df1 <- head(mtcars)
df2 <- head(iris)

# Write the files as CSV into working directory
write.csv(df1, file = "Test_File1.csv", row.names = FALSE, quote = FALSE)
write.csv(df2, file = "Test_File2.csv", row.names = FALSE, quote = FALSE)

# Read the 2 CSV file names from working directory
Zip_Files <- list.files(path = getwd(), pattern = ".csv$")

# Zip the files and place the zipped file in working directory
zip(zipfile = "TestZip", files = Zip_Files)

我收到以下警告消息。 Zip 文件尚未创建。

Warning message:
running command '"zip" -r9X "TestZip" "Test_File1.csv" "Test_File2.csv" ' had status 127

我什至尝试了这个命令来读取 CSV 文件名:Zip_Files &lt;- list.files(path = getwd(), pattern = ".csv$", full.names = TRUE) 但我仍然收到上面显示的警告消息。我的电脑中已经安装了WinRAR7-Zip。我正在使用最新版本的 R(3.4.2 64 位)以及最新版本的 RStudio。我有一个 Windows 7 x64 操作系统。对此的任何帮助将不胜感激。

【问题讨论】:

  • 如果您查看帮助文件?zip,它会显示“在 Windows 上,默认依赖于路径中的 zip 程序(例如来自 Rtools 的程序)。”您的路径中似乎没有名为“zip”的程序。尝试打开命令提示符并键入“zip -h”。您是否收到“不被识别为内部或外部命令”?
  • 没错@G5W。我刚刚打开命令提示符并按照建议输入zip-h。我收到'zip' is not recognized as an internal or external command, operable program or batch file.
  • 你的机器上有压缩和解压文件的程序吗?
  • 是的,我愿意。正如我的帖子中已经提到的,我的电脑中同时安装了WinRAR7-Zip

标签: r csv dataframe zip zipfile


【解决方案1】:

问题在于 R 的 zip 实际上没有压缩(压缩)文件的代码。它调用一个外部程序来做到这一点。您必须让zip 知道要使用什么程序以及给该程序提供什么参数。您应该能够像这样进行这项工作:

zip(zipfile = "TestZip", files = Zip_Files, flags = " a -tzip",
    zip = "C:\\Program Files\\7-Zip\\7Z")

如果您的 7Z 路径(7Zip 的命令行版本)不同,请调整以匹配您的安装。

一些解释:

zip = "C:\\Program Files\\7-Zip\\7Z" 参数告诉 R 使用什么程序来执行压缩。在这种情况下,我将其指向 7Z,即 7Zip 的命令行版本,但您可以通过将其更改为指向不同的程序来使用其他命令行程序。

flags = " a -tzip" 参数取决于您使用的程序。我为 7Z 设置了这个。阅读7Z documentation 你会看到你需要给7Z 一个命令(“a”)和标志(“-tzip”)。 “a”命令意味着将这些文件添加到存档中。 -tzip 标志意味着将其设为 zip 存档而不是 7Z 存档。对于不同的程序,您需要阅读文档并为该程序构建适当的标志。

更新:如果您需要在不同客户的机器上拥有此功能,您应该考虑查看zip package 它不需要任何外部程序并提供类似的功能。

【讨论】:

  • 非常感谢您花时间研究这个@GSW。我只有几个问题。 flags = "a -tzip" 在做什么?如果用户在他们的系统中安装了WinzipWinRAR 或任何其他程序怎么办?如何相应地更改代码?
  • 我会添加对答案的回复。
  • 真的很感激。我会在今天晚些时候测试这个建议,如果遇到更多障碍,我会及时通知您。
  • 另外,有什么方法可以让 R 自动检查可用的压缩软件来压缩文件,而不是我指定它?我正在使用的 rscript 将被多个客户端使用。我不知道他们会在他们的系统中安装哪些软件。因此提出了这个问题。
  • 也添加到答案中。
【解决方案2】:

您可以安装zip 包并在您的代码中使用它。这样,任何使用您的代码的人都可以在不安装或搜索配置的情况下压缩文件,这适用于任何操作系统。

library(zip)

# Create two dataframes using inbuilt datasets for reproducible code
df1 <- head(mtcars)
df2 <- head(iris)

# Write the files as CSV into working directory
write.csv(df1, file = "Test_File1.csv", row.names = FALSE, quote = FALSE)
write.csv(df2, file = "Test_File2.csv", row.names = FALSE, quote = FALSE)

# Read the 2 CSV file names from working directory
Zip_Files <- list.files(path = getwd(), pattern = ".csv$")

# Zip the files and place the zipped file in working directory
zip::zip(zipfile = "TestZip", files = Zip_Files)

【讨论】:

  • 谢谢,这工作@BenoitLondon!实际上,出于各种原因,我试图避免安装额外的包来执行我的程序。对于像 R 这样强大的东西,有点令人惊讶的是 R 支持使用 unzip() 进行文件解压缩,但不支持内置文件压缩!
【解决方案3】:

zip 库中的 zip 函数已被弃用。如果要使用绝对路径压缩多个文件,则需要使用 zipr。以下对我有用。

# Install the zip package and call it
install.packages("zip")
library(zip)

# Create two dataframes using prebuilt datasets for reproducible code
df1 <- head(mtcars)
df2 <- head(iris)

# Write the files as CSV into working directory
write.csv(df1, file = "\\path\\to\\your_working_directory\\Test_File1.csv", row.names = FALSE, quote = FALSE)
write.csv(df2, file = "\\path\\to\\your_working_directory\\Test_File2.csv", row.names = FALSE, quote = FALSE)

# Read the 2 CSV file names from working directory
Zip_Files <- list.files(path = "\\path\\to\\your_working_directory\\", pattern = ".csv$", full.names=TRUE)

# Zip the files and place the zipped file in working directory
zip::zipr(zipfile = "\\path\\to\\your_working_directory\\Test.Zip", files = Zip_Files)

【讨论】:

    猜你喜欢
    • 2021-07-04
    • 1970-01-01
    • 2016-06-09
    • 1970-01-01
    • 2021-10-14
    • 2016-11-12
    • 1970-01-01
    • 2019-11-09
    • 1970-01-01
    相关资源
    最近更新 更多