本文整理汇总了C#中ICSharpCode.SharpZipLib.Zip.Compression.Deflater类的典型用法代码示例。如果您正苦于以下问题:C# Deflater类的具体用法?C# Deflater怎么用?C# Deflater使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Deflater类属于ICSharpCode.SharpZipLib.Zip.Compression命名空间,在下文中一共展示了Deflater类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: 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);
}
示例2: ChunkDataPacket
public ChunkDataPacket()
{
if (zLibDeflater == null)
zLibDeflater = new Deflater(CompressionLevel);
if (LockObject == null)
LockObject = new object();
}
示例3: 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();
}
示例4: 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();
}
示例5: TestInflateDeflate
public void TestInflateDeflate()
{
MemoryStream ms = new MemoryStream();
Deflater deflater = new Deflater(6);
DeflaterOutputStream outStream = new DeflaterOutputStream(ms, deflater);
byte[] buf = new byte[1000000];
System.Random rnd = new Random();
rnd.NextBytes(buf);
outStream.Write(buf, 0, buf.Length);
outStream.Flush();
outStream.Finish();
ms.Seek(0, SeekOrigin.Begin);
InflaterInputStream inStream = new InflaterInputStream(ms);
byte[] buf2 = new byte[buf.Length];
int pos = 0;
while (true) {
int numRead = inStream.Read(buf2, pos, 4096);
if (numRead <= 0) {
break;
}
pos += numRead;
}
for (int i = 0; i < buf.Length; ++i) {
Assertion.AssertEquals(buf2[i], buf[i]);
}
}
示例6: 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();
}
示例7: 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;
}
}
示例8: 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;
}
示例9: 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);
}
示例10: Compress
public byte[] Compress(byte[] bytData, params int[] ratio)
{
int compRatio = 9;
try
{
if (ratio[0] > 0)
{
compRatio = ratio[0];
}
}
catch
{
throw;
}
try
{
var ms = new MemoryStream();
var defl = new Deflater(compRatio, false);
Stream s = new DeflaterOutputStream(ms, defl);
s.Write(bytData, 0, bytData.Length);
s.Close();
byte[] compressedData = ms.ToArray();
return compressedData;
}
catch
{
throw;
}
}
示例11: ObjectWriter
/// <summary>
/// Construct an object writer for the specified repository.
/// </summary>
/// <param name="repo"> </param>
public ObjectWriter(Repository repo)
{
_r = repo;
_buf = new byte[0x2000];
_md = new MessageDigest();
_def = new Deflater(_r.Config.getCore().getCompression());
}
示例12: ObjectWriter
public ObjectWriter(Repository repo)
{
this.r = repo;
buf = new byte[8192];
md = new MessageDigest(); // [henon] Sha1 hash digest generator
def = new Deflater(r.Config.Core.Compression);
}
示例13: 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);
}
示例14: ZlibStream
public ZlibStream(Stream inner, Inflater inflater, int buffSize)
{
_innerStream = inner;
_in = inflater;
_inBuff = new byte[buffSize];
_outBuff = _inBuff;
_out = new Deflater();
}
示例15: Compress
/// <summary>
/// Compress an array of bytes.
/// </summary>
/// <param name="_pBytes">An array of bytes to be compressed.</param>
/// <returns>Compressed bytes.</returns>
/// <example>
/// Following example demonstrates the way of compressing an ASCII string text.
/// <code>
/// public void Compress()
/// {
/// string source = "Hello, world!";
/// byte[] source_bytes = System.Text.Encoding.ASCII.GetBytes(source);
/// byte[] compressed = DataCompression.Compress(source_bytes);
///
/// // Process the compressed bytes here.
/// }
/// </code>
/// </example>
/// <remarks>It is the best practice that use the overrided <b>DataCompression.Compress</b> method with <see cref="System.String"/> parameter to compress a string.</remarks>
public static byte[] Compress(byte[] _pBytes)
{
MemoryStream ms = new MemoryStream();
Deflater mDeflater = new Deflater(Deflater.BEST_COMPRESSION);
DeflaterOutputStream outputStream = new DeflaterOutputStream(ms, mDeflater, 131072);
outputStream.Write(_pBytes, 0, _pBytes.Length);
outputStream.Close();
return ms.ToArray();
}