private const int CopyBufferSize = 32*1024; // 32kb public void Compress(Stream source, Stream destination) { /* var deflater = new DeflaterOutputStream(destination, new Deflater(Deflater.DEFAULT_COMPRESSION, true)); var dataBuffer = new byte[CopyBufferSize]; StreamUtils.Copy(source, deflater, dataBuffer); */ var def = new Deflater(Deflater.DEFAULT_COMPRESSION, true); var inputData = new byte[source.Length - source.Position]; source.Read(inputData, 0, inputData.Length); var buffer = new byte[CopyBufferSize]; def.SetInput( inputData, 0, inputData.Length ); def.Finish(); while(!def.IsFinished) { int outputLen = def.Deflate(buffer, 0, buffer.Length); destination.Write( buffer, 0, outputLen ); } def.Reset(); }
public void Compress() { var deflater = new Deflater(); byte[] packet = PacketData; deflater.SetInput(packet, 0, packet.Length); deflater.Finish(); var compBuffer = new byte[1024]; var ret = new List<byte>(); while (!deflater.IsFinished) { try { deflater.Deflate(compBuffer); ret.AddRange(compBuffer); Array.Clear(compBuffer, 0, compBuffer.Length); } catch (Exception ex) { return; } } deflater.Reset(); Seek((byte)_headerType, SeekOrigin.Begin); // Write the compressed bytes over whatever is there. Write(ret.ToArray()); // Set the stream length to the end of the actual packet data. // This makes sure we don't have any 'junk' packets at the end. OutStream.SetLength(BaseStream.Position); }
private void WriteWhole(Deflater def, int typeCode, byte[] data) { int sz = data.Length; int hdrlen = 0; _buffer[hdrlen++] = (byte)((typeCode << 4) | sz & 15); sz = (int)(((uint)sz) >> 4); while (sz > 0) { _buffer[hdrlen - 1] |= 0x80; _buffer[hdrlen++] = (byte)(sz & 0x7f); sz = (int)(((uint)sz) >> 7); } _packDigest.Update(_buffer, 0, hdrlen); _crc.Update(_buffer, 0, hdrlen); _packOut.Write(_buffer, 0, hdrlen); def.Reset(); def.SetInput(data); def.Finish(); while (!def.IsFinished) { int datlen = def.Deflate(_buffer); _packDigest.Update(_buffer, 0, datlen); _crc.Update(_buffer, 0, datlen); _packOut.Write(_buffer, 0, datlen); } }
// make string smaller (or try it at least ;) public string Deflate(string input) { byte[] inputData = Convert.FromBase64String(input); Deflater deflater = new Deflater(Deflater.BEST_SPEED, false); deflater.SetInput(inputData); deflater.Finish(); using (var ms = new MemoryStream()) { var outputBuffer = new byte[65536 * 4]; while (deflater.IsNeedingInput == false) { var read = deflater.Deflate(outputBuffer); ms.Write(outputBuffer, 0, read); if (deflater.IsFinished == true) break; } deflater.Reset(); return Convert.ToBase64String( ms.ToArray()); } }
/// <exception cref="System.IO.IOException"></exception> private void WriteWhole(Deflater def, int typeCode, byte[] data) { int sz = data.Length; int hdrlen = 0; buf[hdrlen++] = unchecked((byte)((typeCode << 4) | sz & 15)); sz = (int)(((uint)sz) >> 4); while (sz > 0) { buf[hdrlen - 1] |= unchecked((int)(0x80)); buf[hdrlen++] = unchecked((byte)(sz & unchecked((int)(0x7f)))); sz = (int)(((uint)sz) >> 7); } packDigest.Update(buf, 0, hdrlen); crc.Update(buf, 0, hdrlen); packOut.Write(buf, 0, hdrlen); def.Reset(); def.SetInput(data); def.Finish(); while (!def.IsFinished) { int datlen = def.Deflate(buf); packDigest.Update(buf, 0, datlen); crc.Update(buf, 0, datlen); packOut.Write(buf, 0, datlen); } }