Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to programmatically extract / unzip a .7z (7-zip) file with R

Tags:

r

zip

unzip

rar

7zip

I'm trying to automate the extraction of a number of files compressed with 7-zip. I need to automate this process, because a) there are many years of data I'd like to unlock and b) I'd like to share my code with others and prevent them from repeating the process by hand.

I have both WinRAR and 7-zip installed on my computer, and I can individually open these files easily with either program.

I've looked around at the unzip untar and unz commands, but I don't believe any of them do what I need.

I don't know anything about compression, but if it makes any difference: each of these files only contains one file and it's just a text file.

I would strongly prefer a solution that does not require the user to install additional software (like WinRAR or 7-Zip) and execute a command with shell, although I acknowledge this task might be impossible with just R and CRAN packages. I actually believe running shell.exec on these files with additional parameters might work on computers with WinRAR installed, but again, I'd like to avoid that installation if possible. :)

Running the code below will load the files I am trying to extract -- the .7z files in files.data are what needs to be unlocked.

# create a temporary file and temporary directory, download the file, extract the file to the temporary directory
tf <- tempfile() ; td <- tempdir()
file.path <- "ftp://ftp.ibge.gov.br/Orcamentos_Familiares/Pesquisa_de_Orcamentos_Familiares_2008_2009/Microdados/Dados.zip"
download.file( file.path , tf , mode = "wb" )
files.data <- unzip( tf , exdir = td )

# how do i unzip ANY of these .7z files?
files.data

Thanks!!! :)

like image 504
Anthony Damico Avatar asked Apr 19 '13 02:04

Anthony Damico


People also ask

How do I unzip a 7z file?

First, download and install the 7-Zip tool on your computer. Next, move to the file you need to open and right-click on it. Doing this gives you a submenu where you can select the 7-zip option and navigate to the 'Open Archive' option. You will get a display screen showcasing the archive contents.

How do I use 7-Zip to extract files from the command line?

If you want extract files with full paths, you must use x (Extract with full paths) command. 7-Zip will prompt the user before overwriting existing files unless the user specifies the -y (Assume Yes on all queries) switch. If the user gives a no answer, 7-Zip will prompt for the file to be extracted to a new filename.


2 Answers

This can be done with the archive package.

library(archive)
tf <- tempfile() ; td <- tempdir()
file.path <- "ftp://ftp.ibge.gov.br/Orcamentos_Familiares/Pesquisa_de_Orcamentos_Familiares_2008_2009/Microdados/Dados.zip"
download.file( file.path , tf , mode = "wb" )
archive(tf)

See https://github.com/jimhester/archive

like image 126
jsta Avatar answered Oct 17 '22 22:10

jsta


If you have 7z executable in your path, you can simple use system command

system('7z e -o <output_dir> <archive_name>')

like image 43
CHP Avatar answered Oct 17 '22 22:10

CHP