C#(VB.NET) 压缩成zip包的两种方式SharpZipLib和System.IO.Packaging。

1.SharpZipLib第三方插件(开源免费)

  1.1官方代码和文档URL:GitHub - icsharpcode/SharpZipLib: #ziplib is a Zip, GZip, Tar and BZip2 library written entirely in C# for the .NET platform.

  1.2关于版本:v0.86.0.518版本支持.netframework4.0,

                    v1.0.0-rc1以上只支持.netframework4.5以上。

  1.3各个版本的信息和dll:Releases · icsharpcode/SharpZipLib · GitHub

  1.4.压缩的代码实例:

Private Sub fileToZip(ByVal sourceDir As String, ByVal targetName As String)
	'sourceDir="D:\test" 被压缩的文件夹路径。
	If sourceDir.Length = 0 Then
	    MessageBox.Show("Please specify a directory")
	    Return
	Else
	    If Not Directory.Exists(sourceDir) Then
		MessageBox.Show(sourceDir, "Directory not found")
		Return
	    End If
	End If

	'targetName="D:\test\A.zip"指定压缩后的路径和文件夹。
	If targetName.Length = 0 Then
	    MessageBox.Show("No name specified", "Zip file name error")
	    Return
	End If

	Dim astrFileNames() As String = Directory.GetFiles(sourceDir)
	Dim strmZipOutputStream As ZipOutputStream

	strmZipOutputStream = New ZipOutputStream(File.Create(targetName))
	Try

	    REM Compression Level: 0-9
	    REM 0: no(Compression)
	    REM 9: maximum compression
	    strmZipOutputStream.SetLevel(9)

	    Dim strFile As String
	    Dim abyBuffer(4096) As Byte

	    For Each strFile In astrFileNames
		Dim strmFile As FileStream = File.OpenRead(strFile)
		Try

		    Dim objZipEntry As ZipEntry = New ZipEntry(Path.GetFileName(strFile))
		    objZipEntry.DateTime = DateTime.Now
		    objZipEntry.Size = strmFile.Length

		    strmZipOutputStream.PutNextEntry(objZipEntry)
		    StreamUtils.Copy(strmFile, strmZipOutputStream, abyBuffer)
		Finally
		    strmFile.Close()
		End Try
	    Next

	    strmZipOutputStream.Finish()
	Finally
	    strmZipOutputStream.Close()
	End Try
End Sub

  1.5解压的代码实例:

Private Sub zipToFile(ByVal folderName As String, ByVal unZipDir As String)
	'folderName = "D:\test\A.zip" 压缩包的文件夹路径。
	If folderName.Length = 0 Then
	    MessageBox.Show("Please specify a directory")
	    Return
	End If

	'unZipDir="D:\test"解压后的路径。
	If Not unZipDir.EndsWith("\\") Then
	    unZipDir += "\\"
	End If

	If Not Directory.Exists(unZipDir) Then
	    Directory.CreateDirectory(unZipDir)
	End If

	If unZipDir.Length = 0 Then
	    MessageBox.Show("No name specified", "Zip file name error")
	    Return
	End If

	Dim strmZipInputStream As ZipInputStream = New ZipInputStream(File.OpenRead(folderName))
	Try
	    Dim objEntry As ZipEntry = strmZipInputStream.GetNextEntry()

	    While IsNothing(objEntry) = False

		Dim directoryName As String = Path.GetDirectoryName(objEntry.Name)
		Dim fileName As String = Path.GetFileName(objEntry.Name)

		If Not directoryName.EndsWith("\\") Then
		    directoryName += "\\"
		End If

		Dim zipDir As String = unZipDir + directoryName
		If directoryName.Length > 0 AndAlso Not Directory.Exists(zipDir) Then
		    Directory.CreateDirectory(zipDir)
		End If

		If objEntry.IsFile AndAlso fileName.Length > 0 Then
		    Using streamWriter As FileStream = File.Create(unZipDir + objEntry.Name)

			Dim nSize As Integer = 2048
			Dim abyData(2048) As Byte

			While True
			    nSize = strmZipInputStream.Read(abyData, 0, abyData.Length)
			    If nSize > 0 Then
				streamWriter.Write(abyData, 0, nSize)
			    Else
				Exit While
			    End If
			End While
		    End Using
		End If

		objEntry = strmZipInputStream.GetNextEntry()
	    End While

	Finally
	    strmZipInputStream.Close()
	End Try
