1
- using System . IO . Compression ;
1
+ using System . Buffers ;
2
+ using ICSharpCode . SharpZipLib . GZip ;
2
3
3
4
namespace GameFrameX . Utility ;
4
5
@@ -21,16 +22,13 @@ public static byte[] Compress(byte[] bytes)
21
22
return bytes ;
22
23
}
23
24
24
- using ( var uncompressed = new MemoryStream ( bytes ) )
25
+ using ( var compressStream = new MemoryStream ( ) )
25
26
{
26
- using ( var compressed = new MemoryStream ( ) )
27
+ using ( var gZipOutputStream = new GZipOutputStream ( compressStream ) )
27
28
{
28
- using ( var gZipStream = new GZipStream ( compressed , CompressionMode . Compress , true ) )
29
- {
30
- uncompressed . CopyTo ( gZipStream ) ;
31
- }
32
-
33
- return compressed . ToArray ( ) ;
29
+ gZipOutputStream . Write ( bytes , 0 , bytes . Length ) ;
30
+ var press = compressStream . ToArray ( ) ;
31
+ return press ;
34
32
}
35
33
}
36
34
}
@@ -50,16 +48,29 @@ public static byte[] Decompress(byte[] bytes)
50
48
return bytes ;
51
49
}
52
50
53
- using ( var compressed = new MemoryStream ( bytes ) )
51
+ using ( var compressedStream = new MemoryStream ( bytes ) )
54
52
{
55
- using ( var decompressed = new MemoryStream ( ) )
53
+ using ( var gZipInputStream = new GZipInputStream ( compressedStream ) )
56
54
{
57
- using ( var gZipStream = new GZipStream ( compressed , CompressionMode . Decompress ) )
55
+ using ( var decompressedStream = new MemoryStream ( ) )
58
56
{
59
- gZipStream . CopyTo ( decompressed ) ;
60
- }
57
+ var buffer = ArrayPool < byte > . Shared . Rent ( 8192 ) ;
58
+ try
59
+ {
60
+ int count ;
61
+ while ( ( count = gZipInputStream . Read ( buffer , 0 , buffer . Length ) ) != 0 )
62
+ {
63
+ decompressedStream . Write ( buffer , 0 , count ) ;
64
+ }
65
+ }
66
+ finally
67
+ {
68
+ ArrayPool < byte > . Shared . Return ( buffer ) ;
69
+ }
61
70
62
- return decompressed . ToArray ( ) ;
71
+ var array = decompressedStream . ToArray ( ) ;
72
+ return array ;
73
+ }
63
74
}
64
75
}
65
76
}
0 commit comments