C#字符串压缩
#region 压缩和解压字符串
public static string GZipCompressString(string rawString)
{
if (string.IsNullOrEmpty(rawString) || rawString.Length == 0)
{
return "";
}
else
{
byte[] rawData = System.Text.Encoding.UTF8.GetBytes(rawString.ToString());
byte[] zippedData = Compress(rawData);
return (string)(Convert.ToBase64String(zippedData));
}
}
static byte[] Compress(byte[] rawData)
{
System.IO.MemoryStream ms = new System.IO.MemoryStream();
System.IO.Compression.GZipStream compressedzipStream = new System.IO.Compression.GZipStream(ms, System.IO.Compression.CompressionMode.Compress, true);
compressedzipStream.Write(rawData, 0, rawData.Length);
compressedzipStream.Close();
return ms.ToArray();
}
public static string GetStringByString(string Value)
{
string CC = GZipDecompressString(Value);
return CC;
}
public static DataSet GetDatasetByString(string Value)
{
DataSet ds = new DataSet();
string CC = GZipDecompressString(Value);
System.IO.StringReader Sr = new System.IO.StringReader(CC);
ds.ReadXml(Sr);
return ds;
}
public static string GZipDecompressString(string zippedString)
{
if (string.IsNullOrEmpty(zippedString) || zippedString.Length == 0)
{
return "";
}
else
{
byte[] zippedData = Convert.FromBase64String(zippedString.ToString());
return (string)(System.Text.Encoding.UTF8.GetString(Decompress(zippedData)));
}
}
public static byte[] Decompress(byte[] zippedData)
{
System.IO.MemoryStream ms = new System.IO.MemoryStream(zippedData);
System.IO.Compression.GZipStream compressedzipStream = new System.IO.Compression.GZipStream(ms, System.IO.Compression.CompressionMode.Decompress);
System.IO.MemoryStream outBuffer = new System.IO.MemoryStream();
byte[] block = new byte[1024];
while (true)
{
int bytesRead = compressedzipStream.Read(block, 0, block.Length);
if (bytesRead <= 0)
break;
else
outBuffer.Write(block, 0, bytesRead);
}
compressedzipStream.Close();
return outBuffer.ToArray();
}
#endregion
转载自:www.cnblogs.com/sdusrz/p/13…