如何以编程方式使用R提取或解压缩.7z(7-zip)文件

时间:2020-12-04 23:58:31

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.

我正在尝试自动提取大量使用7-zip压缩的文件。我需要自动化这个过程,因为a)我想要解锁多年的数据和b)我想与其他人分享我的代码,并防止他们手动重复这个过程。

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

我的计算机上安装了WinRAR和7-zip,我可以使用任一程序轻松打开这些文件。

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

我已经查看了unzip untar和unz命令,但我不相信他们中的任何一个都能满足我的需求。

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

我非常喜欢不需要用户安装其他软件(如WinRAR或7-Zip)并使用shell执行命令的解决方案,尽管我承认只有R和CRAN软件包可能无法执行此任务。我实际上相信在这些带有附加参数的文件上运行shell.exec可能适用于安装了WinRAR的计算机,但同样,我想尽可能避免安装。 :)

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.

运行下面的代码将加载我试图提取的文件 - files.data中的.7z文件是需要解锁的。

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

谢谢!!! :)

2 个解决方案

#1


12  

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

如果路径中有7z可执行文件,则可以使用system命令

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

system('7z e -o ')

#2


10  

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

请参阅https://github.com/jimhester/archive

#1


12  

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

如果路径中有7z可执行文件,则可以使用system命令

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

system('7z e -o ')

#2


10  

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

请参阅https://github.com/jimhester/archive