End Sub

2.System.IO.Packaging压缩和解压

  2.1System.IO.Packaging的优缺点:

    优点:从.netframework中可以直接应用,不用添加第三方DLL。属于WindowsBase.dll。

    缺点:使用Packaging压缩文件会在zip文件自动生成[Content_Type].xml,用来描述zip文件解压支持的文件格式。

  2.2压缩代码实例(解压代码可以百度):

Private Sub fileToZip(ByVal folderName As String, ByVal compressedFileName As String) ', ByVal sDirectory As String)

	'folderName="D:\test" 被压缩的文件夹路径。
	If folderName.Substring(folderName.Length - 1) = "\" Then
	    folderName = folderName.Remove(folderName.Length - 1)
	End If

	If Directory.Exists(folderName) = False Then
	    Return
	End If

	Try
	    'compressedFileName="D:\test\A.zip"指定压缩后的路径和文件夹。
	    Using package As Packaging.Package = System.IO.Packaging.Package.Open(compressedFileName, FileMode.Create)

		For Each fileName As String In Directory.EnumerateFiles(folderName, "*", SearchOption.AllDirectories)

		    Dim pathInPackage As String = ""
		    pathInPackage = Path.GetDirectoryName(fileName).Replace(folderName, String.Empty) & "/" & Path.GetFileName(fileName)

		    Dim partUriDocument As Uri = Packaging.PackUriHelper.CreatePartUri(New Uri(pathInPackage, UriKind.Relative))
		    Dim packagePartDocument As Packaging.PackagePart = package.CreatePart(partUriDocument, "", Packaging.CompressionOption.Maximum)

		    Using fileStream As FileStream = New FileStream(fileName, FileMode.Open, FileAccess.Read)
			fileStream.CopyTo(packagePartDocument.GetStream())
		    End Using

		Next
	    End Using

	Catch ex As Exception

	End Try
End Sub

  2.3 关于[Content_Type].xml文件,

    如果zip文件不包含[Content_Type].xml文件,或者[Content_Type].xml文件不包含所对应扩展名的描述(手动添加的[Content_Type].xml也是可以),将无法解压zip包文件。 

附件: 支持.net framework 4.0版的SharpZipLib.dll

如果资源学习了,可以点赞支持!

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ICSharpCode.SharpZipLib.Zip是一个开源的C#库,可以用于创建和解压缩Zip文件。以下是VB.NET中使用ICSharpCode.SharpZipLib.Zip进行Zip文件操作的基本步骤: 1. 首先,将ICSharpCode.SharpZipLib.Zip添加到你的项目中。 2. 创建一个ZipOutputStream对象来创建Zip文件,如下所示: ``` Dim zipStream As New ZipOutputStream(File.Create("C:\Test.zip")) ``` 这将创建一个名为Test.zipZip文件,并将其保存在C盘根目录下。 3. 要添加文件Zip文件中,可以使用ZipEntry对象。创建一个ZipEntry对象,并将其添加到ZipOutputStream对象中,如下所示: ``` Dim entry As New ZipEntry("C:\TestFolder\TestFile.txt") zipStream.PutNextEntry(entry) ``` 这将添加名为TestFile.txt文件到名为TestFolder的文件夹中。 4. 使用ZipOutputStream对象的Write方法将文件内容写入Zip文件中,如下所示: ``` Dim buffer As Byte() = New Byte(4096) {} Dim count As Integer = 0 Using streamReader As New FileStream("C:\TestFolder\TestFile.txt", FileMode.Open, FileAccess.Read) Do While (count = streamReader.Read(buffer, 0, buffer.Length)) > 0 zipStream.Write(buffer, 0, count) Loop End Using ``` 这将逐字节读取TestFile.txt文件并将其写入Zip文件中。 5. 最后,关闭ZipOutputStream对象以完成Zip文件创建,如下所示: ``` zipStream.Close() ``` 这将关闭ZipOutputStream对象并完成Zip文件的创建。 以上是使用ICSharpCode.SharpZipLib.ZipVB.NET中创建Zip文件的基本步骤。解压缩Zip文件的过程类似,可以使用ZipInputStream对象和ZipEntry对象来读取Zip文件的内容。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值