Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
736 views
in Technique[技术] by (71.8m points)

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

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!!! :)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...