6
votes

I am trying to zip multiple CSV files in R. Below is the code for reference.

# 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)

I am getting the below warning message. The Zip file has not been created.

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

I even tried this command to read CSV file names: Zip_Files <- list.files(path = getwd(), pattern = ".csv$", full.names = TRUE) But I still get the warning message shown above. I already have WinRAR and 7-Zip installed in my computer. I am using the latest version of R (3.4.2 64 Bit) along with latest version of RStudio. I have a Windows 7 x64 OS. Any help on this would be really appreciated.

3
If you look at the help file ?zip it says "On Windows, the default relies on a zip program (for example that from Rtools) being in the path." It seems likely that you do not have a program named "zip" in your path. Try opening a command prompt and type "zip -h". Do you get "not recognized as an internal or external command" ? - G5W
That's right @G5W. I just opened a command prompt and typed zip-h as suggested. I am getting 'zip' is not recognized as an internal or external command, operable program or batch file. - Code_Sipra
Do you have a program on your machine to zip and unzip files? - G5W
Yes I do. As already mentioned in my post, I have both WinRAR and 7-Zip installed in my computer. - Code_Sipra

3 Answers

11
votes

The problem is that R's zip does not actually have code to zip (compress) files. It calls an external program to do that. You must let zip know what program to use and what arguments to give that program. You should be able to make this work like this:

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

If your path to 7Z, the command line version of 7Zip, is different, please adjust to match your installation.

Some explanation:

The zip = "C:\\Program Files\\7-Zip\\7Z" argument tells R what program to use perform the compression. In this case, I pointed it at 7Z, the command line version of 7Zip, but you can use other command line programs by changing this to point to a different program.

The flags = " a -tzip" argument depends on the program that you are using. I set this up for 7Z. Reading the 7Z documentation you will see that you need to give 7Z a command (the "a") and flags (the "-tzip"). The "a" command means add these files to the archive. The -tzip flag means make it a zip archive instead of a 7Z archive. With different programs, you would need to read the documentation and construct appropriate flags for that program.

Update: If you need to have this functionality on diverse customer machines, you should consider looking into the zip package It does not require any external program and provides similar functionality.

5
votes

you could install the zip package and use it in your code . That way, anybody using your code would be able to zip the files without installing or searching to configure and this works for any OS.

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)
3
votes

The zip function in the zip library has been deprecated. If you want to zip multiple files with an absolute path, you'll need to use zipr. The below worked for me.

# 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)