如何在vb.net 2005中压缩文件 [英] how to Zip files in vb.net 2005

查看:124
本文介绍了如何在vb.net 2005中压缩文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

推荐答案

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

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

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

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

提取zip文件的代码:

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

使用进度条提取:

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 Forms中增强进度栏.它执行受密码保护的zip文件,Unicode,ZIP64和自解压缩的存档.它生成的zip文件与所有其他zip工具兼容-WinZip,WinRAR,Windows Explorer,Pkunzip等.有一个很好的帮助文件(示例可供下载.

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.

这篇关于如何在vb.net 2005中压缩文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