今天搞了一晚上压缩和解压缩问题,java压缩的字符串,用C#始终没解开,后来考虑到实际的应用,还是数据库存储压力,不适合存储压缩后的长字符串,决定去掉压缩,用明文,在其他地方处理保密问题。
不过,今天找了一个很好用的压缩/解压缩方法,首先需要去http://www.icsharpcode.net/下载SharpZipLib,然后引用ICSharpCode.SharpZipLib.dll
类如下,可以直接使用。
[csharp]usingSystem;usingSystem.Text;usingSystem.IO;usingICSharpCode.SharpZipLib.BZip2;usingICSharpCode.SharpZipLib.GZip;usingICSharpCode.SharpZipLib.Zip;namespacehenry24264.com.Common
{///
///压缩方式。///
public enumCompressionType
{///
///GZip 压缩格式///
GZip,///
///BZip2 压缩格式///
BZip2,///
///Zip 压缩格式///
Zip
}///
///使用 SharpZipLib 进行压缩的辅助类,简化对字节数组和字符串进行压缩的操作。///
public classCompressionHelper
{///
///压缩供应者,默认为 GZip。///
public static CompressionType CompressionProvider =CompressionType.GZip;///
///从原始字节数组生成已压缩的字节数组。///
/// 原始字节数组。
/// 返回已压缩的字节数组
public static byte[] Compress(byte[] bytesToCompress)
{
MemoryStream ms= newMemoryStream();
Stream s=OutputStream(ms);
s.Write(bytesToCompress,0, bytesToCompress.Length);
s.Close();returnms.ToArray();
}///
///从原始字符串生成已压缩的字符串。///
/// 原始字符串。
/// 返回已压缩的字符串。
public static string Compress(stringstringToCompress)
{byte[] compressedData =CompressToByte(stringToCompress);string strOut =Convert.ToBase64String(compressedData);returnstrOut;
}///
///从原始字符串生成已压缩的字节数组。///
/// 原始字符串。
/// 返回已压缩的字节数组。
public static byte[] CompressToByte(stringstringToCompress)
{byte[] bytData =Encoding.Unicode.GetBytes(stringToCompress);returnCompress(bytData);
}///
///从已压缩的字符串生成原始字符串。///
/// 已压缩的字符串。
/// 返回原始字符串。
public string DeCompress(stringstringToDecompress)
{string outString = string.Empty;if (stringToDecompress == null)
{throw new ArgumentNullException("stringToDecompress", "You tried to use an empty string");
}try{byte[] inArr =Convert.FromBase64String(stringToDecompress.Trim());
outString=Encoding.Unicode.GetString(DeCompress(inArr));
}catch(NullReferenceException nEx)
{returnnEx.Message;
}returnoutString;
}///
///从已压缩的字节数组生成原始字节数组。///
/// 已压缩的字节数组。
/// 返回原始字节数组。
public static byte[] DeCompress(byte[] bytesToDecompress)
{byte[] writeData = new byte[4096];
Stream s2= InputStream(newMemoryStream(bytesToDecompress));
MemoryStream outStream= newMemoryStream();while (true)
{int size = s2.Read(writeData, 0, writeData.Length);if (size > 0)
{
outStream.Write(writeData,0, size);
}else{break;
}
}
s2.Close();byte[] outArr =outStream.ToArray();
outStream.Close();returnoutArr;
}///
///从给定的流生成压缩输出流。///
/// 原始流。
/// 返回压缩输出流。
private staticStream OutputStream(Stream inputStream)
{switch(CompressionProvider)
{caseCompressionType.BZip2:return newBZip2OutputStream(inputStream);caseCompressionType.GZip:return newGZipOutputStream(inputStream);caseCompressionType.Zip:return newZipOutputStream(inputStream);default:return newGZipOutputStream(inputStream);
}
}///
///从给定的流生成压缩输入流。///
/// 原始流。
/// 返回压缩输入流。
private staticStream InputStream(Stream inputStream)
{switch(CompressionProvider)
{caseCompressionType.BZip2:return newBZip2InputStream(inputStream);caseCompressionType.GZip:return newGZipInputStream(inputStream);caseCompressionType.Zip:return newZipInputStream(inputStream);default:return newGZipInputStream(inputStream);
}
}
}
}
[/csharp]