如何用密码解压.rar?

问题描述

我有一个包含 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(文件=文件标题=标题,sep=sep,报价=报价,: 第 2 行似乎包含嵌入的空值

解决方法

假设有 test.csv 包含存储在存档 test.rar 中的数据框。我们可以使用 system() 命令使用 7zip 的命令行模式打开它。这实际上是一个从 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