我正在使用以下代码压缩 2 个字符串,它在压缩中效果很好。但是,当我尝试解压缩第二个字符串并且如果第一个字符串很短(大约 4 个字符)时,我得到块长度与其补码异常不匹配。这是我用于压缩的类:
using System;
using System.IO;
using System.IO.Compression;
using System.Text;
namespace CompressString {
internal static class StringCompressor
{
/// <summary>
/// Compresses the string.
/// </summary>
/// <param name="text">The text.</param>
/// <returns></returns>
public static string CompressString(string value)
{
//Transform string into byte[]
byte[] byteArray = new byte[value.Length];
int indexBA = 0;
foreach (char item in value.ToCharArray())
{
byteArray[indexBA++] = (byte)item;
}
//Prepare for compress
System.IO.MemoryStream ms = new System.IO.MemoryStream();
System.IO.Compression.GZipStream sw = new System.IO.Compression.GZipStream(ms,
System.IO.Compression.CompressionMode.Compress);
//Compress
sw.Write(byteArray, 0, byteArray.Length);
//Close, DO NOT FLUSH cause bytes will go missing...
sw.Close();
//Transform byte[] zip data to string
byteArray = ms.ToArray();
System.Text.StringBuilder sB = new System.Text.StringBuilder(byteArray.Length);
foreach (byte item in byteArray)
{
sB.Append((char)item);
}
ms.Close();
sw.Dispose();
ms.Dispose();
return sB.ToString();
}
/// <summary>
/// Decompresses the string.
/// </summary>
/// <param name="compressedText">The compressed text.</param>
/// <returns></returns>
public static string DecompressString(string sData)
{
byte[] byteArray = new byte[sData.Length];
int indexBa = 0;
foreach (char item in sData)
byteArray[indexBa++] = (byte)item;
MemoryStream memoryStream = new MemoryStream(byteArray);
GZipStream gZipStream = new GZipStream(memoryStream, CompressionMode.Decompress);
byteArray = new byte[1024];
StringBuilder stringBuilder = new StringBuilder();
int readBytes;
while ((readBytes = gZipStream.Read(byteArray, 0, byteArray.Length)) != 0)
{
for (int i = 0; i < readBytes; i++) stringBuilder.Append((char)byteArray[i]);
} gZipStream.Close(); memoryStream.Close(); gZipStream.Dispose(); memoryStream.Dispose(); return stringBuilder.ToString();
}
}
}
我在以下行中的 DecompressString 方法出现异常:
while ((readBytes = gZipStream.Read(byteArray, 0, byteArray.Length)) != 0)