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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/906354/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 14:13:59  来源:igfitidea点击:

how to Zip files in vb.net 2005

vb.netfilezip

提问by somu

How to zip files(Any files or folder ) in vb.net 2005?

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

回答by Cheeso

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

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

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

一些示例 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

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:

提取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

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.

DotNetZip 具有用于读取、保存或提取的进度事件,因此您可以为 ASP.NET 或 Windows 窗体中的进度条提供动力。它可以处理受密码保护的 zip 文件、Unicode、ZIP64 和自解压档案。它生成的 zip 文件与所有其他 zip 工具(WinZip、WinRAR、Windows 资源管理器、Pkunzip 等)兼容。有一个很好的帮助文件(此处为在线版本),其中包含大量代码示例。也有可供下载的示例

回答by Eoin Campbell

Have a look at SharpZipLib

看看SharpZipLib

回答by Alan Haggai Alavi

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.

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

回答by Kirtan

You can use ICSharCode'sSharpZipLiblibrary.

您可以使用ICSharCode 的SharpZipLib库。

回答by Jan ?otola

You can use our Rebex ZIPcomponent.

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

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.

可以在此处找到更多示例。

回答by sealz

Shell it, wa-la done in two lines

Shell it, 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)