FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Magicodes.NET/Magicodes.Utility/CompressionUtils.cs at master · magicodes/Magicodes.NET · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
This repository was archived by the owner on Feb 21, 2022. It is now read-only.
magicodes
/
Magicodes.NET
Public archive
Notifications
You must be signed in to change notification settings
Fork
90
Star
115
Code
Issues
11
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
Magicodes.NET
/
Magicodes.Utility
/
CompressionUtils.cs
Copy path
More file actions
More file actions
Latest commit
History
History
History
96 lines (95 loc) · 3.13 KB
Breadcrumbs
Magicodes.NET
/
Magicodes.Utility
/
CompressionUtils.cs
Copy path
File metadata and controls
96 lines (95 loc) · 3.13 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
using
System
;
using
System
.
Collections
.
Generic
;
using
System
.
IO
;
using
System
.
IO
.
Compression
;
using
System
.
Linq
;
using
System
.
Text
;
using
System
.
Web
;
using
Magicodes
.
Utility
;
namespace
Magicodes
.
Utility
{
/// <summary>
/// 压缩
/// </summary>
public
class
CompressionUtils
{
/// <summary>
/// GZip压缩输出
/// </summary>
/// <param name="instance"></param>
public
static
void
GZipAndSend
(
object
instance
)
{
CompressionUtils
.
GZipAndSend
(
(
instance
!=
null
)
?
instance
.
ToString
(
)
:
string
.
Empty
)
;
}
/// <summary>
/// GZip压缩输出
/// </summary>
/// <param name="instance"></param>
public
static
void
GZipAndSend
(
string
instance
)
{
CompressionUtils
.
GZipAndSend
(
instance
,
"application/json"
)
;
}
/// <summary>
/// GZip压缩输出
/// </summary>
/// <param name="instance"></param>
/// <param name="responseType"></param>
public
static
void
GZipAndSend
(
string
instance
,
string
responseType
)
{
CompressionUtils
.
GZipAndSend
(
Encoding
.
UTF8
.
GetBytes
(
instance
)
,
responseType
)
;
}
/// <summary>
/// GZip压缩输出
/// </summary>
/// <param name="instance"></param>
/// <param name="responseType"></param>
public
static
void
GZipAndSend
(
byte
[
]
instance
,
string
responseType
)
{
CompressionUtils
.
Send
(
CompressionUtils
.
GZip
(
instance
)
,
responseType
)
;
}
/// <summary>
/// 输出内容
/// </summary>
/// <param name="instance"></param>
/// <param name="responseType"></param>
public
static
void
Send
(
byte
[
]
instance
,
string
responseType
)
{
HttpResponse
response
=
HttpContext
.
Current
.
Response
;
response
.
AppendHeader
(
"Content-Encoding"
,
"gzip"
)
;
response
.
Charset
=
"utf-8"
;
response
.
ContentType
=
responseType
;
response
.
BinaryWrite
(
instance
)
;
}
/// <summary>
/// GZip压缩
/// </summary>
public
static
byte
[
]
GZip
(
string
instance
)
{
return
GZip
(
Encoding
.
UTF8
.
GetBytes
(
instance
)
)
;
}
/// <summary>
/// GZip压缩
/// </summary>
public
static
byte
[
]
GZip
(
byte
[
]
instance
)
{
MemoryStream
stream
=
new
MemoryStream
(
)
;
GZipStream
zipstream
=
new
GZipStream
(
stream
,
CompressionMode
.
Compress
)
;
zipstream
.
Write
(
instance
,
0
,
instance
.
Length
)
;
zipstream
.
Close
(
)
;
return
stream
.
ToArray
(
)
;
}
/// <summary>
/// 是否支持GZip压缩
/// </summary>
public
static
bool
IsGZipSupported
{
get
{
HttpRequest
request
=
HttpContext
.
Current
.
Request
;
bool
ie6
=
request
.
Browser
.
IsBrowser
(
"IE"
)
&&
request
.
Browser
.
MajorVersion
<=
6
;
string
encoding
=
(
request
.
Headers
[
"Accept-Encoding"
]
??
string
.
Empty
)
.
ToLowerInvariant
(
)
;
return
!
ie6
&&
encoding
.
Contains
(
"gzip"
,
"deflate"
)
;
}
}
}
}
Back
|
FazBrowse Home
|
New Git URL