Search code examples
rzipunzip7zip

Opening a .7z file in R


can someone point me to a way of unzipping and opening .7z files through R?

Here is an example of a file that I want to download:

 utils::download.file(
          url = "ftp://ftp.mtps.gov.br/pdet/microdados/RAIS/AC2008.7z")

All files that I want are .txt once unzipped.

If I try unzip("./AC2008.7z"), I get the message:

In unzip(fileName, exdir = mainDir, subDir) : error 1 in extracting from zip file

Any help?

I don't necessarily need to unzip the files - If somehow R opened the underlying .txt directly, it would be okay.

The solution should be such that it is implementable within a function in a package.


Solution

  • The archive package will open 7zip format.

    You will need to install the devtools package to install it.

    devtools::install_github("jimhester/archive")
    

    I'm unable to access your example file on the FTP server. Assuming that it is a multi-file archive of .txt files, you would access it like this:

    a <- archive("AC2008.7z")
    

    Assuming it contained a file named x.txt with columns delimited by white space, you might do something like:

    library(readr)
    x <- read_table(archive_read(a, "x.txt"))