110,953
社区成员
发帖
与我相关
我的任务
分享
public static Stream CompressStream(Stream stream)
{
if (stream == null)
{
throw new ArgumentNullException("stream");
}
//压缩流
GZipStream gzipStream = new GZipStream(stream, CompressionMode.Compress,true);
//将缓冲区中的数据刷新到基础流
gzipStream.Flush();
//返回压缩流对象
return gzipStream;
}
/// <summary>
/// 解压流操作
/// </summary>
/// <param name="stream">需要解压的流对象</param>
/// <returns>返回解压GZipStream流对象</returns>
public static Stream DecompressStream(Stream stream)
{
if (stream == null)
{
throw new ArgumentNullException("stream");
}
GZipStream gzipStream = new GZipStream(stream, CompressionMode.Decompress);
gzipStream.Flush();
return gzipStream;
}
//创建一个编码对象
UTF8Encoding uniEncoding = new UTF8Encoding();
//将字符串编码成字节序列
byte[] firstString = uniEncoding.GetBytes("Invalid file path characters are: ");
MemoryStream memoryStream = new MemoryStream();
// 将字节数组写入到当前流中.
memoryStream.Write(firstString, 0, firstString.Length);
#region 压缩解压object
public static byte[] CompressionObject(object DataOriginal)
{
if (DataOriginal == null) return null;
BinaryFormatter bFormatter = new BinaryFormatter();
MemoryStream mStream = new MemoryStream();
bFormatter.Serialize(mStream, DataOriginal);
byte[] bytes = mStream.ToArray();
MemoryStream oStream = new MemoryStream();
DeflateStream zipStream = new DeflateStream(oStream, CompressionMode.Compress);
zipStream.Write(bytes, 0, bytes.Length);
zipStream.Flush();
zipStream.Close();
return oStream.ToArray();
}
public static object DecompressionObject(byte[] bytes)
{
if (bytes == null) return null;
MemoryStream mStream = new MemoryStream(bytes);
mStream.Seek(0, SeekOrigin.Begin);
DeflateStream unZipStream = new DeflateStream(mStream, CompressionMode.Decompress, true);
object dsResult = null;
BinaryFormatter bFormatter = new BinaryFormatter();
dsResult = (object)bFormatter.Deserialize(unZipStream);
return dsResult;
}
#endregion