在不创建文件夹的情况下解压缩文件

时间:2015-09-02 15:55:23

标签: vb.net zipfile

我正在尝试解压缩.zip文件。这一切都很好,但它总是创建一个名为.zip文件的文件夹。

如何在不创建新文件夹的情况下获取文件?

Imports System.IO.Compression

Private Sub XYform_Load(sender As Object, e As EventArgs) Handles Me.Load 
    Try
        If (Not System.IO.Directory.Exists(System.IO.Path.GetTempPath & "\XML")) Then
            System.IO.Directory.CreateDirectory(System.IO.Path.GetTempPath & "\XML")
        End If
    Catch
    End Try

    Try
        ZipFile.ExtractToDirectory("D:\Test\Test data.zip", System.IO.Path.GetTempPath & "\XML")
    Catch
        'Allready Exists
    End Try
End Sub

如果我这样做,它将始终在“XML”文件夹中创建一个“Test Data”文件夹。

1 个答案:

答案 0 :(得分:1)

如果要操作文件,则需要单独使用ZipArchiveEntries。

以下是一个例子:

Dim zipPath As String = "c:\example\start.zip" 
Dim extractPath As String = "c:\example\extract" 

Using archive As ZipArchive = ZipFile.OpenRead(zipPath)
    For Each entry As ZipArchiveEntry In archive.Entries
        entry.ExtractToFile(Path.Combine(extractPath, entry.FullName))
    Next 
End Using 

如果你只是想要提取到另一个目录,你可以使用:

Dim zipPath As String = "c:\users\exampleuser\end.zip" 
Dim extractPath As String = "c:\users\exampleuser\extract" 

Using archive As ZipArchive = ZipFile.Open(zipPath, ZipArchiveMode.Update)
    archive.ExtractToDirectory(extractPath)
End Using 

Source

编辑:

如果您不想保留结构,请尝试以下方法:

Dim zipPath As String = "c:\example\start.zip" 
Dim extractPath As String = "c:\example\extract" 

Dim zipPath As String = "c:\example\start.zip" 
Dim extractPath As String = "c:\example\extract"

Using archive As ZipArchive = ZipFile.OpenRead(zipPath)
    For Each entry As ZipArchiveEntry In archive.Entries.Where(Function(a) Not String.IsNullOrEmpty(a.Name))
        entry.ExtractToFile(entry.Name)
    Next
End Using