【问题标题】:Download a zip file and extract it using background worker下载一个 zip 文件并使用后台工作程序将其解压缩
【发布时间】:2015-09-13 18:02:18
【问题描述】:

我是 VB.NET 的新手,我在 Google 上搜索了很多,但找不到答案。

我希望程序下载一个 zip 文件并使用 BackgroundWorker 解压缩它。

我有下载代码,但找不到如何使用 BackgroundWorker 解压缩文件。

 Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    downloadFile("https://dl.dropboxusercontent.com/u/85542087/test.exe", "test.exe")
    Status.Text = "Downloading file from W4H Servers...."
End Sub
Private Sub downloadFile(ByVal srcPath As String, ByVal destPath As String)

    Dim wClient As New System.Net.WebClient()
    AddHandler wClient.DownloadProgressChanged, AddressOf downloadFile_ProgressChanged

    wClient.DownloadFileAsync(New System.Uri(srcPath), destPath)
    Status.Text = "Downloading file from W4H Servers...."
End Sub

Private Sub downloadFile_ProgressChanged(ByVal sender As Object, ByVal e As System.Net.DownloadProgressChangedEventArgs)
    Status.Text = "Downloading file from W4H Servers...."
    ProgressBar1.Maximum = e.TotalBytesToReceive
    ProgressBar1.Value = e.BytesReceived
    Application.DoEvents()
    If e.ProgressPercentage = 100 Then
        'download completed
        System.Threading.Thread.Sleep(2000)
        Me.Close()
        Form1.Close()
        Process.Start("test.exe")
    End If
End Sub

以上代码将下载我的文件到 .exe 执行路径并从那里运行。所以我想在那里提取并运行它。

压缩包为“test.zip”,其中包含“test.exe”。

【问题讨论】:

  • 我使用 DotNetZip- 添加 dll 但工作正常。据我所知,没有用于从 .zip 文件解压缩或提取文件的 Windows 或 .Net 解决方案。
  • 我使用了 SharpZipLib (icsharpcode.github.io/SharpZipLib),效果很好。除非它是一个非常大的文件,否则您可能真的不需要 BackgroundWorker 来执行此操作。解压缩一个典型的 .exe 应该不到一秒钟。
  • 您需要关注如何使用后台工作人员解压缩文件,而是关注两个不同的问题 - 使用后台工作人员 --> msdn.microsoft.com/en-us/library/cc221403%28v=vs.95%29.aspx解压文件。一旦你有了解压文件的代码,你就可以在bgwDoWork运行它——完成

标签: vb.net


【解决方案1】:

你可以使用这个功能:

Imports System.IO

'Declare the shell object
Dim shObj As Object = Activator.CreateInstance(Type.GetTypeFromProgID("Shell.Application"))

Public Sub UnZip()

    'Create directory in which you will unzip your items.
    Directory.CreateDirectory(output-Folder)

    'Declare the folder where the items will be extracted.
    Dim output As Object = shObj.NameSpace((output-Folder))

    'Declare the input zip file.
    Dim input As Object = shObj.NameSpace((path-of-zip-file))

    'Extract the items from the zip file.
    output.CopyHere((input.Items), 4)

End Sub

【讨论】:

  • 最后一次看到 OP 是在 3 年前 的 9 月 19 日。我非常怀疑他会回来查看这个答案。你为什么要找到并回答这些老酸甜甜圈?
猜你喜欢
  • 1970-01-01
  • 2011-05-28
  • 1970-01-01
  • 2021-03-07
  • 1970-01-01
  • 2021-03-03
  • 1970-01-01
  • 2016-01-15
  • 1970-01-01
相关资源
最近更新 更多