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:
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.
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 you can see in the above screenshot. 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)
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.
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.
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.
Krunal Lathiya is a seasoned Computer Science expert with over eight years in the tech industry. He boasts deep knowledge in Data Science and Machine Learning. Versed in Python, JavaScript, PHP, R, and Golang. Skilled in frameworks like Angular and React and platforms such as Node.js. His expertise spans both front-end and back-end development. His proficiency in the Python language stands as a testament to his versatility and commitment to the craft.
Whether you are reading or writing files via programs in the file system, it is…
When it comes to checking the data type of a variable, it depends on what…
The grepl() function (stands for "grep logical") in R searches for patterns within each element…
When working with file systems, checking the directory or file existence is always better before…
To create a grouped boxplot in R, we can use the ggplot2 library's aes() and…
For data exploration, quickly inspecting the data, verifying the structure, or debugging large datasets, we…
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!