【问题标题】:Can´t unzip file in R无法在 R 中解压缩文件
【发布时间】:2017-08-03 19:26:54
【问题描述】:

我从某个 URL 下载了一些数据,但我无法解压缩下载的任何文件,我不明白为什么。下载数据的代码如下。

library(downloader)
path <- getwd()
for(i in 1:15){
fileName <- sprintf("%02d",i)
if (!file.exists(paste0(fileName,".zip"))) {
urlFile = paste0("http://www.censo2017.cl/wp-content/uploads/2016/12/R", 
fileName,".zip")
download(urlFile, dest = paste0("./R",fileName, ".zip"), mode ="wb")
 }
}

然后我有 15 个 zip 文件,名为: R01.zip R02.zip ...等等,但是当我使用

unzip(R01.zip)

或尝试解压缩任何其他文件,我收到以下错误Warning message: In unzip("R01.zip") : error 1 in extracting from zip file

我已经阅读过相关的 StackOverflow 帖子,例如 this onethis one,但在我的情况下没有解决方案。

我可以手动解压缩文件,但我想直接在 RStudio 中解压。有什么想法吗?

PD:.zip 文件中包含地理数据,即 .dbf、.prj、.shp 文件等。

谢谢!

【问题讨论】:

  • download 函数从何而来?它不是基本的 R 函数,并且您没有指定任何额外的包。请尽可能剪切和粘贴工作代码。
  • 您确定它们是有效的 ZIP 文件吗?你能在 R 之外提取它们吗?
  • 这是我的代码,从包“downloader”下载。不过谢谢你的提醒。
  • 你是如何手动解压的?我怀疑你的解压程序支持 RAR 文件。
  • 我使用的是 7-zip,所以是的,它支持 WinRAR

标签: r unzip


【解决方案1】:

它们不是 zip 文件,它们是 RAR 档案:

$ unrar v 01.zip

UNRAR 5.00 beta 8 freeware      Copyright (c) 1993-2013 Alexander Roshal

Archive: 01.zip
Details: RAR 4

 Attributes      Size    Packed Ratio   Date   Time   Checksum  Name
----------- ---------  -------- ----- -------- -----  --------  ----
    ..A....      1213       240  19%  23-11-16 16:12  C6C40C6D  R01/Comuna.dbf
    ..A....       151       138  91%  23-11-16 16:12  A3C83CE4  R01/Comuna.prj
    ..A....       212       165  77%  23-11-16 16:12  01752C2A  R01/Comuna.sbn
    ..A....       132       101  76%  23-11-16 16:12  C4CA93A2  R01/Comuna.sbx

不知道有没有R函数可以提取RAR压缩包。

它们可能不应该有 .zip 文件扩展名,而是 .rar。我在命令行上使用unrar 提取了上述内容。

【讨论】:

    【解决方案2】:

    好的,所以基于这个post,我能够解决一个解决方案。

    由于这些文件实际上不是 .zip 文件,而且 7-zip 支持手动提取文件,因此我寻找一种在 R 中调用 7-zip 的方法。我刚刚发布的链接显示了如何做到这一点。

    我修改了我的代码,现在文件会自动下载并解压缩。

    # load neccesary packages
    library(downloader)
    library(installr)
    install.7zip(page_with_download_url = "http://www.7-zip.org/download.html")
    
    # download data and unzipped data
    path <- getwd()
    for(i in 1:15){   # the files correspond to administrative regions of Chile
                      # there are fifteen of them and they are ordered.
    fileName <- sprintf("%02d",i) # adding leading zeros to the index if 
                                  # the index number is of one digit
    if (!file.exists(paste0("R",fileName,".zip"))) { # download only 
                                                     # if file is not already
                                                     # downloaded
    urlFile = paste0("http://www.censo2017.cl/wp-content/uploads/2016/12/R", 
              fileName,".zip") # specifying url address
    download(urlFile, dest = paste0("./R",fileName, ".zip"), mode ="wb")
    } # download file
    if (!file.exists(paste0("R",fileName))){ # if file is not already unzipped,
                                             # unzip it
    z7path = shQuote('C:\\Program Files (x86)\\7-Zip\\7z')
    file = paste0(getwd(), "/", "R", fileName, ".zip")
    cmd = paste0(z7path, ' e ', file, ' -y -o', paste0(getwd(),"/R", fileName), 
          '/')
    shell(cmd)
     }
    }
    

    如果有人能告诉我这个解决方案是否也适合你,那就太棒了!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多