【问题标题】:how to Zip files in vb.net 2005 [closed]如何在 vb.net 2005 中压缩文件 [关闭]
【发布时间】:2009-05-25 11:18:44
【问题描述】:

如何在 vb.net 2005 中压缩文件(任何文件或文件夹)?

【问题讨论】:

    标签: vb.net file zip


    【解决方案1】:

    DotNetZip 是一个易于使用的免费开源库,用于处理 VB.NET 和其他 .NET 语言的 ZIP 文件。

    一些示例 VB.NET 代码,用于创建一个 zip 文件,一次添加一个文件:

    Dim ZipToCreate As String = "ex1.zip"
    Dim DirectoryToZip As String = "c:\temp"
    Using zip As ZipFile = New ZipFile
        Dim filenames As String() = System.IO.Directory.GetFiles(DirectoryToZip)
        Dim filename As String
        For Each filename In filenames
            zip.AddFile(filename)
        Next
        zip.Save(ZipToCreate)
    End Using
    

    或者,在组中添加文件:

    Dim ZipToCreate As String = "ex1.zip"
    Dim DirectoryToZip As String = "c:\temp"
    Dim filenames As String() = System.IO.Directory.GetFiles(DirectoryToZip)
    Using zip As ZipFile = New ZipFile
        zip.AddFiles(filenames, "temp")
        zip.Save(ZipToCreate)
    End Using
    

    或者,压缩整个目录或文件夹的代码:

    Using zip As ZipFile = New ZipFile
        zip.AddDirectory(directory)
        zip.Save(targetZip)
    End Using
    

    提取zip文件的代码:

        Dim ZipFileToExtract As String = "c:\foo.zip"
        Using zip As ZipFile = ZipFile.Read(ZipFileToExtract)
            Dim e As ZipEntry
            For Each e In zip
                ' can conditionally extract here, '
                ' based on name, size, date, whatever.'
                e.Extract
            Next
        End Using
    

    带有进度条的提取:

    Imports Ionic.Zip
    
    Module SimpleUnzip
      Public Sub Unzip(ByVal ZipToUnpack As String, ByVal ExtractDirectory As String)
        Try
          Using zip As ZipFile = ZipFile.Read(ZipToUnpack)
            Form1.ProgressBar1.Maximum = zip.Entries.Count
            Dim entry As ZipEntry
            For Each entry In zip
                Form1.Label1.Text = entry.FileName
                entry.Extract(ExtractDirectory, ExtractExistingFileAction.OverwriteSilently)
                Form1.ProgressBar1.Value = Form1.ProgressBar1.Value + 1
                ' sleep because it's too fast otherwise.
                System.Threading.Thread.Sleep(50)
            Next
            Form1.ProgressBar1.Value = 0
            Form1.Label1.Text = "Done"
          End Using
        Catch ex1 As Exception
          Form1.Label1.Text = ("Exception: " & ex1.ToString())
        End Try
      End Sub
    End Module
    

    DotNetZip 具有用于读取、保存或提取的进度事件,因此您可以为 ASP.NET 或 Windows 窗体中的进度条提供动力。它执行受密码保护的 zip 文件、Unicode、ZIP64 和自解压档案。它生成的 zip 文件与所有其他 zip 工具兼容 - WinZip、WinRAR、Windows Explorer、Pkunzip 等。有一个很好的帮助文件 (online version here),其中包含大量代码示例。也有samples available for download

    【讨论】:

    • 我过去一直使用 SharpZipLib,但我刚刚尝试过 DotNetZip,它很棒。更容易使用,并且花了我几分钟的时间来设置。谢谢芝士!
    【解决方案2】:

    看看SharpZipLib

    【讨论】:

    • DotNetZip 正在 CodePlex 上积极改进,并在 SharpZipLib 上简化了一些方法。
    【解决方案3】:

    我不知道如何在 VB.NET 中编程。然而,搜索发现了一个有趣的链接:Zip Compression VB.NET Examples。希望对你有用。

    【讨论】:

    • 注意:这些示例依赖于商业 Chilkat ZIP 库。
    【解决方案4】:

    您可以使用ICSharCode's SharpZipLib 库。

    【讨论】:

      【解决方案5】:

      您可以使用我们的Rebex ZIP 组件。

      以下是您要求的一些操作示例:

      一行代码中的简单压缩文件:

      ' add content of the local directory C:\Data\  '
      ' to the directory \Data-2010 (within the ZIP archive) '
      ' (ZIP archive C:\archive.zip doesn't have to exist) 
      ZipArchive.Add("C:\archive.zip", "C:\Data\*", "\Data-2010")
      

      一行代码简单解压:

      ' extract all *.TXT files from the directory \Data-2010 (within the ZIP file) '
      ' to the existing local directory C:\Data '
      ZipArchive.Extract("C:\archive.zip", "\Data-2010\*.html", "C:\Data")
      

      更多样例可以在here找到。

      【讨论】:

        【解决方案6】:

        去壳吧,两行就搞定了

        Dim zipcmd as String = "zip -r C:\directory\of\my\folder C:\directory\of\my\zip"
        Shell("cmd.exe /c" + zipcmd1, AppWinStyle.Hide, True)
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-08-14
          • 2012-08-22
          • 2018-10-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-11-29
          相关资源
          最近更新 更多