正文

vb2005如何实现文件的压缩2006-08-27 09:17:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/iamben250/17928.html

分享到:

GZipStream 类 

注意:此类在 .NET Framework 2.0 版中是新增的。

提供用于压缩和解压缩流的方法和属性。

命名空间:System.IO.Compression
程序集:System(在 system.dll 中)

语法
Visual Basic(声明)
Public Class GZipStream
    Inherits Stream
Visual Basic(用法)
Dim instance As GZipStream
C#
public class GZipStream : Stream
C++
public ref class GZipStream : public Stream
J#
public class GZipStream extends Stream
JScript
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 继承时,必须重写下列成员:CanSeekCanWriteCanRead

示例

下面的代码示例演示如何使用 GZipStream 类压缩和解压缩文件。

Visual Basic
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 

阅读(3205) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册