R Advanced

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; you need to install one 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:

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.

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:

We will unpack the “sources.zip” file, as shown in the screenshot above.

Add below line of code.

unzip("sources.zip")

It will unpack the sources.zip 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)

It will then display the zip file contents in this format.

     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:

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.

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.

View Comments

  • 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!

Recent Posts

R scale(): Scaling and Centering of Matrix-like Objects

The scale() function in R centers (subtracting the mean) and/or scales (dividing by the standard…

2 weeks ago

file.rename(): Renaming Single and Multiple Files in R

To rename a file in R, you can use the file.rename() function. It renames a…

3 weeks ago

R prop.table() Function

The prop.table() function in R calculates the proportion or relative frequency of values in a…

3 weeks ago

exp() Function: Calculate Exponential of a Number in R

The exp() is a built-in function that calculates the exponential of its input, raising Euler's…

3 weeks ago

R split() Function: Splitting a Data

The split() function divides the input data into groups based on some criteria, typically specified…

1 month ago

colMeans(): Calculating the Mean of Columns in R Data Frame

The colMeans() function in R calculates the arithmetic mean of columns in a numeric matrix,…

1 month ago