zip(), unzip() and tar(), untar() Functions in R

The zip() function creates a new zip archive file. You must ensure that the zip tool is available in your system to work with zip() functions.

For example, Windows and Mac systems have built-in zip tools, but Linux does not have a tool; you need to install it separately. Then, you can use this zip() function to create an archive.

Create a folder inside your project called “Rdir”.

Inside the Rdir folder, create two files called Pro.R and data.R.

Before zipping a file, our Rdir directory looks like this:

Current directory before zipping the files

Write the following line of code inside the Pro.R.

zip("sources.zip", "data.R")

Go to the terminal and run the Pro.R file.

Rscript Pro.R
adding: data.R (stored 0%)

In your current project directory, a file called sources.zip is created.

Created zip files in R

unzip() function

To unzip the files, you can use the “unzip()” method. It unpacks and distributes all the files inside the current working directory.

Before unzipping a file, our current working directory looks like this:

Before unzipping a file

We will unpack the “sources.zip” file, as you can see in the above screenshot. Add below line of code.

unzip("sources.zip")

It will unpack the sources.zip file.

After unzipping a file

To see the contents inside the zip file, use the unzip() function and pass the second argument as a list.

unzip("sources.zip", list=TRUE)

And it will display the zip file contents like this.

     Name  Length      Date
1    data.R  8    2020-10-27 10:36:00

It provides a list of files that we are examining. In our case, the zip file contains only one file called data.R.

tar() function

The tar() and untar() functions also handle archives, which are common in Unix-based systems.

The tar combines multiple files into a single archive file, which is often compressed with gzip or bzip2, hence the .tar.gz or .tar.bz2 extensions.

To create a tar file, use the tar() function. It takes one required parameter: the tar filename you want to create, and an optional second parameter.

Before creating a tar file, our current working directory, “Rdir”, looks like this:

Before creating a tar file

Write the below single line of code inside the Pro.R file.

tar("compress.tar")

Running the above code will create a new 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.

After creating a tar file

untar() function

To untar files (unarchive files), use the untar() function. It will unpack all the files in the current directory.

untar("compress.tar")

To see the list of files in the compress.tar file, you must 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" "./sources.zip"

That’s it.

1 thought on “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!

    Reply

Leave a Comment