本文整理汇总了C#中ICSharpCode.SharpZipLib.Zip.Compression.Deflater.SetInput方法的典型用法代码示例。如果您正苦于以下问题:C# Deflater.SetInput方法的具体用法?C# Deflater.SetInput怎么用?C# Deflater.SetInput使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ICSharpCode.SharpZipLib.Zip.Compression.Deflater
的用法示例。
在下文中一共展示了Deflater.SetInput方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Compress
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();
}
示例2: Compress
public byte[] Compress(byte[] input)
{
// Create the compressor with highest level of compression
Deflater compressor = new Deflater();
compressor.SetLevel(Deflater.BEST_COMPRESSION);
// Give the compressor the data to compress
compressor.SetInput(input);
compressor.Finish();
/*
* Create an expandable byte array to hold the compressed data.
* You cannot use an array that's the same size as the orginal because
* there is no guarantee that the compressed data will be smaller than
* the uncompressed data.
*/
MemoryStream bos = new MemoryStream(input.Length);
// Compress the data
byte[] buf = new byte[1024];
while (!compressor.IsFinished)
{
int count = compressor.Deflate(buf);
bos.Write(buf, 0, count);
}
// Get the compressed data
return bos.ToArray();
}
示例3: Compress
public static byte[] Compress(string str)
{
byte[] bytes = Encoding.Unicode.GetBytes(str);
Deflater deflater = new Deflater(Deflater.BEST_COMPRESSION);
deflater.SetInput(bytes);
deflater.Finish();
MemoryStream stream = new MemoryStream(bytes.Length);
try
{
byte[] output = new byte[0x400];
while (!deflater.IsFinished)
{
int count = deflater.Deflate(output);
stream.Write(output, 0, count);
}
}
finally
{
stream.Close();
}
byte[] buffer3 = stream.ToArray();
if ((buffer3.Length % 2) == 0)
{
return buffer3;
}
byte[] buffer4 = new byte[buffer3.Length + 1];
for (int i = 0; i < buffer3.Length; i++)
{
buffer4[i] = buffer3[i];
}
buffer4[buffer3.Length] = 0;
return buffer4;
}
示例4: Compress
/// <summary>
///
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public byte[] Compress(Stream input)
{
String s = String.Format("Compressing output with level {0:d}", CompressionLevel);
//Log.Debug(this, s);
Deflater deflater = new Deflater(CompressionLevel, false);
byte[] uncompressedData = new byte[(Int32)input.Length];
input.Seek(0, SeekOrigin.Begin);
input.Read(uncompressedData, 0, uncompressedData.Length);
byte[] buffer = new byte[input.Length];
try
{
deflater.SetInput(uncompressedData);
deflater.Finish();
int bytesDeflated = deflater.Deflate(buffer, 0, buffer.Length);
byte[] compressedData = new byte[bytesDeflated];
Array.Copy(buffer, compressedData, bytesDeflated);
//Log.Debug(this, "Compression completed.");
return compressedData;
}
catch (Exception e)
{
Log.Error(this, e);
throw e;
}
}
示例5: IOCompress
public static void IOCompress(byte[] input, int len, byte[] output, out int deflatedLength)
{
Deflater item = new Deflater();
item.SetInput(input, 0, len);
item.Finish();
deflatedLength = item.Deflate(output, 0, output.Length);
}
示例6: Compress
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);
}
示例7: Compress
/// <summary>Compresses the specified byte range using the
/// specified compressionLevel (constants are defined in
/// java.util.zip.Deflater).
/// </summary>
public static byte[] Compress(byte[] value_Renamed, int offset, int length, int compressionLevel)
{
/* Create an expandable byte array to hold the compressed data.
* You cannot use an array that's the same size as the orginal because
* there is no guarantee that the compressed data will be smaller than
* the uncompressed data. */
System.IO.MemoryStream bos = new System.IO.MemoryStream(length);
Deflater compressor = new Deflater();
try
{
compressor.SetLevel(compressionLevel);
compressor.SetInput(value_Renamed, offset, length);
compressor.Finish();
// Compress the data
byte[] buf = new byte[1024];
while (!compressor.IsFinished)
{
int count = compressor.Deflate(buf);
bos.Write(buf, 0, count);
}
}
finally
{
}
return bos.ToArray();
}
示例8: CompressZLib
/// <summary>
/// Performs deflate compression on the given data.
/// </summary>
/// <param name="input">the data to compress</param>
/// <param name="output">the compressed data</param>
public static void CompressZLib(byte[] input, byte[] output, int compressionLevel, out int deflatedLength)
{
Deflater item = new Deflater(compressionLevel);
item.SetInput(input, 0, input.Length);
item.Finish();
deflatedLength = item.Deflate(output, 0, output.Length);
}
示例9: SendTo
/// <summary>
/// Sends packet (might be compressed)
/// </summary>
/// <returns></returns>
public void SendTo(IRealmClient client)
{
if (TotalLength <= WCellDef.MAX_UNCOMPRESSED_UPDATE_PACKET)
{
client.Send(GetFinalizedPacket());
}
else
{
var segment = ((SegmentStream)BaseStream).Segment;
//var input = ((MemoryStream)BaseStream).ToArray();
var inputOffset = HeaderSize;
//Compression.CompressZLib(packetBuffer, outputBuffer, RealmServer.Instance.Configuration.CompressionLevel, out deflatedLength);
var length = ContentLength;
if (length > 0x7FFF)
{
log.Warn("Sent UpdatePacket with Length {0} to {1} in {2}", length, client,
client.ActiveCharacter.Zone as IWorldSpace ?? client.ActiveCharacter.Region);
}
var maxOutputLength = length + FullUpdatePacketHeaderSize;
var outSegment = BufferManager.GetSegment(maxOutputLength);
var deflater = new Deflater(RealmServerConfiguration.CompressionLevel);
deflater.SetInput(segment.Buffer.Array, segment.Offset + inputOffset, length);
//deflater.SetInput(input, 0 + inputOffset, length);
deflater.Finish();
int deflatedLength = deflater.Deflate(outSegment.Buffer.Array,
outSegment.Offset + FullUpdatePacketHeaderSize, length);
var totalLength = deflatedLength + FullUpdatePacketHeaderSize;
if (totalLength > MaxPacketSize)
{
//TODO: Split up packet if packet size exceeds max length
throw new Exception("Compressed Update packet exceeded max length: " + totalLength);
}
SendPacket(client, outSegment, totalLength, length);
outSegment.DecrementUsage();
}
}
示例10: Compress
public static byte[] Compress(byte[] input, int level)
{
byte[] bytesOut;
byte[] temp = new byte[input.Length];
Deflater deflater = new Deflater(level, true);
try
{
deflater.SetInput(input, 0, input.Length);
deflater.Finish();
bytesOut = new byte[deflater.Deflate(temp)];
}
catch (Exception e)
{
throw e;
}
Array.Copy(temp, 0, bytesOut, 0, bytesOut.Length);
return bytesOut;
}
示例11: Compress
public static byte[] Compress(byte[] content)
{
//return content;
Deflater compressor = new Deflater();
compressor.SetLevel(Deflater.BEST_COMPRESSION);
compressor.SetInput(content);
compressor.Finish();
using (MemoryStream bos = new MemoryStream(content.Length))
{
var buf = new byte[1024];
while (!compressor.IsFinished)
{
int n = compressor.Deflate(buf);
bos.Write(buf, 0, n);
}
return bos.ToArray();
}
}
示例12: WriteWhole
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);
}
}
示例13: EndCompress
/// <summary>
/// End compression.
/// </summary>
private void EndCompress()
{
// If no buffer exists, return.
if (_buffer == null)
return;
// Input buffer.
byte[] input = _buffer.ToArray();
// Dump buffer
_buffer.Clear();
_buffer = null;
// Create a new memory stream.
using (MemoryStream ms = new MemoryStream())
{
Deflater compress = new Deflater();
compress.SetInput(input);
compress.Finish();
// the result buffer
byte[] result = new byte[input.Length];
// Loop it just in case (shouldn't happen BUT, zlib's been known to cause a size increase)
while (!compress.IsFinished)
{
int length = compress.Deflate(result);
ms.Write(result, 0, length);
}
// Write the size of the compressed data, and the data.
WriteInt((int)ms.Length);
WriteBytes(ms.ToArray());
}
}
示例14: Write
/// <summary>
/// Writes the MPK to a specific filename
/// </summary>
/// <param name="fname"></param>
public void Write(string fname)
{
Deflater def;
byte[] buf, dir, name;
var files = new MPKFile[_files.Count];
using (var dirmem = new MemoryStream(_files.Count*MPKFileHeader.MaxSize))
{
int index = 0;
uint offset = 0;
uint diroffset = 0;
foreach (MPKFile file in _files.Values)
{
file.Header.DirectoryOffset = diroffset;
file.Header.Offset = offset;
files[index] = file;
using (var wrtr = new BinaryWriter(dirmem, Encoding.UTF8))
{
file.Header.Write(wrtr);
}
offset += file.Header.UncompressedSize;
diroffset += file.Header.CompressedSize;
index++;
}
def = new Deflater();
def.SetInput(dirmem.GetBuffer(), 0, (int) dirmem.Position);
def.Finish();
dir = new byte[dirmem.Position];
def.Deflate(dir);
_sizeDir = (int) def.TotalOut;
}
def = new Deflater();
_crc.Reset();
_crc.Update(dir, 0, _sizeDir);
def.SetInput(Encoding.UTF8.GetBytes(_name));
def.Finish();
name = new byte[_name.Length];
def.Deflate(name);
_numFiles = _files.Count;
_sizeName = (int) def.TotalOut;
using (var filemem = new MemoryStream())
{
using (var wrtr = new BinaryWriter(filemem, Encoding.UTF8))
{
wrtr.Write((int) _crc.Value);
wrtr.Write(_sizeDir);
wrtr.Write(_sizeName);
wrtr.Write(_numFiles);
buf = new byte[16];
Buffer.BlockCopy(filemem.GetBuffer(), 0, buf, 0, 16);
for (byte i = 0; i < 16; i++)
{
buf[i] ^= i;
}
}
}
using (FileStream fileStream = File.Open(fname, FileMode.Create, FileAccess.Write))
{
using (var wrtr = new BinaryWriter(fileStream, Encoding.UTF8))
{
wrtr.Write(Magic);
wrtr.Write((byte) 2);
wrtr.Write(buf, 0, 16);
wrtr.Write(name, 0, _sizeName);
wrtr.Write(dir, 0, _sizeDir);
foreach (MPKFile file in files)
{
wrtr.Write(file.CompressedData);
}
}
}
}
示例15: ComprimirBuffer
//cualquiera de las dos funciones de comprimir causa un leak de memoria enorme
public static int ComprimirBuffer(byte[] in_buffer,ref byte[] out_buffer)
{
out_buffer=new byte[in_buffer.Length+300];
Deflater compresor=new Deflater();
compresor.SetInput(in_buffer);
compresor.Flush();
compresor.Finish();
int compressedsize=compresor.Deflate(out_buffer,0,(int)(in_buffer.Length)+300);
compresor=null;
return compressedsize;
}