2015-09-06 52 views
0

我將我的名爲Result的字節數組壓縮爲zipped.dat文件。如何再次解壓我的字節數組?

Using zip As Ionic.Zip.ZipFile = New Ionic.Zip.ZipFile 
    zip.AddEntry("Result", Result) 
    zip.Save("zipped.dat") 
End Using 

我想打開該文件在運行時,得到zipped.dat內容,終於找回我的Result陣列。

'Extract contain to Result array again 
Dim Result() as byte = ... 

回答

1

您可以解壓縮這樣的文件,使用「zipped.dat」爲zipPath和extractPath目標文件夾:

Imports System.IO.Compression 
... 
ZipFile.ExtractToDirectory(zipPath, extractPath) 

您可以提取單個文件是這樣的(未經測試):

Using archive As ZipArchive = ZipFile.OpenRead(zipPath) 
    entry = getentry(filename)   
    if entry isnot nothing then entry.ExtractToFile(Path.Combine(extractPath, entry.FullName)) 
End Using 

爲了將其提取到一個流,使用:

Using archive As ZipArchive = ZipFile.OpenRead("c:\tmp\track.zip"), 
    entry = archive.GetEntry("track.gdb") 
    If entry IsNot Nothing Then 
    Using zStream As Stream = entry.Open() 
     Dim b As Byte 
     b = zStream.ReadByte() 
    End Using 
    End If 
End Using 

隨着流,你可以將它讀入內存。

+0

感謝的答案,但我需要運行時字節數組。我不想提取內容到一個文件.. –

+0

我添加這問題的答案。 – xpda

0

我稍微修改了xpda的答案。這現在正在爲我工​​作。由於xpda

Dim data As New MemoryStream() 
    Dim archive As ZipArchive = ZipFile.OpenRead("zipped.dat") 
    Dim entry = archive.GetEntry("Result") 
    entry.Open.CopyTo(data) 
    Dim Result() As Byte = data.ToArray() 
相關問題