2015-02-06 103 views

回答

2

的.NET Framework 4.5已经ZipFile类可为你做这个。此代码应该让你开始:

Dim zipPath As String = "Sample.zip" 
Using archive = ZipFile.Open(zipPath, ZipArchiveMode.Read) 
    Dim entry = archive.GetEntry("MyFile.pdf") 
    Using reader As New BinaryReader(entry.Open()) 
    System.IO.File.WriteAllBytes("MyFile.pdf", ReadAllBytes(reader)) 
    End Using 
End Using 

ReadAllBytes()是提取从二进制流中的所有字节的一个辅助方法:

Public Shared Function ReadAllBytes(reader As BinaryReader) As Byte() 
    Const bufferSize As Integer = 4096 
    Using ms As New MemoryStream() 
     Dim buffer(bufferSize) As Byte 
     Dim count As Integer 
     Do 
      count = reader.Read(buffer, 0, buffer.Length) 
      If count > 0 Then ms.Write(buffer, 0, count) 
     Loop While count <> 0 

     Return ms.ToArray() 
    End Using 
End Function 

确保您使用.NET Framework 4.5或以上,你已包括对System.IO.CompressionSystem.IO.Compression.FileSystem的引用。

0

尝试用这个代码的DotNetZip

Using zip As ZipFile = ZipFile.Read(ExistingZipFile) 
    Dim e As ZipEntry = zip("DocumentToFind.txt") 
    e.Extract(OutputStream) 
End Using 

的帮助,否则,你可以以这种方式使用ZipArchiveClass

Using zip As ZipArchive = ZipFile.Open(zipfile, ZipArchiveMode.Read) 
Dim file = zip.Entries.Where(Function(x) x.Name = "fileToFind") 
If file IsNot Nothing Then 
    file.ExtractToFile("yourFile") 

End If 

末使用

0

这将允许您通过线

Dim zipPath As String = "ZIP FILE LOCATION" 
     Using zipStream = New FileStream(last_pafx23_open, FileMode.Open) 
      Using archive = New ZipArchive(zipStream, ZipArchiveMode.Read) 
       For Each ent In archive.Entries 
        MsgBox(ent.ToString) 
        Using stream = ent.Open() 
         Using reader = New StreamReader(stream) 
          While Not reader.EndOfStream 
           MsgBox(reader.ReadLine) 
          End While 
         End Using 
        End Using 
       Next 
      End Using 
     End Using 
-1

读从一个zip线txt文件跳过BinaryReader在W/ReadAllBytes()辅助函数,使用ExtractToFile()代替:

Imports System.IO.Compression 

Using archive = ZipFile.Open("Sample.zip", ZipArchiveMode.Read) 
    Dim entry = archive.GetEntry("MyFile.pdf") 
    If entry IsNot Nothing then entry.ExtractToFile("MyFile.pdf") 
End Using 

仍当然需要参考System.IO.CompressionSystem.IO.Compression.FileSystem

+0

我不能评论顶部的答案,我的答案在这里是一个简化版本。尽管如此,当我希望从zip压缩文件中提取项目而不必首先将其写入磁盘时,我确实在该答案中称赞了帮助函数。 – 2016-12-16 22:22:12

+0

不编译 – stigzler 2017-08-19 08:26:02