April 8,2006
Zip files in .NET program
可以在 .net framework 中執行的 zip 程式 (in vb.net)
原始程式碼是從 #ziplib (ic#code 的一個porject)拿來的,經過自己修改,歡迎取用。(license 請參考 #ziplib 頁面)
'Zip Files
'queueFileNames: please enqueue the filename of files in the same folder (strPathName)
'strZipFileName: the filename of the result zip file
'nCompressionLevel: Compression Level, from 0 (no compression) to 9
'nBlockSize: block size, choose a positive integer u like
Private Sub ZipFiles(ByVal queueFileNames As Queue, ByVal strPathName As String, ByVal strZipFileName As String, ByVal nCompressionLevel As Integer, ByVal nBlockSize As Integer)
Dim strFileToZip As String
Dim strmZipFile As System.IO.FileStream
Dim strmZipStream As ZipOutputStream
Dim strmStreamToZip As System.IO.FileStream
Dim myZipEntry As ZipEntry
Dim abyBuffer(nBlockSize) As Byte
Dim nSize As System.Int32
Dim nSizeRead As Integer
'刪除已經存在的 zip file
If System.IO.File.Exists(strZipFileName) = True Then
System.IO.File.Delete(strZipFileName)
End If
'建立 zip file stream
strmZipFile = New System.IO.FileStream(strZipFileName, FileMode.OpenOrCreate, System.IO.FileAccess.ReadWrite)
strmZipStream = New ZipOutputStream(strmZipFile)
strmZipStream.SetLevel(nCompressionLevel)
While queueFileNames.Count > 0
strFileName = queueFileNames.Dequeue().ToString()
strFileToZip = strPathName & "\" & strFileName
If System.IO.File.Exists(strFileToZip) = True Then
strmStreamToZip = New System.IO.FileStream(strFileToZip, System.IO.FileMode.Open, System.IO.FileAccess.Read)
myZipEntry = New ZipEntry(strFileName)
strmZipStream.PutNextEntry(myZipEntry)
nSize = strmStreamToZip.Read(abyBuffer, 0, abyBuffer.Length)
strmZipStream.Write(abyBuffer, 0, nSize)
Try
While (nSize < strmStreamToZip.Length)
nSizeRead = strmStreamToZip.Read(abyBuffer, 0, abyBuffer.Length)
strmZipStream.Write(abyBuffer, 0, nSizeRead)
nSize = nSize + nSizeRead
End While
Catch Ex As System.Exception
Throw Ex
End Try
End If
End While
strmZipStream.Finish()
strmZipStream.Close()
strmStreamToZip.Close()
End Sub
引用URL
http://cgi.blog.roodo.com/trackback/1384597