1 | #Region "Microsoft.VisualBasic::4dd2e8adb45b820828d170c71d1fb73e, Microsoft.VisualBasic.Core\Net\HTTP\GZStream.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 GZStream |
35 | ' |
36 | ' Function: GZipAsBase64, GZipStream, UnGzipBase64, UnGzipStream |
37 | ' |
38 | ' |
39 | ' /********************************************************************************/ |
40 | |
41 | #End Region |
42 | |
43 | Imports System.IO |
44 | Imports System.IO.Compression |
45 | Imports System.Runtime.CompilerServices |
46 | |
47 | Namespace Net.Http |
48 | |
49 | ''' <summary> |
50 | ''' 请注意,这个模块是处理http请求或者响应之中的gzip压缩的数据, |
51 | ''' 对于zip压缩的数据需要使用<see cref="ZipStreamExtensions"/>模块之中的帮助函数来完成 |
52 | ''' </summary> |
53 | Public Module GZStream |
54 | |
55 | ''' <summary> |
56 | ''' Unzip the stream data in the <paramref name="base64"/> string. |
57 | ''' </summary> |
58 | ''' <param name="base64$"></param> |
59 | ''' <returns></returns> |
60 | ''' |
61 | <MethodImpl(MethodImplOptions.AggressiveInlining)> |
62 | <Extension> |
63 | Public Function UnGzipBase64(base64$) As MemoryStream |
64 | Return Convert.FromBase64String(base64).UnGzipStream |
65 | End Function |
66 | |
67 | ''' <summary> |
68 | ''' 将输入的流数据进行gzip解压缩 |
69 | ''' </summary> |
70 | ''' <param name="stream"></param> |
71 | ''' <returns></returns> |
72 | <Extension> |
73 | Public Function UnGzipStream(stream As IEnumerable(Of Byte)) As MemoryStream |
74 | Dim ms As New MemoryStream |
75 | Dim gz As New GZipStream(New MemoryStream(stream.ToArray), CompressionMode.Decompress) |
76 | Call gz.CopyTo(ms) |
77 | Return ms |
78 | End Function |
79 | |
80 | ''' <summary> |
81 | ''' 对所输入的流进行gzip压缩 |
82 | ''' </summary> |
83 | ''' <param name="stream"></param> |
84 | ''' <returns></returns> |
85 | <Extension> |
86 | Public Function GZipStream(stream As Stream) As MemoryStream |
87 | Dim ms As New MemoryStream |
88 | Dim gz As New GZipStream(ms, CompressionMode.Compress) |
89 | Call stream.CopyTo(gz) |
90 | Return ms |
91 | End Function |
92 | |
93 | ''' <summary> |
94 | ''' 将目标流对象之中的数据进行zip压缩,然后转换为base64字符串 |
95 | ''' </summary> |
96 | ''' <param name="stream"></param> |
97 | ''' <returns></returns> |
98 | <Extension> Public Function GZipAsBase64(stream As Stream) As String |
99 | Dim bytes As Byte() = stream.GZipStream.ToArray |
100 | Dim s$ = Convert.ToBase64String(bytes) |
101 | Return s |
102 | End Function |
103 | End Module |
104 | End Namespace |