Chinaunix首页 | 论坛 | 博客
  • 博客访问: 188399
  • 博文数量: 106
  • 博客积分: 3810
  • 博客等级: 中校
  • 技术积分: 1007
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-18 13:35
文章分类

全部博文(106)

文章存档

2014年(17)

2011年(5)

2010年(75)

2009年(9)

我的朋友

分类:

2010-04-27 07:58:44

第5个压缩类:

Imports System.IO
Imports System.IO.Compression

Public Class ZipUtility

    Public Sub CompressFile(ByVal sourceFile As String, ByVal destinationFile As String)

        ' 判断来源文件是否存在。如果不存在则掷回例外状况 FileNotFoundException。
        If File.Exists(sourceFile) = False Then
            Throw New FileNotFoundException
        End If

        ' 宣告所要使用的字节数组并将其初值设定为 Nothing。
        Dim buffer As Byte() = Nothing

        Try
            ' 建立一个 FileStream 对象来开启并读取所要压缩的文件。
            Using sourceStream As New FileStream(sourceFile, FileMode.Open, FileAccess.Read, FileShare.Read)
                ' 建立一个字节数组并将其大小设定为 sourceStream 的长度。
                buffer = New Byte(sourceStream.Length - 1) {}

                ' 将 sourceStream 对象的内容写入到字节数组 buffer 中,
                ' 并建立整数变量 checkCounter 来接收 Read 方法回传之成功读取的长度值。
                Dim checkCounter As Integer = sourceStream.Read(buffer, 0, buffer.Length)

                ' 建立一个 FileStream 对象来建立并写入一个新文件,此新文件就是用来存放压缩后之数据的压缩文件。
                Using destinationStream As New FileStream(destinationFile, FileMode.OpenOrCreate, FileAccess.Write)
                    ' 使用目标文件的数据流 destinationStream 来建立一个 GZipStream 对象。
                    Using compressedStream As New GZipStream(destinationStream, CompressionMode.Compress, True)
                        ' 压缩字节数组中的数据并将压缩后的数据写入基础数据流中。
                        compressedStream.Write(buffer, 0, buffer.Length)
                    End Using
                End Using
            End Using

            ' 处理程序执行过程中所掷回的 ApplicationException 例外状况。
        Catch ex As ApplicationException
            MessageBox.Show(ex.Message, " 乐博网提示:压缩文件过程中发生错误!", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Public Sub DecompressFile(ByVal sourceFile As String, ByVal destinationFile As String)

        ' 判断源文件是否存在,如果不存在则引发 FileNotFoundException 异常。
        If File.Exists(sourceFile) = False Then
            Throw New FileNotFoundException
        End If

        ' 宣告要使用的字节数组并将其初始值设定为 Nothing。
        Dim quartetBuffer As Byte() = Nothing

        Try
            ' 建立一个FileStream对象来开启并读取所要解压缩的压缩文档。
            Using sourceStream As New FileStream(sourceFile, FileMode.Open)

                ' 建立 GZipStream 对象 decompressedStream 同时准备进行解压缩数据流对象内容。
                Using decompressedStream As New GZipStream(sourceStream, CompressionMode.Decompress, True)

                    ' 建立字节数组并指定大小为 4。
                    quartetBuffer = New Byte(4) {}

                    ' 建立整数变量 position 并将 sourceStream 对象的长度减 4 之后赋给它。
                    Dim position As Integer = CType(sourceStream.Length, Integer) - 4

                    ' 设定 sourceStream 的串流位置为变量 position 之值。
                    sourceStream.Position = position

                    ' 从现在的 sourceStream 串流位置读取长度为 4 的数据并写入变量 quartetBuffer。
                    sourceStream.Read(quartetBuffer, 0, 4)

                    ' 设定 sourceStream 的串流位置为最开头。
                    sourceStream.Position = 0

                    ' 将 quartetBuffer 中各个数值转换为 Int32 类型的数值。
                    Dim checkLength As Integer = BitConverter.ToInt32(quartetBuffer, 0)

                    ' 建立字节数组 buffer 并指定大小为 checkLength + 100。
                    Dim buffer(checkLength + 100) As Byte

                    ' 建立整数变量 offset 并指定初始值为 0。
                    Dim offset As Integer = 0

                    ' 建立整数变量 total 并指定初始值为 0。
                    Dim total As Integer = 0

                    ' 此循环会反复呼叫 GZipStream 对象的 Read 方法
                    ' 以便将解压缩之后的字节写入字节数组中。
                    While True
                        Dim bytesRead As Integer = decompressedStream.Read(buffer, offset, 100)

                        ' 判断读取 decompressedStream 内容的状况。当成功读取的
                        ' 长度值为 0 时表示已经没有数据可以读取,离开循环。
                        If bytesRead = 0 Then
                            Exit While
                        End If

                        ' 计算新的 decompressedStream 数据流位置准备进行下一次的内容读取。
                        offset += bytesRead

                        ' 计算总共成功读取的长度值。
                        total += bytesRead
                    End While

                    ' 建立一个 FileStream 对象来建立并写入一个新文件,
                    ' 此新文件就是用来存放解压缩后之数据的文件。
                    Using destinationStream As New FileStream(destinationFile, FileMode.Create)

                        ' 将存放解压缩数据之字节数组的内容写入文件数据流。。
                        destinationStream.Write(buffer, 0, total)

                    End Using '更多.net源码和教程,来自[乐博网 www.lob.cn]
                End Using
            End Using
        Catch ex As ApplicationException
            MessageBox.Show(ex.Message, " 解压缩文件过程中发生错误!", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub
End Class


阅读(351) | 评论(0) | 转发(0) |
0

上一篇:.Net经典压缩类4

下一篇:.Net经典压缩类6

给主人留下些什么吧!~~