Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
N
netcoreplus
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
tsai
netcoreplus
Commits
318b5cb7
Commit
318b5cb7
authored
Jul 19, 2019
by
阿星Plus
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Compresses and DeCompresses the string
parent
dff51e45
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
45 additions
and
0 deletions
+45
-0
Extensions.cs
src/Plus.Extensions/Extensions.cs
+45
-0
No files found.
src/Plus.Extensions/Extensions.cs
View file @
318b5cb7
...
...
@@ -6,6 +6,7 @@ using System.Data;
using
System.Dynamic
;
using
System.Globalization
;
using
System.IO
;
using
System.IO.Compression
;
using
System.Linq
;
using
System.Linq.Expressions
;
using
System.Reflection
;
...
...
@@ -516,10 +517,54 @@ public static class Extensions
}
}
/// <summary>
/// Compresses the string
/// </summary>
/// <param name="this"></param>
/// <returns></returns>
public
static
string
Compress
(
this
string
@this
)
{
var
buffer
=
Encoding
.
UTF8
.
GetBytes
(
@this
);
var
memoryStream
=
new
MemoryStream
();
using
(
var
gzipStream
=
new
GZipStream
(
memoryStream
,
CompressionMode
.
Compress
,
true
))
{
gzipStream
.
Write
(
buffer
,
0
,
buffer
.
Length
);
}
memoryStream
.
Position
=
0
;
var
compressedData
=
new
byte
[
memoryStream
.
Length
];
memoryStream
.
Read
(
compressedData
,
0
,
compressedData
.
Length
);
var
gzipBuffer
=
new
byte
[
compressedData
.
Length
+
4
];
Buffer
.
BlockCopy
(
compressedData
,
0
,
gzipBuffer
,
4
,
compressedData
.
Length
);
Buffer
.
BlockCopy
(
BitConverter
.
GetBytes
(
buffer
.
Length
),
0
,
gzipBuffer
,
0
,
4
);
return
Convert
.
ToBase64String
(
gzipBuffer
);
}
#
endregion
#
region
D
/// <summary>
/// Decompresses the string
/// </summary>
/// <param name="this"></param>
/// <returns></returns>
public
static
string
DeCompress
(
this
string
@this
)
{
byte
[]
gzipBuffer
=
Convert
.
FromBase64String
(
@this
);
using
(
var
memoryStream
=
new
MemoryStream
())
{
var
dataLength
=
BitConverter
.
ToInt32
(
gzipBuffer
,
0
);
memoryStream
.
Write
(
gzipBuffer
,
4
,
gzipBuffer
.
Length
-
4
);
var
buffer
=
new
byte
[
dataLength
];
memoryStream
.
Position
=
0
;
using
(
var
gzipStream
=
new
GZipStream
(
memoryStream
,
CompressionMode
.
Decompress
))
{
gzipStream
.
Read
(
buffer
,
0
,
buffer
.
Length
);
}
return
Encoding
.
UTF8
.
GetString
(
buffer
);
}
}
/// <summary>
/// DecodeBase64
/// </summary>
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment