兔子党-督察 2017年09月22日
高分求 C# 长字符串 压缩/解压缩 算法
现有一随机字符串,长度在2000左右:

string sample = "MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQC9rGtAXTfglq2acKsGYDs+zLoW7LJbiU8D2cfrOzXFrgEX8AtwuyFOUiKsj7HwhbfcjRIdiMKR7sv1l0bzOxAlaGVDEnoSJ3tyDhnvC9Uqeh5vLc0s9LG+8/e6cUhdeTY0RUt23fGFgK8OIZ2eYkmNqTtoIV21C8xDBmjMYNS21jHgdw9KWQIqEyeBTXNeIVcZuP4r/gbtplhfnpCe7mqHmGZTygxn4r/fDPQ3IGx/sNmTu5i1npdw4fP2xZCjCkSTz106EmRM9TXypzIvDHi8ycFM/uByKyUuKkdq8fIQOijAgMg4VSXmaAzrvu2yJPIBYm1fYBwQCK090+ivf2avAgMBAAECggEAf0dWaUikmHdEY+C7Q3oMB6ZGMeAAB+DqPwFDYJzJBrAvV6rjYnCQdwgy8G000NxKdxvLTjpZpqgCAfnTyKCXwyJ84Tdi5w/LjMvdp0Xfc7Oi/KRVjJdfN25rjJc8Ik2WjBj7/PYOfrHNxsPUC1aVWRR5IvVQ9o7GMSv54zwPQejsdxJKteZ0bUhPOzgcNMfd6051nXLFRdLSYiiWRqGlEAD1lbZqOmCzF7QJz9OJh7wM2jtMF7ihw3REVFT/B3sE1pMGmb/3P3bLLhxDxKJnlPiO0sZ2/Gp4S9kmoBgPmYnBPKbXbVTfxZg07UCPc97Bc1rJuJOw1hdyrkeaotcMAQKBgQD5BrBrus0fOAnSoLv79O5GL3OxXo4+SZzAaB6/rC6wZvtFFkqm/3eJaDywfWjpMpPZ1VSxqd0kCEfK+jSqNK2+H9RNEm+oOAC3cbjWw4zBwt0Y4DxaEo6fMo1+gokgkns9oKexhgwTacFyUrvEKAWDTl2qAcpvb5k5DIiNWJeRAQKBgQDC/DiDK8oWuz55omQil6m7FQTDE3NUlogwhTDPT61kdGER+TFk0jey/JBi/0VjZ+eF5R98yx0xuQ+nf6wudWGiX5WbNF5yjZpPXu1wJkAK2abIX/ggfNMn73kNW01bL4aT0TpV2rGW3hKd9ydvadZ5jJYcDDuFSf2QxpSXqqxHrwKBgQDF7jUnS1BiMe5MxYjk2GbSzkCMh/VTOLsoixl1i2uItjGdVfx8A62FP56NQQCz6YluqIsqszKbwyEdCgX2CuzVowLhR3gMIocfR3p86OzlzPZjIUeW6A0IJ+wi06oeg48FCr5+8WaDv5kMPwoS/SR0m0MDL20xaWhF2dpnTjUaAQKBgQC5Q+l2SN8lphgAfpnifHRbO+dga1TD5JvWblcoQ66eqi2pZDrYbx1ZRbzzM1V81DcZ89BtRJiirBIBtr+lDQcNvwBpjeLHuWALVkkIrG9hX9imvvkF9VS0t0cvt7bSk1+th7mD5d2jWbIawcGIjOmqaDggwkazqM/zBZweV56GJQKBgQCEFPU4tshsObee1IngYqcBlOygw0LmBUhbImayzOoUpw/NhNKQHKPyxpa4ylOdlwpXHis4MyjoqwxnZE+znrNYN8kyVP4GbZ6c2YnY4+YYu/IpRythNuivaIh+ZkJgZqLYZ+5h0MYwoQGem1/pJPdaeaaIDRsbYlhwiGwW2IXmvQ==";


需要一算法,将此字符串长度压缩到1000内,并且可以再解锁回原字符串

尝试过 GZipStream 等.NET framework自带的类,没有达到目标,反而长度越压缩越长...
尝试换不同的Encoding也不行,反而长度越压缩越长...

以下是没达到要求的代码:

 public static class Extensions
{
static System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();

/// <summary>
/// Compresses a string and returns a deflate compressed, Base64 encoded string.
/// </summary>
/// <param name="uncompressedString">String to compress</param>
public static string Compress(this string uncompressedString)
{
var compressedStream = new MemoryStream();
var uncompressedStream = new MemoryStream(encoding.GetBytes(uncompressedString));

using (var compressorStream = new GZipStream(compressedStream, CompressionMode.Compress, true))
{
uncompressedStream.CopyTo(compressorStream);
}

return Convert.ToBase64String(compressedStream.ToArray());
}

/// <summary>
/// Decompresses a deflate compressed, Base64 encoded string and returns an uncompressed string.
/// </summary>
/// <param name="compressedString">String to decompress.</param>
public static string Decompress(this string compressedString)
{
var decompressedStream = new MemoryStream();
var compressedStream = new MemoryStream(Convert.FromBase64String(compressedString));

using (var decompressorStream = new GZipStream(compressedStream, CompressionMode.Decompress, false))
{
decompressorStream.CopyTo(decompressedStream);
}

return encoding.GetString(decompressedStream.ToArray());
}
}
...全文
2268 点赞 收藏 34
写回复
34 条回复

还没有回复,快来抢沙发~

发动态
发帖子
C#
创建于2007-09-28

8.4w+

社区成员

64.0w+

社区内容

.NET技术 C#
社区公告
暂无公告