【问题标题】:How to unzip .rar with password?如何用密码解压.rar?
【发布时间】:2021-04-23 06:50:00
【问题描述】:

我有一个包含 csv 文件的 .rar 存档。 .rar 有密码,我想用 R Studio 读取它(csv 是 .rar 中唯一的文件)。

我尝试使用以下代码:

library(Hmisc)

getZip("datos/diarios.rar", password = "israel")

但是 R 返回了这个:

A connection with                                                                                    
description "C:\\WINDOWS\\system32\\cmd.exe /c unzip -p -P israel datos/diarios.rar"
class       "pipe"                                                                  
mode        "r"                                                                     
text        "text"                                                                  
opened      "closed"                                                                
can read    "yes"                                                                   
can write   "yes" 

我该如何解决这个问题?


当我在上面运行read.csv 时,它不起作用。看:

read.csv(gzfile("datos/diarios.zip", open = ""), header = T) 

read.table 中的错误(文件 = 文件,标题 = 标题,sep = sep,quote = 引用,:比列名更多的列另外:警告消息: 1: 在 read.table(file = file, header = header, sep = sep, quote = 引用,:第 1 行似乎包含嵌入的空值 2:在 read.table(file = file, header = header, sep = sep, quote = quote, : 第 2 行似乎包含嵌入的空值

【问题讨论】:

  • 从帮助页面看来,例如,在此使用 read.csv
  • 它不起作用。看:在 read.csv(gzfile("datos/diarios.zip", open = ""), header = T) 错误 read.table(file = file, header = header, sep = sep, quote = quote, :比列名更多的列另外:警告消息:1:在read.table(文件=文件,标题=标题,sep=sep,quote=quote,:第1行似乎包含嵌入的空值2:在read.table( file = file, header = header, sep = sep, quote = quote, : 第 2 行似乎包含嵌入的空值
  • 现在看起来更像是数据和 read.csv 的问题,而不是打开压缩文件。不幸的是,如果没有数据,这有点难以解决。你能在文本编辑器中打开它看看吗?

标签: r unzip password-encryption unrar


【解决方案1】:

假设有一个 test.csv 包含存储在存档 test.rar 中的数据框。我们可以使用7zip的命令行模式使用system()命令打开它。这实际上是一个从 R 执行的 .bat 文件,我们只需将 paste 与命令放在一起。

z7 <- shQuote("C:/Program Files/7-Zip/7z.exe")  ## path to yoour 7zip.exe
arch <- "V:/test.rar"  ## path to archive
temp <- tempdir()  ## creating a temporary directory
pw <- "1234"  ## provide password

现在使用paste,命令可能如下所示

(cmd <- paste(z7, "x", arch, "-aot", paste0("-o", temp), paste0("-p", pw)))
# [1] "\"C:/Program Files/7-Zip/7z.exe\" x V:/test.rar -aot -oC:\\Users\\jay\\AppData\\Local\\Temp\\Rtmp67ZAPK -p1234"

(x: 提取-aot: 为现有文件添加后缀以免被覆盖, -o: 输出目录, -p: 提供密码)

我们将使用system() 执行它

system(cmd)
dat <- read.csv(paste0(temp, "/test.csv"))
dat
#   X X1 X2 X3 X4
# 1 1  1  4  7 10
# 2 2  2  5  8 11
# 3 3  3  6  9 12

unlink(temp, recursive=TRUE)  ## unlink the tempdir to clean up

【讨论】:

    猜你喜欢
    • 2012-04-15
    • 2014-08-29
    • 2010-10-07
    • 2021-01-15
    • 1970-01-01
    • 2020-09-03
    • 1970-01-01
    • 2011-03-21
    • 2020-09-18
    相关资源
    最近更新 更多