轻松压缩文件!
本文的目的是演示如何使用 System.IO.Packaging 命名空间中的 ZipPackage 类来压缩文件。此演示使用:VB.Net 2008 .Net, Framework 3.0。
演示预览
![]() |
引言
对于 .Net 程序员来说,使用 .Net Framework 3.0 压缩多个文件从未如此简单! 使用 System.IO.Packaging
命名空间中的新 ZipPackage 类和几行代码,您可以轻松创建包含任意数量文件的 .zip 压缩包!
Using the Code
最简单的方法
- 添加对 "WindowsBase.dll" 的引用(项目菜单 | 添加引用...)
如果在 .net 选项卡上找不到该 .dll,请单击“浏览”选项卡,并尝试在以下目录中查找它
- C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.0\
如果仍然找不到,则可能需要搜索您的系统。
- 在类的顶部添加 Imports 语句
Imports System.IO.Packaging
- 编写一些代码
Dim zipPath As String = "C:\TEMP\Compression\myzip.zip" Dim fileToAdd As String = "C:\TEMP\Compression\Compress Me.txt" 'Open the zip file if it exists, else create a new one Dim zip As Package = ZipPackage.Open(zipPath, _ IO.FileMode.OpenOrCreate, IO.FileAccess.ReadWrite) 'Replace spaces with an underscore (_) Dim uriFileName As String = fileToAdd.Replace(" ", "_") 'A Uri always starts with a forward slash "/" Dim zipUri As String = String.Concat("/", _ IO.Path.GetFileName(uriFileName)) Dim partUri As New Uri(zipUri, UriKind.Relative) Dim contentType As String = _ Net.Mime.MediaTypeNames.Application.Zip 'The PackagePart contains the information: ' Where to extract the file when it's extracted (partUri) ' The type of content stream (MIME type) - (contentType) ' The type of compression to use (CompressionOption.Normal) Dim pkgPart As PackagePart = _ zip.CreatePart(partUri, contentType, _ CompressionOption.Normal) 'Read all of the bytes from the file to add to the zip file Dim bites As Byte() = File.ReadAllBytes(fileToAdd) 'Compress and write the bytes to the zip file pkgPart.GetStream().Write(bites, 0, bites.Length) zip.Close() 'Close the zip file
- 重构代码以提高效率
Private Sub ZipFiles() Dim zipPath As String = "C:\TEMP\Compression\myzip.zip" 'Open the zip file if it exists, else create a new one Dim zip As Package = ZipPackage.Open(zipPath, _ IO.FileMode.OpenOrCreate, IO.FileAccess.ReadWrite) 'Add as many files as you like: AddToArchive(zip, "C:\TEMP\Compression\Compress Me1.txt") AddToArchive(zip, "C:\TEMP\Compression\Compress Me2.txt") AddToArchive(zip, "C:\TEMP\Compression\Compress Me3.txt") zip.Close() 'Close the zip file End Sub Private Sub AddToArchive(ByVal zip As Package, _ ByVal fileToAdd As String) 'Replace spaces with an underscore (_) Dim uriFileName As String = fileToAdd.Replace(" ", "_") 'A Uri always starts with a forward slash "/" Dim zipUri As String = String.Concat("/", _ IO.Path.GetFileName(uriFileName)) Dim partUri As New Uri(zipUri, UriKind.Relative) Dim contentType As String = _ Net.Mime.MediaTypeNames.Application.Zip 'The PackagePart contains the information: ' Where to extract the file when it's extracted (partUri) ' The type of content stream (MIME type): (contentType) ' The type of compression: (CompressionOption.Normal) Dim pkgPart As PackagePart = zip.CreatePart(partUri, _ contentType, CompressionOption.Normal) 'Read all of the bytes from the file to add to the zip file Dim bites As Byte() = File.ReadAllBytes(fileToAdd) 'Compress and write the bytes to the zip file pkgPart.GetStream().Write(bites, 0, bites.Length) End Sub
关注点
如果您使用 WinZip 或其他压缩工具打开 .zip 文件,您会注意到一个“[Content_Types].xml”文件。不幸的是,您无法控制此文件的存在。它是一个包含有关压缩文件中文件类型内容信息的文件,例如文本文件的“txt”,Word 文档的“doc”等。
此外,如果您设置了 PackagePart
(pkgPart.Package.PackageProperties
)的任何属性,则压缩包中将包含其他文件,例如一个“###.psmdcp”文件(其中 ### 是随机生成的一个数字),该文件包含有关包属性的元数据;以及一个“.rels”文件,它是一个包含有关包关系的元数据的 xml 文件。
我提供了一个完整的 Zip 演示,演示了在 VB.Net 2008 中压缩和解压缩 zip 压缩包。它的风格类似于 WinZip,但功能没有那么全面。
希望对您有所帮助!
VBRocks
2008 MS Visual Basic MVP