How to Use zip(), unzip() and tar(), untar() Functions in R

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.

zip("", "data.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 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 file. If you want to see the contents inside the zip file, use the unzip() function and pass the second argument as a list.

unzip("", list=TRUE)

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.

[1] "./.DS_Store" "./Pro.R" "./data.R" "./"


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.

  1. To zip file, use zip().
  2. To unzip the file, use unzip().
  3. To tar the file, use tar().
  4. To untar the file, use untar().

That is it for this tutorial.

See also

R Vector

R Subsetting

R Vector Math

1 thought on “How to Use zip(), unzip() and tar(), untar() Functions in R”

  1. Mу brother sᥙgցested I might like this blog.
    He was totally right. This post truly made my day.
    You can not imаgine јսst how much time
    I had spent for thіs information! Thanks!


Leave a Comment