/// <summary> /// 将解压缩后的数据写入了指定的字节数组中 /// </summary> /// <param name="buffer">将要压缩的源字节数组</param> /// <param name="srcBuffOffset">源数组的起始位置偏移</param> /// <param name="srcBuffCount">源数组中要压缩的数据的数量</param> /// <param name="outPutBuff">用于输出的缓冲区</param> /// <param name="outPutBuffOffset">输出缓冲区的起始位置偏移</param> /// <returns>返回解压缩后的数据长度</returns> public static int UnCompressToByteArray(this byte[] buffer, int srcBuffOffset, int srcBuffCount, byte[] outPutBuff, int outPutBuffOffset) { if (outPutBuff == null || outPutBuff.Length <= 0 || outPutBuffOffset >= outPutBuff.Length) { throw new ArgumentOutOfRangeException("outPutBuff"); } using (var bs = new ByteStream(buffer, srcBuffOffset, srcBuffCount)) using (var cs = bs.UnCompressStream()) using (var outStream = new ByteStream(outPutBuff, outPutBuffOffset)) { return(StreamAbout.CopyTo(cs, outStream)); } }
/// <summary> /// 解压缩数据流中的数据 /// </summary> /// <param name="ms">要解压缩的数据流</param> /// <param name="type">解压缩使用的算法</param> /// <returns>返回解压缩后的数据流</returns> public static Stream UnCompressStream(this Stream ms, CompressType type) { Stream compressStream = null; MemoryStream resultMs = null; if (ms.CanSeek) { resultMs = new MemoryStream((int)ms.Length); ms.Seek(0, SeekOrigin.Begin); } else { resultMs = new MemoryStream(4096); } switch (type) { case CompressType.Zip_GZip: compressStream = new GZipStream(ms, CompressionMode.Decompress, true); StreamAbout.CopyTo(compressStream, resultMs); compressStream.Flush(); compressStream.Close(); break; case CompressType.Zip_Deflate: compressStream = new DeflateStream(ms, CompressionMode.Decompress, true); StreamAbout.CopyTo(compressStream, resultMs); compressStream.Flush(); compressStream.Close(); break; default: return(ms); } resultMs.Seek(0, SeekOrigin.Begin); return(resultMs); }
/// <summary> /// 压缩数据流 /// </summary> /// <param name="ms">要压缩的数据流</param> /// <param name="type">压缩使用的算法</param> /// <returns>返回压缩后的数据流</returns> public static Stream CompressStream(this Stream ms, CompressType type) { Stream compressStream = null; var resultMs = new MemoryStream(4096); if (ms.CanSeek) { ms.Seek(0, SeekOrigin.Begin); } switch (type) { case CompressType.Zip_GZip: compressStream = new GZipStream(resultMs, CompressionMode.Compress, true); StreamAbout.CopyTo(ms, compressStream); compressStream.Flush(); compressStream.Close(); break; case CompressType.Zip_Deflate: compressStream = new DeflateStream(resultMs, CompressionMode.Compress, true); StreamAbout.CopyTo(ms, compressStream); compressStream.Flush(); compressStream.Close(); break; default: return(ms); } resultMs.Seek(0, SeekOrigin.Begin); return(resultMs); }