本文整理汇总了C#中Ionic.Zlib.ZlibCodec.SyncInflate方法的典型用法代码示例。如果您正苦于以下问题:C# ZlibCodec.SyncInflate方法的具体用法?C# ZlibCodec.SyncInflate怎么用?C# ZlibCodec.SyncInflate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Ionic.Zlib.ZlibCodec
的用法示例。
在下文中一共展示了ZlibCodec.SyncInflate方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Zlib_TestFlushSync
public void Zlib_TestFlushSync()
{
int rc;
int bufferSize = 40000;
byte[] CompressedBytes = new byte[bufferSize];
byte[] DecompressedBytes = new byte[bufferSize];
string TextToCompress = "This is the text that will be compressed.";
byte[] BytesToCompress = System.Text.ASCIIEncoding.ASCII.GetBytes(TextToCompress);
ZlibCodec compressor = new ZlibCodec(CompressionMode.Compress);
compressor.InputBuffer = BytesToCompress;
compressor.NextIn = 0;
compressor.AvailableBytesIn = 3;
compressor.OutputBuffer = CompressedBytes;
compressor.NextOut = 0;
compressor.AvailableBytesOut = CompressedBytes.Length;
rc = compressor.Deflate(FlushType.Full);
CompressedBytes[3]++; // force an error in first compressed block // dinoch - ??
compressor.AvailableBytesIn = TextToCompress.Length - 3;
rc = compressor.Deflate(FlushType.Finish);
Assert.AreEqual<int>(ZlibConstants.Z_STREAM_END, rc, String.Format("at Deflate() [{0}]", compressor.Message));
rc = compressor.EndDeflate();
bufferSize = (int)(compressor.TotalBytesOut);
ZlibCodec decompressor = new ZlibCodec(CompressionMode.Decompress);
decompressor.InputBuffer = CompressedBytes;
decompressor.NextIn = 0;
decompressor.AvailableBytesIn = 2;
decompressor.OutputBuffer = DecompressedBytes;
decompressor.NextOut = 0;
decompressor.AvailableBytesOut = DecompressedBytes.Length;
rc = decompressor.Inflate(FlushType.None);
decompressor.AvailableBytesIn = bufferSize - 2;
rc = decompressor.SyncInflate();
bool gotException = false;
try
{
rc = decompressor.Inflate(FlushType.Finish);
}
catch (ZlibException ex1)
{
TestContext.WriteLine("Got Expected Exception: " + ex1);
gotException = true;
}
Assert.IsTrue(gotException, "inflate should report DATA_ERROR");
rc = decompressor.EndInflate();
Assert.AreEqual<int>(ZlibConstants.Z_OK, rc, String.Format("at EndInflate() [{0}]", decompressor.Message));
int j = 0;
for (; j < DecompressedBytes.Length; j++)
if (DecompressedBytes[j] == 0)
break;
var result = System.Text.ASCIIEncoding.ASCII.GetString(DecompressedBytes, 0, j);
Assert.AreEqual<int>(TextToCompress.Length, result.Length + 3, "Strings are unequal lengths");
Console.WriteLine("orig length: {0}", TextToCompress.Length);
Console.WriteLine("compressed length: {0}", compressor.TotalBytesOut);
Console.WriteLine("uncompressed length: {0}", decompressor.TotalBytesOut);
Console.WriteLine("result length: {0}", result.Length);
Console.WriteLine("result of inflate:\n(Thi){0}", result);
}
示例2: Run
private void Run()
{
int rc;
int comprLen = 40000;
int uncomprLen = comprLen;
byte[] CompressedBytes = new byte[comprLen];
byte[] DecompressedBytes = new byte[uncomprLen];
string TextToCompress = "This is the text that will be compressed.";
byte[] BytesToCompress = System.Text.ASCIIEncoding.ASCII.GetBytes(TextToCompress);
ZlibCodec compressor = new ZlibCodec(CompressionMode.Compress);
compressor.InputBuffer = BytesToCompress;
compressor.NextIn = 0;
compressor.OutputBuffer = CompressedBytes;
compressor.NextOut = 0;
compressor.AvailableBytesIn = 3;
compressor.AvailableBytesOut = CompressedBytes.Length;
rc = compressor.Deflate(ZlibConstants.Z_FULL_FLUSH);
CheckForError(compressor, rc, "Deflate");
CompressedBytes[3]++; // force an error in first compressed block // dinoch
compressor.AvailableBytesIn = TextToCompress.Length - 3;
rc = compressor.Deflate(ZlibConstants.Z_FINISH);
if (rc != ZlibConstants.Z_STREAM_END)
{
CheckForError(compressor, rc, "Deflate");
}
rc = compressor.EndDeflate();
CheckForError(compressor, rc, "EndDeflate");
comprLen = (int) (compressor.TotalBytesOut);
ZlibCodec decompressor = new ZlibCodec(CompressionMode.Decompress);
decompressor.InputBuffer = CompressedBytes;
decompressor.NextIn = 0;
decompressor.AvailableBytesIn = 2;
decompressor.OutputBuffer = DecompressedBytes;
decompressor.NextOut = 0;
decompressor.AvailableBytesOut = DecompressedBytes.Length;
rc = decompressor.Inflate(ZlibConstants.Z_NO_FLUSH);
CheckForError(decompressor, rc, "Inflate");
decompressor.AvailableBytesIn = CompressedBytes.Length - 2;
rc = decompressor.SyncInflate();
CheckForError(decompressor, rc, "SyncInflate");
bool gotException = false;
try
{
rc = decompressor.Inflate(ZlibConstants.Z_FINISH);
}
catch (ZlibException ex1)
{
Console.WriteLine("Got Expected Exception: " + ex1);
gotException = true;
}
if (!gotException)
{
System.Console.Out.WriteLine("inflate should report DATA_ERROR");
/* Because of incorrect adler32 */
System.Environment.Exit(1);
}
rc = decompressor.EndInflate();
CheckForError(decompressor, rc, "EndInflate");
int j = 0;
for (; j < DecompressedBytes.Length; j++)
if (DecompressedBytes[j] == 0)
break;
var result = System.Text.ASCIIEncoding.ASCII.GetString(DecompressedBytes, 0, j);
Console.WriteLine("orig length: {0}", TextToCompress.Length);
Console.WriteLine("compressed length: {0}", compressor.TotalBytesOut);
Console.WriteLine("uncompressed length: {0}", decompressor.TotalBytesOut);
Console.WriteLine("result length: {0}", result.Length);
Console.WriteLine("result of inflate:\n(Thi){0}", result);
}