本文整理汇总了C#中GZipStream.WriteByte方法的典型用法代码示例。如果您正苦于以下问题:C# GZipStream.WriteByte方法的具体用法?C# GZipStream.WriteByte怎么用?C# GZipStream.WriteByte使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类GZipStream
的用法示例。
在下文中一共展示了GZipStream.WriteByte方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Zlib_GZipStream_ByteByByte_CheckCrc
public void Zlib_GZipStream_ByteByByte_CheckCrc()
{
// select the name of the zip file
string FileToCompress = System.IO.Path.Combine(TopLevelDir, "Zlib_GZipStream_ByteByByte.dat");
Assert.IsFalse(System.IO.File.Exists(FileToCompress), "The temporary zip file '{0}' already exists.", FileToCompress);
byte[] working = new byte[WORKING_BUFFER_SIZE];
int n = -1;
int sz = this.rnd.Next(21000) + 15000;
TestContext.WriteLine(" Creating file: {0} sz({1})", FileToCompress, sz);
CreateAndFillFileText(FileToCompress, sz);
System.IO.FileInfo fi1 = new System.IO.FileInfo(FileToCompress);
int crc1 = DoCrc(FileToCompress);
// four trials, all combos of FileName and Comment null or not null.
for (int k = 0; k < 4; k++)
{
string CompressedFile = String.Format("{0}-{1}.compressed", FileToCompress, k);
using (Stream input = File.OpenRead(FileToCompress))
{
using (FileStream raw = new FileStream(CompressedFile, FileMode.Create))
{
using (GZipStream compressor =
new GZipStream(raw, CompressionMode.Compress, CompressionLevel.BestCompression, true))
{
// FileName is optional metadata in the GZip bytestream
if (k % 2 == 1)
compressor.FileName = FileToCompress;
// Comment is optional metadata in the GZip bytestream
if (k > 2)
compressor.Comment = "Compressing: " + FileToCompress;
byte[] buffer = new byte[1024];
n = -1;
while (n != 0)
{
if (n > 0)
{
for (int i=0; i < n; i++)
compressor.WriteByte(buffer[i]);
}
n = input.Read(buffer, 0, buffer.Length);
}
}
}
}
System.IO.FileInfo fi2 = new System.IO.FileInfo(CompressedFile);
Assert.IsTrue(fi1.Length > fi2.Length, String.Format("Compressed File is not smaller, trial {0} ({1}!>{2})", k, fi1.Length, fi2.Length));
// decompress twice:
// once with System.IO.Compression.GZipStream and once with Alienlab.Zlib.GZipStream
for (int j = 0; j < 2; j++)
{
using (var input = System.IO.File.OpenRead(CompressedFile))
{
Stream decompressor = null;
try
{
switch (j)
{
case 0:
decompressor = new Alienlab.Zlib.GZipStream(input, CompressionMode.Decompress, true);
break;
case 1:
decompressor = new System.IO.Compression.GZipStream(input, System.IO.Compression.CompressionMode.Decompress, true);
break;
}
string DecompressedFile =
String.Format("{0}.{1}.decompressed", CompressedFile, (j == 0) ? "Ionic" : "BCL");
TestContext.WriteLine("........{0} ...", System.IO.Path.GetFileName(DecompressedFile));
using (var s2 = System.IO.File.Create(DecompressedFile))
{
n = -1;
while (n != 0)
{
n = decompressor.Read(working, 0, working.Length);
if (n > 0)
s2.Write(working, 0, n);
}
}
int crc2 = DoCrc(DecompressedFile);
Assert.AreEqual<Int32>(crc1, crc2);
}
finally
{
if (decompressor as Alienlab.Zlib.GZipStream != null)
{
//.........这里部分代码省略.........