如何在 vb.net 2005 中压缩文件

发布于 2024-07-22 02:40:41 字数 1435 浏览 2 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

以酷 2024-07-29 02:40:42

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

一些示例 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 等。有一个很好的帮助文件 (在线版本)包含大量代码示例。 还有示例可供下载

DotNetZip is an easy-to-use, free, open-source library for handling ZIP files in VB.NET and other .NET languages.

Some sample VB.NET code, to create a zip file, adding files one at a time:

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

Or, add files in a group:

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

or, Code to zip up an entire directory or folder:

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

Code to extract a zip file:

    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

Extract with a progress bar:

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 has progress events for reading, saving, or extracting, so you can power progress bars in ASP.NET or Windows Forms. It does password-protected zip files, Unicode, ZIP64, and self-extracting archives. The zip files it produces are compatible with all other zip tools - WinZip, WinRAR, Windows Explorer, Pkunzip, etc. There's a good help file (online version here) with tons of code examples. There are samples available for download, too.

筱武穆 2024-07-29 02:40:42

Have a look at SharpZipLib

有木有妳兜一样 2024-07-29 02:40:42

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

I do not know how to program in VB.NET. However, a search revealed an interesting link: Zip Compression VB.NET Examples. I hope it will be useful to you.

誰認得朕 2024-07-29 02:40:42

您可以使用 ICSharCode 的 SharpZipLib 库。

You can use ICSharCode's SharpZipLib library.

洋洋洒洒 2024-07-29 02:40:42

您可以使用我们的 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")

更多示例可以在 此处

You can use our Rebex ZIP component.

Here are some samples of operations you are asking for:

Simple zipping files in one line of code:

' 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")

Simple unzipping in one line of code:

' 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")

More samples can be found here.

雨落星ぅ辰 2024-07-29 02:40:42

剥壳,wa-la 分两行完成

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

Shell it, wa-la done in two lines

Dim zipcmd as String = "zip -r C:\directory\of\my\folder C:\directory\of\my\zip"
Shell("cmd.exe /c" + zipcmd1, AppWinStyle.Hide, True)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文