| 1 | #Region "Microsoft.VisualBasic::91762bbb449e95456d3322a02d642d4e, Microsoft.VisualBasic.Core\Net\HTTP\ZipStream.vb" |
| 2 | |
| 3 | ' Author: |
| 4 | ' |
| 5 | ' asuka (amethyst.asuka@gcmodeller.org) |
| 6 | ' xie (genetics@smrucc.org) |
| 7 | ' xieguigang (xie.guigang@live.com) |
| 8 | ' |
| 9 | ' Copyright (c) 2018 GPL3 Licensed |
| 10 | ' |
| 11 | ' |
| 12 | ' GNU GENERAL PUBLIC LICENSE (GPL3) |
| 13 | ' |
| 14 | ' |
| 15 | ' This program is free software: you can redistribute it and/or modify |
| 16 | ' it under the terms of the GNU General Public License as published by |
| 17 | ' the Free Software Foundation, either version 3 of the License, or |
| 18 | ' (at your option) any later version. |
| 19 | ' |
| 20 | ' This program is distributed in the hope that it will be useful, |
| 21 | ' but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 22 | ' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 23 | ' GNU General Public License for more details. |
| 24 | ' |
| 25 | ' You should have received a copy of the GNU General Public License |
| 26 | ' along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 27 | |
| 28 | |
| 29 | |
| 30 | ' /********************************************************************************/ |
| 31 | |
| 32 | ' Summaries: |
| 33 | |
| 34 | ' Module ZipStreamExtensions |
| 35 | ' |
| 36 | ' Function: UnZipStream |
| 37 | ' |
| 38 | ' |
| 39 | ' /********************************************************************************/ |
| 40 | |
| 41 | #End Region |
| 42 | |
| 43 | Imports System.IO |
| 44 | Imports System.IO.Compression |
| 45 | Imports System.Runtime.CompilerServices |
| 46 | Imports Microsoft.VisualBasic.Linq |
| 47 | |
| 48 | Namespace Net.Http |
| 49 | |
| 50 | Public Module ZipStreamExtensions |
| 51 | |
| 52 | ''' <summary> |
| 53 | ''' 进行zlib数据流的zip解压缩 |
| 54 | ''' |
| 55 | ''' > https://bbs.csdn.net/topics/392275364 |
| 56 | ''' </summary> |
| 57 | ''' <param name="stream"></param> |
| 58 | ''' <returns></returns> |
| 59 | ''' <remarks> |
| 60 | ''' ###### 2018-11-15 经过测试,没有bug,与zlibnet的测试结果一致 |
| 61 | ''' </remarks> |
| 62 | <Extension> |
| 63 | Public Function UnZipStream(stream As IEnumerable(Of Byte)) As MemoryStream |
| 64 | Dim pBytes = stream.SafeQuery.ToArray |
| 65 | |
| 66 | ' Deflate 算法压缩之后的数据,第一个字节是 78h(120b),第二个字节是 DAh(218b) |
| 67 | If pBytes.Length < 2 OrElse (Not pBytes(0) = 120 AndAlso Not pBytes(1) = 218) Then |
| 68 | Return New MemoryStream(pBytes) |
| 69 | End If |
| 70 | |
| 71 | Using compress As New MemoryStream(pBytes) |
| 72 | Dim deflatMs As New MemoryStream() |
| 73 | |
| 74 | ' 先读取前两个deflate压缩算法标识字节,然后才能用deflateStream解压 |
| 75 | ' 这个行为与 zlib库、sharpZiplib库等不同(这些库都是直接传入解压) |
| 76 | compress.ReadByte() |
| 77 | compress.ReadByte() |
| 78 | |
| 79 | Using deflatestream As New DeflateStream(compress, CompressionMode.Decompress, False) |
| 80 | deflatestream.CopyTo(deflatMs, 8192) |
| 81 | End Using |
| 82 | |
| 83 | Return deflatMs |
| 84 | End Using |
| 85 | End Function |
| 86 | End Module |
| 87 | End Namespace |