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
|
#nullable disable
using System;
using System.IO;
using System.IO.Compression;
using System.Text;
namespace StardewModdingAPI.Web.Framework.Compression
{
/// <summary>Handles GZip compression logic.</summary>
internal class GzipHelper : IGzipHelper
{
/*********
** Fields
*********/
/// <summary>The first bytes in a valid zip file.</summary>
/// <remarks>See <a href="https://en.wikipedia.org/wiki/Zip_(file_format)#File_headers"/>.</remarks>
private const uint GzipLeadBytes = 0x8b1f;
/*********
** Public methods
*********/
/// <summary>Compress a string.</summary>
/// <param name="text">The text to compress.</param>
/// <remarks>Derived from <a href="https://stackoverflow.com/a/17993002/262123"/>.</remarks>
public string CompressString(string text)
{
// get raw bytes
byte[] buffer = Encoding.UTF8.GetBytes(text);
// compressed
byte[] compressedData;
using (MemoryStream stream = new())
{
using (GZipStream zipStream = new(stream, CompressionLevel.Optimal, leaveOpen: true))
zipStream.Write(buffer, 0, buffer.Length);
stream.Position = 0;
compressedData = new byte[stream.Length];
stream.Read(compressedData, 0, compressedData.Length);
}
// prefix length
byte[] zipBuffer = new byte[compressedData.Length + 4];
Buffer.BlockCopy(compressedData, 0, zipBuffer, 4, compressedData.Length);
Buffer.BlockCopy(BitConverter.GetBytes(buffer.Length), 0, zipBuffer, 0, 4);
// return string representation
return Convert.ToBase64String(zipBuffer);
}
/// <summary>Decompress a string.</summary>
/// <param name="rawText">The compressed text.</param>
/// <remarks>Derived from <a href="https://stackoverflow.com/a/17993002/262123"/>.</remarks>
public string DecompressString(string rawText)
{
// get raw bytes
byte[] zipBuffer;
try
{
zipBuffer = Convert.FromBase64String(rawText);
}
catch
{
return rawText; // not valid base64, wasn't compressed by the log parser
}
// skip if not gzip
if (BitConverter.ToUInt16(zipBuffer, 4) != GzipHelper.GzipLeadBytes)
return rawText;
// decompress
using MemoryStream memoryStream = new();
{
// read length prefix
int dataLength = BitConverter.ToInt32(zipBuffer, 0);
memoryStream.Write(zipBuffer, 4, zipBuffer.Length - 4);
// read data
byte[] buffer = new byte[dataLength];
memoryStream.Position = 0;
using (GZipStream gZipStream = new(memoryStream, CompressionMode.Decompress))
gZipStream.Read(buffer, 0, buffer.Length);
// return original string
return Encoding.UTF8.GetString(buffer);
}
}
}
}
|