static MemoryStream Deflate(byte[] data)
{
MemoryStream memoryStream = new MemoryStream();
using (GZipStream gzip = new GZipStream(memoryStream, CompressionMode.Compress, true))
{
gzip.Write(data, 0, data.Length);
gzip.Flush();
gzip.Close();
}
return memoryStream;
}
static MemoryStream DeflateUseSharpZipLib(byte[] data)
{
MemoryStream memoryStream = new MemoryStream();
using (GZipOutputStream outStream = new GZipOutputStream(memoryStream))
{
outStream.IsStreamOwner = false;
outStream.Write(data, 0, data.Length);
outStream.Flush();
outStream.Finish();
}
return memoryStream;
}
public override void Write(byte[] array, int offset, int count)
{
if (this.deflateStream == null)
{
throw new ObjectDisposedException(null, SR.GetString("ObjectDisposed_StreamClosed"));
}
this.deflateStream.Write(array, offset, count);
}
public override void Write(byte[] array, int offset, int count)
{
this.EnsureCompressionMode();
this.ValidateParameters(array, offset, count);
this.InternalWrite(array, offset, count, false);
}
internal void InternalWrite(byte[] array, int offset, int count, bool isAsync)
{
int deflateOutput;
while (!this.deflater.NeedsInput())
{
deflateOutput = this.deflater.GetDeflateOutput(this.buffer);
if (deflateOutput != 0)
{
if (isAsync)
{
IAsyncResult asyncResult = this._stream.BeginWrite(this.buffer, 0, deflateOutput, null, null);
this._stream.EndWrite(asyncResult);
}
else
{
this._stream.Write(this.buffer, 0, deflateOutput);
}
}
}
this.deflater.SetInput(array, offset, count);
while (!this.deflater.NeedsInput())
{
deflateOutput = this.deflater.GetDeflateOutput(this.buffer);
if (deflateOutput != 0)
{
if (isAsync)
{
IAsyncResult result2 = this._stream.BeginWrite(this.buffer, 0, deflateOutput, null, null);
this._stream.EndWrite(result2);
}
else
{
this._stream.Write(this.buffer, 0, deflateOutput);
}
}
}
}
public int GetDeflateOutput(byte[] output)
{
return this.encoder.GetCompressedOutput(output);
}
public int GetCompressedOutput(byte[] outputBuffer)
{
this.output.UpdateBuffer(outputBuffer);
if (this.usingGzip && !this.hasGzipHeader)
{
this.output.WriteGzipHeader(3);
this.hasGzipHeader = true;
}
if (!this.hasBlockHeader)
{
this.hasBlockHeader = true;
this.output.WritePreamble();
}
do
{
int count = (this.inputBuffer.Count < this.inputWindow.FreeWindowSpace) ? this.inputBuffer.Count : this.inputWindow.FreeWindowSpace;
if (count > 0)
{
this.inputWindow.CopyBytes(this.inputBuffer.Buffer, this.inputBuffer.StartIndex, count);
if (this.usingGzip)
{
this.gzipCrc32 = DecodeHelper.UpdateCrc32(this.gzipCrc32, this.inputBuffer.Buffer, this.inputBuffer.StartIndex, count);
uint num2 = this.inputStreamSize + ((uint) count);
if (num2 < this.inputStreamSize)
{
throw new InvalidDataException(SR.GetString("StreamSizeOverflow"));
}
this.inputStreamSize = num2;
}
this.inputBuffer.ConsumeBytes(count);
}
while ((this.inputWindow.BytesAvailable > 0) && this.output.SafeToWriteTo())
{
this.inputWindow.GetNextSymbolOrMatch(this.currentMatch);
if (this.currentMatch.State == MatchState.HasSymbol)
{
this.output.WriteChar(this.currentMatch.Symbol);
}
else
{
if (this.currentMatch.State == MatchState.HasMatch)
{
this.output.WriteMatch(this.currentMatch.Length, this.currentMatch.Position);
continue;
}
this.output.WriteChar(this.currentMatch.Symbol);
this.output.WriteMatch(this.currentMatch.Length, this.currentMatch.Position);
}
}
}
while (this.output.SafeToWriteTo() && !this.NeedsInput());
this.needsEOB = true;
return this.output.BytesWritten;
}
internal void WriteGzipHeader(int compression_level)
{
this.outputBuf[this.outputPos++] = 0x1f;
this.outputBuf[this.outputPos++] = 0x8b;
this.outputBuf[this.outputPos++] = 8;
this.outputBuf[this.outputPos++] = 0;
this.outputBuf[this.outputPos++] = 0;
this.outputBuf[this.outputPos++] = 0;
this.outputBuf[this.outputPos++] = 0;
this.outputBuf[this.outputPos++] = 0;
if (compression_level == 10)
{
this.outputBuf[this.outputPos++] = 2;
}
else
{
this.outputBuf[this.outputPos++] = 4;
}
this.outputBuf[this.outputPos++] = 0;
}来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/12639172/viewspace-608035/ ,如需转载,请注明出处,否则将追究法律责任。