2016-11-21 17 views
1

在R存贮器,我怎么能解压一个gzcon内存解压gzcon作为R

背景:

我需要在内存中的.tar.gz文件执行某些操作,这是非常重要的文件不会被写入磁盘。该文件最初与curl_fetch_memory一起下载并产生与以下示例数据类似的对象。

如果我再在对象上做untar(gzcon(rawConnection(res$content)))它会在tar文件到磁盘,这是不可取的写入数据。

实例数据(含.tar.gz名为test.txt与内容hello world!文件):

res <- structure(list(url = "sftp://[email protected]:/test.tar.gz", 
status_code = 0L, headers = raw(0), modified = structure(1479765215L, class = c("POSIXct", 
"POSIXt")), times = structure(c(0, 0, 0, 0, 0.312, 0.312), .Names = c("redirect", 
"namelookup", "connect", "pretransfer", "starttransfer", 
"total")), content = as.raw(c(0x1f, 0x8b, 0x08, 0x00, 0xdf, 
0x6c, 0x33, 0x58, 0x00, 0x03, 0xed, 0xce, 0x3d, 0x0a, 0xc2, 
0x50, 0x10, 0xc4, 0xf1, 0xad, 0x73, 0x8a, 0xe7, 0x05, 0x64, 
0x37, 0x79, 0xd9, 0x9c, 0x47, 0x30, 0x90, 0xe2, 0x49, 0x20, 
0x59, 0x3f, 0x8e, 0xaf, 0x22, 0x42, 0x2a, 0x4d, 0x13, 0x44, 
0xf8, 0xff, 0x9a, 0x29, 0x66, 0x8a, 0x89, 0x7e, 0x8e, 0x7d, 
0xdc, 0x42, 0x36, 0xa4, 0x0f, 0xee, 0xf9, 0x99, 0xd6, 0xb5, 
0xba, 0xcc, 0x17, 0x73, 0xb1, 0x46, 0x2d, 0xbb, 0x7b, 0xa3, 
0xad, 0xa8, 0x69, 0xae, 0x3b, 0x49, 0xba, 0xe5, 0xa9, 0xb7, 
0xf3, 0x1c, 0x87, 0x29, 0x25, 0xb9, 0x9c, 0x3e, 0xef, 0xbe, 
0xf5, 0x7f, 0x6a, 0xe8, 0x4b, 0x19, 0xd3, 0x75, 0x9c, 0xca, 
0x71, 0x57, 0x55, 0xbf, 0x7e, 0x03, 0x00, 0x00, 0x00, 0x00, 
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x58, 
0xeb, 0x0e, 0x02, 0xc4, 0x36, 0xca, 0x00, 0x28, 0x00, 0x00 
))), .Names = c("url", "status_code", "headers", "modified", 
"times", "content")) 
+0

如果你知道如何从一个bash控制台做到这一点(我没有),那么你应该能够读取'代码解压“,并找出如何破解替代品。 –

回答

1

原来解析tarfiles并不难做到。的utils:::untar2核心圈是一个内存untar工具的实现一个很好的起点。基本上,tar文件具有以下结构:

+-----------------+-----------+-----------------+-----------+-~ 
| 512-byte header | file data | 512-byte header | file data | 
+-----------------+-----------+-----------------+-----------+-~ 

焦油头格式更详细的描述GNU manual for tar,并且由一些文件属性,幻数和校验和的。

的伪代码在内存中解压缩工具很简单:

repeat { 
    parse tar header with file attributes 
    for each block in file { 
    write block to raw connection 
    } 
    write raw connection and file attributes to file object 
    add file to list 
} 
return list of files 
1

是否在解压extras = "O"标志让你更接近你需要什么?

例如,如果我犯了一个文件

echo "hello world" > afile 
tar -cvf afile.tar.gz afile 

然后我能够将其与

untar('afile.tar.gz', compressed = 'gzip', extras = "-O") 

我使用GNU焦油1.29读取到stdout(其打印到R)

+0

不幸的是,它没有。看起来该标志对内部tar版本没有任何作用,并且不会显示到标准输出。 – ruser9575ba6f

+0

我在这里迟到了,但如果您仍然需要,请查看以上修改 –