microsoft-visualbasic-runtime/Net/HTTP/Stream/ZipStream.vb

109 lines
3.8 KiB
VB.net
Raw Normal View History

2019-05-24 18:40:50 +08:00
#Region "Microsoft.VisualBasic::2bc81f82cf1a26d06e4d18edad34ebe8, Microsoft.VisualBasic.Core\Net\HTTP\Stream\ZipStream.vb"
' Author:
'
' asuka (amethyst.asuka@gcmodeller.org)
' xie (genetics@smrucc.org)
' xieguigang (xie.guigang@live.com)
'
' Copyright (c) 2018 GPL3 Licensed
'
'
' GNU GENERAL PUBLIC LICENSE (GPL3)
'
'
' This program is free software: you can redistribute it and/or modify
' it under the terms of the GNU General Public License as published by
' the Free Software Foundation, either version 3 of the License, or
' (at your option) any later version.
'
' This program is distributed in the hope that it will be useful,
' but WITHOUT ANY WARRANTY; without even the implied warranty of
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
' GNU General Public License for more details.
'
' You should have received a copy of the GNU General Public License
' along with this program. If not, see <http://www.gnu.org/licenses/>.
' /********************************************************************************/
' Summaries:
' Module ZipStreamExtensions
'
2019-05-24 18:40:50 +08:00
' Function: Deflate, UnZipStream
'
'
' /********************************************************************************/
#End Region
Imports System.IO
2018-11-17 17:52:38 +08:00
Imports System.IO.Compression
Imports System.Runtime.CompilerServices
Imports Microsoft.VisualBasic.Linq
Namespace Net.Http
Public Module ZipStreamExtensions
''' <summary>
''' 进行zlib数据流的zip解压缩
'''
''' > https://bbs.csdn.net/topics/392275364
''' </summary>
''' <param name="stream"></param>
''' <returns></returns>
''' <remarks>
''' ###### 2018-11-15 经过测试没有bug与zlibnet的测试结果一致
''' </remarks>
<Extension>
2019-05-24 18:40:50 +08:00
Public Function UnZipStream(stream As IEnumerable(Of Byte), Optional noMagic As Boolean = False) As MemoryStream
2018-11-17 17:52:38 +08:00
Dim pBytes = stream.SafeQuery.ToArray
' Deflate 算法压缩之后的数据,第一个字节是 78h120b第二个字节是 DAh(218b)
2019-05-24 18:40:50 +08:00
If pBytes.Length < 2 Then
2018-11-17 17:52:38 +08:00
Return New MemoryStream(pBytes)
2019-05-24 18:40:50 +08:00
ElseIf (Not pBytes(0) = 120 AndAlso Not pBytes(1) = 218) Then
If noMagic Then
' 最开始的两个字节不是magic标记
' 并且也指定了没有magic头的参数选项
' 在这里直接尝试解压缩
Using compress As New MemoryStream(pBytes)
Return compress.Deflate
End Using
Else
Return New MemoryStream(pBytes)
End If
2018-11-17 17:52:38 +08:00
End If
Using compress As New MemoryStream(pBytes)
' 先读取前两个deflate压缩算法标识字节然后才能用deflateStream解压
' 这个行为与 zlib库、sharpZiplib库等不同这些库都是直接传入解压
2019-05-24 18:40:50 +08:00
Call compress.ReadByte()
Call compress.ReadByte()
2018-11-17 17:52:38 +08:00
2019-05-24 18:40:50 +08:00
Return compress.Deflate
End Using
End Function
''' <summary>
''' 在这里应该是正确的跳过了deflate压缩算法标识字节的数据流
''' </summary>
''' <returns></returns>
<Extension>
Public Function Deflate(compress As Stream) As MemoryStream
Dim deflatMs As New MemoryStream()
2018-11-17 17:52:38 +08:00
2019-05-24 18:40:50 +08:00
Using deflatestream As New DeflateStream(compress, CompressionMode.Decompress)
deflatestream.CopyTo(deflatMs, 8192)
2018-11-17 17:52:38 +08:00
End Using
2019-05-24 18:40:50 +08:00
Return deflatMs
2018-11-17 17:52:38 +08:00
End Function
End Module
End Namespace