/// <summary> /// Compress a byte array into a new byte array using DEFLATE. /// </summary> /// /// <remarks> /// Uncompress it with <see cref="DeflateStream.UncompressBuffer(byte[])"/>. /// </remarks> /// /// <seealso cref="DeflateStream.CompressString(string)">DeflateStream.CompressString(string)</seealso> /// <seealso cref="DeflateStream.UncompressBuffer(byte[])">DeflateStream.UncompressBuffer(byte[])</seealso> /// <seealso cref="GZipStream.CompressBuffer(byte[])">GZipStream.CompressBuffer(byte[])</seealso> /// <seealso cref="ZlibStream.CompressBuffer(byte[])">ZlibStream.CompressBuffer(byte[])</seealso> /// /// <param name="b"> /// A buffer to compress. /// </param> /// /// <returns>The data in compressed form</returns> public static byte[] CompressBuffer(byte[] b) { using (var ms = new System.IO.MemoryStream()) { System.IO.Stream compressor = new DeflateStream( ms, CompressionMode.Compress, CompressionLevel.BestCompression ); ZlibBaseStream.CompressBuffer(b, compressor); return ms.ToArray(); } }
/// <summary> /// Uncompress a DEFLATE'd byte array into a byte array. /// </summary> /// /// <seealso cref="DeflateStream.CompressBuffer(byte[])">DeflateStream.CompressBuffer(byte[])</seealso> /// <seealso cref="DeflateStream.UncompressString(byte[])">DeflateStream.UncompressString(byte[])</seealso> /// <seealso cref="GZipStream.UncompressBuffer(byte[])">GZipStream.UncompressBuffer(byte[])</seealso> /// <seealso cref="ZlibStream.UncompressBuffer(byte[])">ZlibStream.UncompressBuffer(byte[])</seealso> /// /// <param name="compressed"> /// A buffer containing data that has been compressed with DEFLATE. /// </param> /// /// <returns>The data in uncompressed form</returns> public static byte[] UncompressBuffer(byte[] compressed) { using (var input = new System.IO.MemoryStream(compressed)) { System.IO.Stream decompressor = new DeflateStream( input, CompressionMode.Decompress ); return ZlibBaseStream.UncompressBuffer(compressed, decompressor); } }
private Stream MaybeApplyCompression(Stream s, long streamLength) { if (_CompressionMethod == 0x08 && CompressionLevel != Ionic.Zlib.CompressionLevel.None) { #if !NETCF // ParallelDeflateThreshold == 0 means ALWAYS use parallel deflate // ParallelDeflateThreshold == -1L means NEVER use parallel deflate // Other values specify the actual threshold. if (_container.ParallelDeflateThreshold == 0L || (streamLength > _container.ParallelDeflateThreshold && _container.ParallelDeflateThreshold > 0L)) { // This is sort of hacky. // // It's expensive to create a ParallelDeflateOutputStream, because // of the large memory buffers. But the class is unlike most Stream // classes in that it can be re-used, so the caller can compress // multiple files with it, one file at a time. The key is to call // Reset() on it, in between uses. // // The ParallelDeflateOutputStream is attached to the container // itself - there is just one for the entire ZipFile or // ZipOutputStream. So it gets created once, per save, and then // re-used many times. // // This approach will break when we go to a "parallel save" // approach, where multiple entries within the zip file are being // compressed and saved at the same time. But for now it's ok. // // instantiate the ParallelDeflateOutputStream if (_container.ParallelDeflater == null) { _container.ParallelDeflater = new ParallelDeflateOutputStream(s, CompressionLevel, _container.Strategy, true); // can set the codec buffer size only before the first call to Write(). if (_container.CodecBufferSize > 0) _container.ParallelDeflater.BufferSize = _container.CodecBufferSize; if (_container.ParallelDeflateMaxBufferPairs > 0) _container.ParallelDeflater.MaxBufferPairs = _container.ParallelDeflateMaxBufferPairs; } // reset it with the new stream Ionic.Zlib.ParallelDeflateOutputStream o1 = _container.ParallelDeflater; o1.Reset(s); return o1; } #endif var o = new DeflateStream(s, OfficeOpenXml.Packaging.Ionic.Zlib.CompressionMode.Compress, CompressionLevel, true); if (_container.CodecBufferSize > 0) o.BufferSize = _container.CodecBufferSize; o.Strategy = _container.Strategy; return o; } #if BZIP if (_CompressionMethod == 0x0c) { #if !NETCF if (_container.ParallelDeflateThreshold == 0L || (streamLength > _container.ParallelDeflateThreshold && _container.ParallelDeflateThreshold > 0L)) { var o1 = new Ionic.BZip2.ParallelBZip2OutputStream(s, true); return o1; } #endif var o = new Ionic.BZip2.BZip2OutputStream(s, true); return o; } #endif return s; }