private static BinaryReader Decompress(byte[] buffer, int expectedSize = 0) { if (buffer == null) { throw new ArgumentNullException("buffer"); } var output = new MemoryStream(expectedSize); try { using (var inflater = new Inflater()) { inflater.DataAvailable += output.Write; inflater.Add(buffer, 0, buffer.Length); } output.Position = 0; return(new BinaryReader(output)); } catch { output.Dispose(); throw; } }
public void Inflate_Expand() { uncompressedData.Clear(); using (Inflater inf = new Inflater()) { inf.DataAvailable += new DataAvailableHandler(DDataAvail); inf.Add((byte[])compressedData.ToArray(typeof(byte))); inf.Finish(); adler2 = inf.Checksum; } Assert.AreEqual(adler1, adler2); }
/// <summary> /// Decompress a stream with ZLib /// </summary> /// <param name="compressLevel">Compression level of stream</param> /// <param name="expectedSize">Expected size</param> public static void Decompress(out CompressLevel compressLevel, int expectedSize = 0) { uint numBytesAddressing = ZLibWrapper.InputBufferLength; compressLevel = CompressLevel.None; try { ZLibWrapper.Position(0, ZLibBufferType.InputBuffer); compressLevel = RetrieveCompressionLevel(); ZLibWrapper.Position(0, ZLibBufferType.InputBuffer); if (expectedSize == 0) { // no decompression. copy input buffer in output buffer ZLibWrapper.CopyInputBufferToOutputBuffer(8); compressLevel = CompressLevel.None; // for compression } else { if (_inflater == null) { _inflater = new Inflater(); _inflater.DataAvailable += ZLibWrapper.WriteInOutputBuffer; } while (numBytesAddressing > 0u) { uint numBytes = Math.Min(numBytesAddressing, 8192u); //8192u); 65536u _inflater.Add(ZLibWrapper.ReadBytes((int)numBytes, ZLibBufferType.InputBuffer)); numBytesAddressing -= numBytes; } string inflateErrorMsg = _inflater._ztream.msg; if (!string.IsNullOrWhiteSpace(inflateErrorMsg)) { MessageBox.Show(string.Format("ZLib.Decompress: {0}", inflateErrorMsg), TranslateUI.TranslateUiGlobalization.ResManager.GetString("ZLib_Error"), MessageBoxButtons.OK, MessageBoxIcon.Error); } _inflater.Finish(); //flush zlib buffer } ZLibWrapper.Position(0, ZLibBufferType.OutputBuffer); } catch (Exception ex) { MessageBox.Show( string.Format(TranslateUI.TranslateUiGlobalization.ResManager.GetString(name: "MSG_ErrorWithNewLine"), ex), TranslateUI.TranslateUiGlobalization.ResManager.GetString(name: "ZLib_Decompress"), MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { if (ZLibWrapper.OutputBufferLength == 0) { MessageBox.Show( TranslateUI.TranslateUiGlobalization.ResManager.GetString(name: "ZLib_OutputBufferEmpty"), TranslateUI.TranslateUiGlobalization.ResManager.GetString(name: "ZLib_Decompress"), MessageBoxButtons.OK, MessageBoxIcon.Error); } } }