注意:此类在 .NET Framework 2.0 版中是新增的。
提供用于压缩和解压缩流的方法和属性。
命名空间:System.IO.Compression
程序集:System(在 system.dll 中)
Public Class GZipStream Inherits Stream
Dim instance As GZipStream
public class GZipStream : Stream
public ref class GZipStream : public Stream
public class GZipStream extends Stream
public class GZipStream extends Stream
此类表示 GZip 数据格式,它使用无损压缩和解压缩文件的行业标准算法。这种格式包括一个检测数据损坏的循环冗余校验值。GZip 数据格式使用的算法与 DeflateStream 类的算法相同,但它可以扩展以使用其他压缩格式。这种格式可以通过不涉及专利使用权的方式轻松实现。gzip 的格式可以从 RFC 1952“GZIP file format specification 4.3(GZIP 文件格式规范 4.3)GZIP file format specification 4.3(GZIP 文件格式规范 4.3)”中获得。此类不能用于压缩大于 4 GB 的文件。
给继承者的说明 当从 GZipStream 继承时,必须重写下列成员:CanSeek、CanWrite 和 CanRead。
下面的代码示例演示如何使用 GZipStream 类压缩和解压缩文件。
Imports System Imports System.IO Imports System.IO.Compression Public Class GZipTest Shared msg As String Public Shared Function ReadAllBytesFromStream(stream As Stream, buffer() As Byte) As Integer ' Use this method is used to read all bytes from a stream. Dim offset As Integer = 0 Dim totalCount As Integer = 0 While True Dim bytesRead As Integer = stream.Read(buffer, offset, 100) If bytesRead = 0 Then Exit While End If offset += bytesRead totalCount += bytesRead End While Return totalCount End Function 'ReadAllBytesFromStream Public Shared Function CompareData(buf1() As Byte, len1 As Integer, buf2() As Byte, len2 As Integer) As Boolean ' Use this method to compare data from two different buffers. If len1 <> len2 Then msg = "Number of bytes in two buffer are different" & len1 & ":" & len2 MsgBox(msg) Return False End If Dim i As Integer For i = 0 To len1 - 1 If buf1(i) <> buf2(i) Then msg = "byte " & i & " is different " & buf1(i) & "|" & buf2(i) MsgBox(msg) Return False End If Next i msg = "All bytes compare." MsgBox(msg) Return True End Function 'CompareData Public Shared Sub GZipCompressDecompress(filename As String) msg = "Test compression and decompression on file " & filename MsgBox(msg) Dim infile As FileStream Try ' Open the file as a FileStream object. infile = New FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read) Dim buffer(infile.Length - 1) As Byte ' Read the file to ensure it is readable. Dim count As Integer = infile.Read(buffer, 0, buffer.Length) If count <> buffer.Length Then infile.Close() msg = "Test Failed: Unable to read data from file" MsgBox(msg) Return End If infile.Close() Dim ms As New MemoryStream() ' Use the newly created memory stream for the compressed data. Dim compressedzipStream As New GZipStream(ms, CompressionMode.Compress, True) compressedzipStream.Write(buffer, 0, buffer.Length) ' Close the stream. compressedzipStream.Close() msg = "Original size: " & buffer.Length & ", Compressed size: " & ms.Length MsgBox(msg) ' Reset the memory stream position to begin decompression. ms.Position = 0 Dim zipStream As New GZipStream(ms, CompressionMode.Decompress) Dim decompressedBuffer(buffer.Length + 100) As Byte ' Use the ReadAllBytesFromStream to read the stream. Dim totalCount As Integer = GZipTest.ReadAllBytesFromStream(zipStream, decompressedBuffer) msg = "Decompressed " & totalCount & " bytes" MsgBox(msg) If Not GZipTest.CompareData(buffer, buffer.Length, decompressedBuffer, totalCount) Then msg = "Error. The two buffers did not compare." MsgBox(msg) End If zipStream.Close() Catch e As Exception msg = "Error: The file being read contains invalid data." MsgBox(msg) End Try End Sub 'GZipCompressDecompress Public Shared Sub Main(ByVal args() As String) Dim usageText As String = "Usage: GZIPTEST <inputfilename>" 'If no file name is specified, write usage text. If args.Length = 0 Then Console.WriteLine(usageText) Else If File.Exists(args(0)) Then GZipCompressDecompress(args(0)) End If End If End Sub 'Main End Class 'GZipTest
评论