C#(VB.NET) 压缩成zip包
SharpZipLib第三方插件(开源免费)
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