Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating zip file from folders in R

Tags:

r

zip

Try to create a zip file from one folder using R.

It mentioned "Rcompression" package here: Creating zip file from folders

But I didn't find where I can download this package for Windows system.

Any suggestions? or other functions to create a zip file?

like image 505
Jian Zhang Avatar asked May 15 '14 02:05

Jian Zhang


People also ask

How do I zip a folder in R?

To zip files in R, use the zip() function. The zipped file is stored inside the current directory unless a different path is specified in the zip() function argument. The zip() method creates a new ZIP archive, and it overwrites the output file if it exists.

How do I convert multiple folders into a zip file?

Right-click on the file or folder. To place multiple files into a zip folder, select all of the files while hitting the Ctrl button. Then, right-click on one of the files, move your cursor over the “Send to” option and select “Compressed (zipped) folder”.

How do I zip an entire folder?

To zip (compress) a file or folder Locate the file or folder that you want to zip. Press and hold (or right-click) the file or folder, select (or point to) Send to, and then select Compressed (zipped) folder. A new zipped folder with the same name is created in the same location.

How do I zip an R project?

To zip your R Project Right-click > Send to > Compressed (zipped) folder Now you should have a zipped version of your R Project to submit through Blackboard.


4 Answers

You can create a zip file with the function zip from utils package quite easily. Say you have a directory testDir and you wish to zip a file (or multiple files) inside the directory,

dir('testDir')
# [1] "cats.csv" "test.csv" "txt.txt" 
zip(zipfile = 'testZip', files = 'testDir/test.csv')
# adding: testDir/test.csv (deflated 68%)

The zipped file is saved in the current working directory, unless a different path is specified in the zipfile argument. We can see its size relative to the original unzipped file with

file.info(c('testZip.zip', 'testDir/test.csv'))['size']
#                  size
# testZip.zip       805
# testDir/test.csv 1493

You can zip the whole directory of files (if no sub-folders) with

files2zip <- dir('testDir', full.names = TRUE)
zip(zipfile = 'testZip', files = files2zip)
# updating: testDir/test.csv (deflated 68%)
# updating: testDir/cats.csv (deflated 27%)
# updating: testDir/txt.txt (stored 0%)

And unzip it to view the files,

unzip('testZip.zip', list = TRUE)
#               Name Length                Date
# 1 testDir/test.csv   1493 2014-05-14 20:54:00
# 2 testDir/cats.csv    116 2014-05-14 20:54:00
# 3  testDir/txt.txt     32 2014-05-08 09:37:00

Note: From ?zip, regarding the zip argument.

On Windows, the default relies on a zip program (for example that from Rtools) being in the path.

like image 159
Rich Scriven Avatar answered Oct 16 '22 10:10

Rich Scriven


For avoiding (a) an issue with relative paths (i.e., the zip file itself containing a folder structure with the full folder path to be zipped) and (b) for loops (well, style), you may use

my_wd<-getwd() # save your current working directory path
dest_path<-"C:/.../folder_with_files_to_be_zipped" 
setwd(dest_path)
files<-list.files(dest_path)
named<-paste0(files,".zip")
mapply(zip,zipfile=named,files=files)
setwd(my_wd) # reset working directory path

Unlike R´s build-in unzip function, zip requires a zip-program like 7-zip (Windows) or the one being part of Rtools to be present in your system path.

like image 23
martin Avatar answered Oct 16 '22 09:10

martin


For people still looking for this: there is now a "zip" package that does not depend on external executables.

like image 4
Juan Jesus Avatar answered Oct 16 '22 09:10

Juan Jesus


You can install from the omegahat repos:

install.packages('Rcompression', repos = "http://www.omegahat.org/R", type = "source")

for windows you will need to jump through hoops installing zlib and bzip2 and linking appropriately.

utils::zip can be used in some cases. There are a number of issues with it. One case is that the maximum length of the string that you can use at the command prompt is 8191 characters (2047 characters on some versions) for windows. If you are zipping a directory with alot of characters for the names of directories/files this will cause issues. For example if you zip your firefox profile directory. Also I found the zip command needed to be issued relative the directory I was zipping to use relative directory names. Rcompression has a altNames argument which handles this. That being said I have always had problems getting Rcompression to run on windows.

like image 1
jdharrison Avatar answered Oct 16 '22 11:10

jdharrison