2015-09-02 100 views
0

我試圖解壓縮.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」文件夾中創建一個「測試數據」文件夾。

回答

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 
+0

THX,但機器人的解決方案仍然創建該文件夾 – Moosli

+0

那麼它可能是因爲zip文件的目錄。看看我的編輯,看看它是否有效。如果是這種情況,那麼相應地處理它。你需要自己做一些調查。我們無法爲您完成每個場景。 – Cory