压缩字符串 C#
Deflate string C#
我试图缩小从 websocket 连接获得的字符串,但每次我尝试 运行 这段代码时,我都会收到错误消息。我还尝试使用 ascii 编码直接从字符串中获取字节,但它也没有用。
public static byte[] Decompress(string input)
{
byte[] data = Convert.FromBase64String(input)
byte[] decompressedArray = null;
using (MemoryStream decompressedStream = new MemoryStream())
{
using (MemoryStream compressStream = new MemoryStream(data))
{
using (DeflateStream deflateStream = new DeflateStream(compressStream, CompressionMode.Decompress))
{
deflateStream.CopyTo(decompressedStream);
}
}
decompressedArray = decompressedStream.ToArray();
}
return decompressedArray;
}
System.IO.InvalidDataException: "Block length does not match with its complement."
我尝试了此代码的几个不同变体,但没有任何效果,但我知道这是可能的,因为我可以使用此网站 http://www.txtwizard.net/compression 和 deflate 选项对其进行压缩。
我要放气的一个示例字符串:
eAGdVMmO2kAU/BXUc3WYXr1wy3LIKYqUjHKIo6jtboODsVG72YT491SbIWMYkcNgiX4uv6pXr7efxHXdatYvtDPs8Qf7wkh0JF3xx5a+J7MjUVnKK8tsQbOEJqyQ2pQ0kSJ8s6118wOZcSlpROp2q411n7Xb2t5bAzyO5SkiymjDdFLaSgpZJCZOK6NYTINE7ztnx1pMifg0kAwrK16mqTSWSl4VcWGVGkiLOniTjKJqa/f+ky314Xu9ghBXIpFZSoPEXK/sCyqylAWTVRfKrTqDbLLrXGMIcrd1v9ENkGNOfE5mOXE5iXKyRywSBAcEFOMOI+MIFghijD1GkKq6aRDlpGh0uRy4vXfd0g7gDpYRIb1b67L2g9qUq9Mpb/9VRF0keDQ0cD5+fZo89Xp+5gUjUuB7oDKMl7pdG9LpNL3Sas7uGT6JZEpVIIYXNg3hniOU6TQb8PAC/H9WNs7Z1t/YgfDAB50P0bMl3dTzFmBOGlvB3JWxmyb5VIrJ40Se7QZX8Vh07OleS2FSXjckxtSbou+31mFe77cjIfmmZlLMZbBz3Ygce7nXhryzMsDH9JtWPmzKpfWTbzhG450yWhr1tqXJsix93QnExmZGvcgwZ5ct9rLHBjRsMIXNBiqJyKa37vlqUYZJQ1NDVZEmlRA6sfgL5/N3jfsDl8+djLNKiwMeDrGvnG6XkC60mQ+3iT+sw2XAI1J2TecYsh60KbQtkDVAPEA8VpXJLhDuNPLAdHgArbXTKzJ7F4uIVE29JjPvNvaE36+/hpSDkQ ==
字符串 (JSON) 可能来自 GZipStream.
您可以使用其他流来解压缩数据内容:
► 您需要.Net Framework 4.7.2+ 才能正确解压数据。
请注意,压缩格式有点 off。压缩数据签名,78 : 01
,可能暗示这是一个ZLib压缩字符串,所以.Net DeflateStream,它基于ZLib(来自.NET Framework 4.5), 应该解压缩数据。
7-Zip 和 WinZip 都无法打开数据(保存为二进制文件的流)。
using System.IO;
using System.IO.Compression;
using System.Text;
public static byte[] Decompress(string input)
{
byte[] data = Convert.FromBase64String(input)
using (var decompressedStream = new MemoryStream())
using (var compressStream = new MemoryStream(data))
using (var gzip = new GZipStream(compressStream, CompressionMode.Decompress)) {
gzip.CopyTo(decompressedStream);
return decompressedStream.ToArray();
}
}
JSON 是 UTF8 编码的(它也适用于 ASCII 字符串):
string json = Encoding.UTF8.GetString(Decompress(base64String));
导致(.Net Fidddle):
[
"room:shard1/W1N1",
{
"objects":{
"5982fe1eb097071b4adc0743":{
"energy":2440,
"invaderHarvested":2664
},
"5dad1a7cef434b7d68fd5160":{
"store":{
"energy":1536
}
},
"5dd1cf2c884de042fb6be550":{
"hits":4100,
"nextDecayTime":25374980
}
},
"gameTime":25373981,
"info":{
"mode":"world"
},
"visual":"{\"t\":\"r\",\"x\":37,\"y\":0,\"w\":12,\"h\":6,\"s\":{\"fill\":\"black\",\"stroke\":\"white\",\"opacity\":0.25}}\n{\"t\":\"t\",\"text\":\"CPU Usage\",\"x\":43,\"y\":1,\"s\":{\"font\":0.8}}\n{\"t\":\"l\",\"x1\":37.05,\"y1\":1.5,\"x2\":48.95,\"y2\":1.5}\n{\"t\":\"t\",\"text\":\"Current Usage\",\"x\":37.5,\"y\":2.5,\"s\":{\"align\":\"left\"}}\n{\"t\":\"t\",\"text\":\"2.43 / 4\",\"x\":46.5,\"y\":2.5}\n{\"t\":\"l\",\"x1\":37.05,\"y1\":3,\"x2\":48.95,\"y2\":3}\n{\"t\":\"t\",\"text\":\"Average Usage\",\"x\":37.5,\"y\":4,\"s\":{\"align\":\"left\"}}\n{\"t\":\"t\",\"text\":\"2.82\",\"x\":46.5,\"y\":4}\n{\"t\":\"l\",\"x1\":37.05,\"y1\":4.5,\"x2\":48.95,\"y2\":4.5}\n{\"t\":\"t\",\"text\":\"Bucket Storage\",\"x\":37.5,\"y\":5.5,\"s\":{\"align\":\"left\"}}\n{\"t\":\"t\",\"text\":9998,\"x\":46.5,\"y\":5.5}\n{\"t\":\"l\",\"x1\":44,\"y1\":1.55,\"x2\":44,\"y2\":5.95}\n",
"users":{
"595d14d08d05b87f33a7e33a":{
"_id":"595d14d08d05b87f33a7e33a",
"username":"wtfrank",
"badge":{
"type":22,
"color1":"#adbaeb",
"color2":"#265fd9",
"color3":"#1a1a1a",
"param":-63,
"flip":true
}
}
}
}
]
我试图缩小从 websocket 连接获得的字符串,但每次我尝试 运行 这段代码时,我都会收到错误消息。我还尝试使用 ascii 编码直接从字符串中获取字节,但它也没有用。
public static byte[] Decompress(string input)
{
byte[] data = Convert.FromBase64String(input)
byte[] decompressedArray = null;
using (MemoryStream decompressedStream = new MemoryStream())
{
using (MemoryStream compressStream = new MemoryStream(data))
{
using (DeflateStream deflateStream = new DeflateStream(compressStream, CompressionMode.Decompress))
{
deflateStream.CopyTo(decompressedStream);
}
}
decompressedArray = decompressedStream.ToArray();
}
return decompressedArray;
}
System.IO.InvalidDataException: "Block length does not match with its complement."
我尝试了此代码的几个不同变体,但没有任何效果,但我知道这是可能的,因为我可以使用此网站 http://www.txtwizard.net/compression 和 deflate 选项对其进行压缩。
我要放气的一个示例字符串:
eAGdVMmO2kAU/BXUc3WYXr1wy3LIKYqUjHKIo6jtboODsVG72YT491SbIWMYkcNgiX4uv6pXr7efxHXdatYvtDPs8Qf7wkh0JF3xx5a+J7MjUVnKK8tsQbOEJqyQ2pQ0kSJ8s6118wOZcSlpROp2q411n7Xb2t5bAzyO5SkiymjDdFLaSgpZJCZOK6NYTINE7ztnx1pMifg0kAwrK16mqTSWSl4VcWGVGkiLOniTjKJqa/f+ky314Xu9ghBXIpFZSoPEXK/sCyqylAWTVRfKrTqDbLLrXGMIcrd1v9ENkGNOfE5mOXE5iXKyRywSBAcEFOMOI+MIFghijD1GkKq6aRDlpGh0uRy4vXfd0g7gDpYRIb1b67L2g9qUq9Mpb/9VRF0keDQ0cD5+fZo89Xp+5gUjUuB7oDKMl7pdG9LpNL3Sas7uGT6JZEpVIIYXNg3hniOU6TQb8PAC/H9WNs7Z1t/YgfDAB50P0bMl3dTzFmBOGlvB3JWxmyb5VIrJ40Se7QZX8Vh07OleS2FSXjckxtSbou+31mFe77cjIfmmZlLMZbBz3Ygce7nXhryzMsDH9JtWPmzKpfWTbzhG450yWhr1tqXJsix93QnExmZGvcgwZ5ct9rLHBjRsMIXNBiqJyKa37vlqUYZJQ1NDVZEmlRA6sfgL5/N3jfsDl8+djLNKiwMeDrGvnG6XkC60mQ+3iT+sw2XAI1J2TecYsh60KbQtkDVAPEA8VpXJLhDuNPLAdHgArbXTKzJ7F4uIVE29JjPvNvaE36+/hpSDkQ ==
字符串 (JSON) 可能来自 GZipStream.
您可以使用其他流来解压缩数据内容:
► 您需要.Net Framework 4.7.2+ 才能正确解压数据。
请注意,压缩格式有点 off。压缩数据签名,78 : 01
,可能暗示这是一个ZLib压缩字符串,所以.Net DeflateStream,它基于ZLib(来自.NET Framework 4.5), 应该解压缩数据。
7-Zip 和 WinZip 都无法打开数据(保存为二进制文件的流)。
using System.IO;
using System.IO.Compression;
using System.Text;
public static byte[] Decompress(string input)
{
byte[] data = Convert.FromBase64String(input)
using (var decompressedStream = new MemoryStream())
using (var compressStream = new MemoryStream(data))
using (var gzip = new GZipStream(compressStream, CompressionMode.Decompress)) {
gzip.CopyTo(decompressedStream);
return decompressedStream.ToArray();
}
}
JSON 是 UTF8 编码的(它也适用于 ASCII 字符串):
string json = Encoding.UTF8.GetString(Decompress(base64String));
导致(.Net Fidddle):
[
"room:shard1/W1N1",
{
"objects":{
"5982fe1eb097071b4adc0743":{
"energy":2440,
"invaderHarvested":2664
},
"5dad1a7cef434b7d68fd5160":{
"store":{
"energy":1536
}
},
"5dd1cf2c884de042fb6be550":{
"hits":4100,
"nextDecayTime":25374980
}
},
"gameTime":25373981,
"info":{
"mode":"world"
},
"visual":"{\"t\":\"r\",\"x\":37,\"y\":0,\"w\":12,\"h\":6,\"s\":{\"fill\":\"black\",\"stroke\":\"white\",\"opacity\":0.25}}\n{\"t\":\"t\",\"text\":\"CPU Usage\",\"x\":43,\"y\":1,\"s\":{\"font\":0.8}}\n{\"t\":\"l\",\"x1\":37.05,\"y1\":1.5,\"x2\":48.95,\"y2\":1.5}\n{\"t\":\"t\",\"text\":\"Current Usage\",\"x\":37.5,\"y\":2.5,\"s\":{\"align\":\"left\"}}\n{\"t\":\"t\",\"text\":\"2.43 / 4\",\"x\":46.5,\"y\":2.5}\n{\"t\":\"l\",\"x1\":37.05,\"y1\":3,\"x2\":48.95,\"y2\":3}\n{\"t\":\"t\",\"text\":\"Average Usage\",\"x\":37.5,\"y\":4,\"s\":{\"align\":\"left\"}}\n{\"t\":\"t\",\"text\":\"2.82\",\"x\":46.5,\"y\":4}\n{\"t\":\"l\",\"x1\":37.05,\"y1\":4.5,\"x2\":48.95,\"y2\":4.5}\n{\"t\":\"t\",\"text\":\"Bucket Storage\",\"x\":37.5,\"y\":5.5,\"s\":{\"align\":\"left\"}}\n{\"t\":\"t\",\"text\":9998,\"x\":46.5,\"y\":5.5}\n{\"t\":\"l\",\"x1\":44,\"y1\":1.55,\"x2\":44,\"y2\":5.95}\n",
"users":{
"595d14d08d05b87f33a7e33a":{
"_id":"595d14d08d05b87f33a7e33a",
"username":"wtfrank",
"badge":{
"type":22,
"color1":"#adbaeb",
"color2":"#265fd9",
"color3":"#1a1a1a",
"param":-63,
"flip":true
}
}
}
}
]