VB.net 2005內GZipStream類別可以針對檔案做壓縮、解壓縮
Imports System.IO
Imports System.IO.Compression
Public Class ClsZip
Public Sub CompressFile(ByVal sourceFile As String, ByVal destinationFile As String)
'壓縮檔案
If Not File.Exists(sourceFile) Then
Throw New FileNotFoundException
End If
Dim sourceStream As FileStream = Nothing
Dim destinationStream As FileStream = Nothing
Dim compressedStream As GZipStream = Nothing
Try
'Read the bytes from the source file into a byte array
sourceStream = New FileStream(sourceFile, FileMode.Open, FileAccess.Read, FileShare.Read)
'Read the source stream values into the buffer
Dim buffer(sourceStream.Length - 1) As Byte
Dim checkCounter As Integer = sourceStream.Read(buffer, 0, buffer.Length)
If checkCounter <> buffer.Length Then
Throw New ApplicationException
End If
'Open the FileStream to write to
destinationStream = New FileStream(destinationFile, FileMode.OpenOrCreate, FileAccess.Write)
'Create a compression stream pointing to the destiantion stream
compressedStream = New GZipStream(destinationStream, CompressionMode.Compress, True)
'Now write the compressed data to the destina ..
訪客只能看到部份內容,免費 加入會員 或由臉書 Google 可以看到全部內容