6

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.

Code_Sipra
  • 1,571
  • 4
  • 19
  • 38
  • 1
    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 Nov 18 '17 at 20:33
  • 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 Nov 18 '17 at 20:39
  • Do you have a program on your machine to zip and unzip files? – G5W Nov 18 '17 at 20:42
  • Yes I do. As already mentioned in my post, I have both `WinRAR` and `7-Zip` installed in my computer. – Code_Sipra Nov 18 '17 at 20:43

3 Answers3

12

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.

G5W
  • 36,531
  • 10
  • 47
  • 80
  • Thanks a lot for taking the time to looking into this @GSW. I just have couple of questions. What is `flags = "a -tzip"` doing? And what if the user had `Winzip` or `WinRAR` or any other program installed in their system? How do I change the code accordingly? – Code_Sipra Nov 18 '17 at 21:22
  • I will add response to the answer. – G5W Nov 18 '17 at 21:24
  • Really appreciate it. I will test this suggestion later today and keep you informed if I encounter any more roadblocks. – Code_Sipra Nov 18 '17 at 21:26
  • Also, is there any way where R can automatically check for available zip software to zip files rather than me specifying it? The rscript i am using will bd used by multiple clients. I wouldn't know which software they would have installed in their systems. Hence this question. – Code_Sipra Nov 18 '17 at 21:30
  • Also adding to answer. – G5W Nov 18 '17 at 21:33
  • Thanks again. I will explore your solution and will also try to replace 7Z with other options and see if it works. I will update this thread once I have my inputs. Thanks! – Code_Sipra Nov 18 '17 at 21:36
  • This solution worked perfectly @G5W! Thanks a lot for helping me out. – Code_Sipra Nov 19 '17 at 09:35
6

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)
NelsonGon
  • 13,015
  • 7
  • 27
  • 57
BenoitLondon
  • 845
  • 6
  • 19
  • Thanks, this works @BenoitLondon! I actually am trying to avoid installing additional packages to execute my program for various reasons.For something as powerful as R, its a little surprising R supports file decompression with `unzip()` but does not have support for file compression inbuilt! – Code_Sipra Nov 19 '17 at 06:26
3

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)
JLB
  • 148
  • 2
  • 8