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

全部博文(106)

文章存档

2014年(17)

2011年(5)

2010年(75)

2009年(9)

我的朋友

分类: 嵌入式

2010-04-26 17:29:43

Imports System
Imports System.Collections.Generic
Imports System.IO
Imports System.Threading

' 此类别代表一个封存档。您可以将文件压缩后置入其中、将其所内含的压缩档解压缩出来、亦或是移除它所内含的压缩档。

Public Class ZipFile
    Private zipEntries As List(Of ZipEntry)
    Private thisReader As ZipReader
    Private thisWriter As ZipWriter
    Private baseStream As Stream
    Private zipName As String

    ' 使用指定的名称建立一个新的压缩文件。
    ' 建构函式拥有两个多载版本。

    ' 此多载版本的建构函式拥有三个参数,分别是压缩文件的文件名称、压缩的方式、以及文件存取模式。
    Public Sub New(ByVal name As String, ByVal method As Byte, ByVal mode As FileMode)
        Try
            zipName = name

            baseStream = New FileStream(zipName, mode)
            thisWriter = New ZipWriter(baseStream)
            thisWriter.Method = method

            ' 新文件。
            thisWriter.WriteSuperHeader(0, method)

            Dim index1 As Integer = zipName.IndexOf(ZipConstants.Dot)
            Dim index2 As Integer = zipName.LastIndexOf(ZipConstants.BackSlash)
            thisReader = New ZipReader(baseStream, zipName.Substring(index2, index1 - index2))

            zipEntries = thisReader.GetAllEntries()
            CH2_DemoForm002.statusMessage = String.Format(Thread.CurrentThread.CurrentUICulture, ZipConstants.NewMessage, name)
        Catch ex As IOException
            ZipConstants.ShowError(ZipConstants.IOError)
        End Try

    End Sub 'New


    ' 此多载版本的构造函数会根据所提供的名称来打开一个压缩文件。
    Public Sub New(ByVal name As String)
        Try
            zipName = name
            baseStream = New FileStream(zipName, FileMode.Open)
            thisWriter = New ZipWriter(baseStream)

            Dim index1 As Integer = zipName.IndexOf(ZipConstants.Dot)
            Dim index2 As Integer = zipName.LastIndexOf(ZipConstants.BackSlash)
            thisReader = New ZipReader(baseStream, zipName.Substring(index2, index1 - index2))

            zipEntries = thisReader.GetAllEntries()
            thisWriter.Method = thisReader.Method
            If CH2_DemoForm002.statusMessage = Nothing Or CH2_DemoForm002.statusMessage <> String.Empty Then
                CH2_DemoForm002.statusMessage = String.Format(Thread.CurrentThread.CurrentUICulture, ZipConstants.OpenMessage, name)
            End If
        Catch ex As IOException
            ZipConstants.ShowError("乐博网提示:打开文件发生错误")
        Catch ex As ArgumentOutOfRangeException
            ZipConstants.ShowError(ZipConstants.CorruptedError)
        End Try

    End Sub 'New

    ' 藉由加总所有个别项目的长度来决定所要跳跃的位移。
    ' 返回值:自 SeekOrigin.Begin 算起的位移。
    Private Function GetOffset(ByVal index As Integer) As Long
        If index > zipEntries.Count Then
            Return -1
        End If
        Dim jump As Integer = ZipConstants.SuperHeaderSize
        Dim i As Integer

        i = 0
        While i < index - 1
            Dim entry As ZipEntry = zipEntries(i)
            jump += ZipConstants.FixedHeaderSize + entry.NameLength + entry.CompressedSize
            i += 1
        End While
        Return jump

    End Function 'GetOffset


    Public Sub Add(ByVal fileName As String)
        Dim ci As System.Globalization.CultureInfo = Thread.CurrentThread.CurrentUICulture
        If fileName.ToLower(ci).Equals(zipName.ToLower(ci)) Then
            ZipConstants.ShowError("乐博网提示:无法新增至目前的 xip 文件")
            CH2_DemoForm002.statusMessage = String.Empty
            Return
        End If
        Dim entry As New ZipEntry(fileName)
        thisWriter.Add(entry)

        If CH2_DemoForm002.statusMessage.Length <> 0 Then
            zipEntries.Add(entry)
            thisWriter.CloseHeaders(CType(zipEntries.Count, Int16))
        End If
    End Sub 'Add '更多.net源码和教程,来自[乐博网 www.lob.cn]


    Public Sub Extract(ByVal index As Integer, ByVal path As String)
        If index < 0 OrElse index >= zipEntries.Count Then
            ZipConstants.ShowError("自变量超出范围" + "异常")
            Return
        End If
        thisReader.Extract(zipEntries(index), GetOffset((index + 1)), path)
    End Sub 'Extract


    Public Sub ExtractAll(ByVal path As String)
        thisReader.ExtractAll(zipEntries, path)
    End Sub 'ExtractAll


    ' 关闭压缩文档。它亦会关闭此类的所有输入数据流。
    ' 在此方法被调用之后,将不能调用其任何方法。
    Public Sub Close()
        If baseStream IsNot Nothing Then
            baseStream.Close()
        End If
    End Sub 'Close

    ' 取得代表压缩文件的输入项目。
    ' 返回值: ZipEntries 的集合。
    Public ReadOnly Property Entries() As List(Of ZipEntry)
        Get
            Return zipEntries
        End Get
    End Property

    Public Function CompressionMethod() As Byte
        Return thisWriter.Method
    End Function

    Public Function CheckFileExists(ByVal fileName As String) As Integer
        Dim ci As System.Globalization.CultureInfo = Thread.CurrentThread.CurrentUICulture
        Dim i As Integer = -1
        Dim eachEntry As ZipEntry
        For Each eachEntry In zipEntries
            i += 1
            If eachEntry.Name.ToLower(ci).Equals(fileName.ToLower(ci)) Then
                Return i
            End If
        Next eachEntry
        Return -1
    End Function 'CheckFileExists

    Private Sub DeleteEntryFromFile(ByVal index As Integer)
        Dim jump As Long = ZipConstants.SuperHeaderSize
        Dim i As Integer

        While i < index
            jump += ZipConstants.FixedHeaderSize + zipEntries(i).NameLength + zipEntries(i).CompressedSize
            i += 1
        End While
        Dim entry As ZipEntry = zipEntries(index)
        Dim fileJump As Long = ZipConstants.FixedHeaderSize + entry.NameLength + entry.CompressedSize
        baseStream.Seek(jump + fileJump, SeekOrigin.Begin)
        Dim length As Long = baseStream.Length - fileJump - jump
        Dim b(length) As Byte
        baseStream.Read(b, 0, CType(length, Integer))
        baseStream.Seek(jump, SeekOrigin.Begin)
        baseStream.Write(b, 0, CType(length, Integer))
        baseStream.SetLength(baseStream.Length - fileJump)
        CH2_DemoForm002.statusMessage = "乐博网提示:成功移除"
    End Sub 'DeleteEntryFromFile

    Public Sub Remove(ByVal index As Integer)
        Dim jump As Long = ZipConstants.SuperHeaderSize
        Dim i As Integer

        While i < index
            jump += ZipConstants.FixedHeaderSize + zipEntries(i).NameLength + zipEntries(i).CompressedSize
            i += 1
        End While
        thisWriter.Remove(jump, zipEntries(index))
        zipEntries.RemoveAt(index)
        If CH2_DemoForm002.statusMessage.Length <> 0 Then
            thisWriter.CloseHeaders(CType(zipEntries.Count, Int16))
        End If

    End Sub 'Remove
End Class 'ZipFile


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

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

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

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