2016-11-21 55 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

我在這裏遲到了,但如果您仍然需要,請查看以上修改 –