如何用密码解压.rar?

How to unzip .rar with password?

我有一个包含 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) 

Error in read.table(file = file, header = header, sep = sep, quote = quote, : more columns than column names In addition: Warning messages: 1: In read.table(file = file, header = header, sep = sep, quote = quote, : line 1 appears to contain embedded nulls 2: In read.table(file = file, header = header, sep = sep, quote = quote, : line 2 appears to contain embedded nulls

假设 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