forked from Hitachi-Momoka/krkrfgformat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLiZlib.cs
61 lines (56 loc) · 1.99 KB
/
LiZlib.cs
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
using System.IO;
using zlib;
namespace Li.Zlib
{
public class LiZlib
{
public static void CopyStream(Stream input, Stream output)
{
byte[] buffer = new byte[2000];
int count;
while ((count = input.Read(buffer, 0, 2000)) > 0)
{
output.Write(buffer, 0, count);
}
output.Flush();
}
public static byte[] compressBytes(byte[] sourceByte)
{
MemoryStream memoryStream = new MemoryStream(sourceByte);
Stream stream = compressStream(memoryStream);
byte[] array = new byte[stream.Length];
stream.Position = 0L;
stream.Read(array, 0, array.Length);
stream.Close();
memoryStream.Close();
return array;
}
public static byte[] deCompressBytes(byte[] sourceByte)
{
MemoryStream memoryStream = new MemoryStream(sourceByte);
Stream stream = deCompressStream(memoryStream);
byte[] array = new byte[stream.Length];
stream.Position = 0L;
stream.Read(array, 0, array.Length);
stream.Close();
memoryStream.Close();
return array;
}
public static Stream compressStream(Stream sourceStream)
{
MemoryStream memoryStream = new MemoryStream();
ZOutputStream zOutputStream = new ZOutputStream(memoryStream, -1);
CopyStream(sourceStream, zOutputStream);
zOutputStream.finish();
return memoryStream;
}
public static Stream deCompressStream(Stream sourceStream)
{
MemoryStream memoryStream = new MemoryStream();
ZOutputStream zOutputStream = new ZOutputStream(memoryStream);
CopyStream(sourceStream, zOutputStream);
zOutputStream.finish();
return memoryStream;
}
}
}