If you are working in R language, you should probably know that datasets are often stored as zip or tar files, and so R provides two commands to deal with zip files and R files.
How to zip files 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. If you want to archive all the directories and files, then you have to pass all directories and files that you want to include in the archive.
Create a folder inside your project called R
Inside the R folder, create two files called Pro.R and data.R.
So, in the R folder, we will write a program inside Pro.R to zip the data.R file.
Write the following line of code inside the Pro.R.
Go to the terminal and run the Pro.R file.
Rscript Pro.R adding: data.R (stored 0%)
Now, in your current project directory, one file is created called sources.zip file.
How to unzip files in R
To unzip the files in R, use the unzip() method. The unzip() method unpacks all the files and distributes them inside the current working directory.
It will unpack the sources.zip file. If you want to see the contents inside the zip file, use the unzip() function and pass the second argument as a list.
And it will display the zip file contents like this.
Rscript Pro.R Name Length Date 1 data.R 8 2020-10-27 10:36:00
It is providing a list of files that we are examining. In our case, the zip file contains only one file called data.R.
How to tar files in R
The tar files are very similar to zip files. The good thing about working with tar files is that R language comes with an internal implementation of tar. So, if you don’t have tar or zip in your local system pathname still, you can tar or untar the files in R.
To create a tar file, use the tar() function. The tar() function takes one required parameter: the tar filename you want to create. It also accepts the second parameter, but it is optional.
If you don’t provide the second parameter that specifies the files we want to tar, it will tar all the files in the current working directory.
Write the following code inside the Pro.R file.
If you run the above code, then it will create a new file called compress.tar file in your directory. It will tar all the files in your working directory because we did not specify the particular file in the tar() function.
How to untar files in R
To untar files in R, use the untar() function. It will unpack all the files in the current directory.
To see the list of files in the compress.tar file, you need to pass the second parameter list=TRUE.
untar("compress.tar", list = TRUE)
It will list everything in that tar file. In our case, it is a compress.tar file.
 "./.DS_Store" "./Pro.R" "./data.R" "./sources.zip"
In R programming, zip, unzip, tar, and untar are everyday operations we need to perform when working with large datasets.
To zip, unzip, tar, and untar operations, use the following.
- To zip file, use zip().
- To unzip the file, use unzip().
- To tar the file, use tar().
- To untar the file, use untar().
That is it for this tutorial.
Krunal Lathiya is an Information Technology Engineer by education and web developer by profession. He has worked with many back-end platforms, including Node.js, PHP, and Python. In addition, Krunal has excellent knowledge of Data Science and Machine Learning, and he is an expert in R Language. Krunal has written many programming blogs, which showcases his vast expertise in this field.