本文整理汇总了C#中System.IO.FileStream.MoveToNextGZip方法的典型用法代码示例。如果您正苦于以下问题:C# FileStream.MoveToNextGZip方法的具体用法?C# FileStream.MoveToNextGZip怎么用?C# FileStream.MoveToNextGZip使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.IO.FileStream
的用法示例。
在下文中一共展示了FileStream.MoveToNextGZip方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: FullDecompress
/// <summary>
/// decompress file using GZipStream
/// </summary>
/// <param name="ArchiveFileFullName">Path to file what need to decompress</param>
/// <param name="DecFileFullName">Path to file for writing decompressed bytes</param>
public void FullDecompress(string ArchiveFileFullName, string DecFileFullName)
{
Console.WriteLine("Start decompression {0}", DateTime.Now);
using (FileStream SourceFile = new FileStream(ArchiveFileFullName, FileMode.Open))
{
using (FileStream DestFile = File.Create(DecFileFullName))
{
//initialize events, by default all events - close
for (int i = 0; i < CountThreads; i++)
events[i] = new ManualResetEvent(false);
AutoResetEvent WaitStart = new AutoResetEvent(false);
//Calculate parametsr for load indicator
int CountAllReads = (int)Math.Ceiling((double)SourceFile.Length / CountBytesOneTimeRead);
int IterationRead = 0;
byte[] buffer;
//check is file multiheaded
SourceFile.MoveToNextGZip(out buffer);
if (buffer == null)
{
#region decompress 1 headed file
//create new decompression stream
using (GZipStream Decompress = new GZipStream(SourceFile, CompressionMode.Decompress))
{
buffer = new byte[CountBytesOneTimeRead];
int bytesLen = 0;
//read file by parts while nothing read
while ((bytesLen = Decompress.Read(buffer, 0, CountBytesOneTimeRead)) > 0)
{
//display compressing progress
LoadIndication(CountAllReads, IterationRead, ExitFlag);
IterationRead++;
DestFile.Write(buffer, 0, bytesLen);
}
//display 100% load
LoadIndication(1, 1, ExitFlag);
}
#endregion
}
else
{
#region decompress multiple headed files
SourceFile.Seek(0, SeekOrigin.Begin);
for (int CountCurrentThreads = 0; CountCurrentThreads < CountThreads; CountCurrentThreads++)
{
if (ExitFlag) break;
Thread thr = new Thread(delegate()
{
PartialDecompress(SourceFile, DestFile, events[CountCurrentThreads]
, WaitStart, CountAllReads, ref IterationRead);
});
thr.IsBackground = true;
try { thr.Start(); }
catch (ThreadStartException ex) { events[CountCurrentThreads].Set(); ShowErrorMessage(ex, "start decompressing thread"); }
//Wait while thread gets all params
if (!ExitFlag)
WaitStart.WaitOne();
}
#endregion
}
//wait while all decompressing threads do they job
WaitHandle.WaitAll(events);
LoadIndication(1, 1, ExitFlag);
}
}
Console.WriteLine("End decompression {0}", DateTime.Now);
}
示例2: PartialDecompress
/// <summary>
/// Decompress part of file into specific filestream
/// </summary>
/// <param name="ArchiveBMass">Compressed bytes</param>
/// <param name="DestFile">Destination file for writing decimpressed bytes</param>
/// <param name="CurEvent">Wait handle. Current thread. Need to send it next thread</param>
/// <param name="WaitEvent">Wait handle. Wait previos thread</param>
/// <param name="WaitStartStream">Wait handle. Stop main thread while current not get all params</param>
/// <param name="CountAllReads">Max reads. Need for display progress</param>
/// <param name="IterationRead">Current read. Need for display progress</param>
/// <param name="CountCurrentThreads">Dont get create threads more then CountThread</param>
private void PartialDecompress(FileStream SourceFile, FileStream DestFile, ManualResetEvent CurEvent
, AutoResetEvent WaitStartStream, int CountAllReads, ref int IterationRead)
{
ManualResetEvent WaitEventRead = new ManualResetEvent(false);
ManualResetEvent CurEventRead = new ManualResetEvent(false);
byte[] ArchiveBMass;
//info main stream about getting all params
WaitStartStream.Set();
try
{
while (SourceFile.MoveToNextGZip(out ArchiveBMass, ReadLocker,ref CurEventRead, ref PrevEvent, ref WaitEventRead))
{
int bytesRead = 0;
int AllBytesRead = 0;
using (MemoryStream ArchiveBuffer = new MemoryStream(ArchiveBMass))
{
//decompress current ArchiveBMass
using (GZipStream Decompress = new GZipStream(ArchiveBuffer, CompressionMode.Decompress, true))
{
byte[] DecompressedArchiveBMass = new byte[CountBytesOneTimeRead];
//read compressed part while it not ends
bool firstread = true;
while ((bytesRead = Decompress.Read(DecompressedArchiveBMass, 0, CountBytesOneTimeRead)) > 0)
{
AllBytesRead += bytesRead;
if (firstread)
ArchiveBMass = (byte[])DecompressedArchiveBMass.Clone();
else
ArchiveBMass = ArchiveBMass.Concat(DecompressedArchiveBMass).ToArray();
firstread = false;
}
}
if (WaitEventRead.WaitOne(10000))
WaitEventRead.Close();
else
ExitFlag = true;
//exit if Ctrl+C
if (ExitFlag)
{
CurEventRead.Set();
break;
}
//write decompressed part into file
lock (WriteLocker)
if (AllBytesRead > 0)
DestFile.Write(ArchiveBMass, 0, AllBytesRead);
}
//display progress
lock (LoadIndicationLocker)
{
LoadIndication(CountAllReads, IterationRead, ExitFlag);
IterationRead++;
}
//set signal to waiting thread
CurEventRead.Set();
}
if (!CurEvent.WaitOne(0))
CurEvent.Set();
if (!WaitStartStream.WaitOne(0))
WaitStartStream.Set();
}
catch (Exception ex)
{
if (!(ex is ObjectDisposedException || ex is NullReferenceException))
{
WaitStartStream.Set();
CurEventRead.Set();
CurEvent.Set();
}
ShowErrorMessage(ex, "Decompress thread process");
}
}