protected override void Write(BlocksQueue writeQueue, string outputFile) { WriteHeader(writeQueue, outputFile); while (writeQueue.PreparedBlocks < writeQueue.TotalBlocksCount && !writeQueue.IsStoped) { try { var block = writeQueue.PopBlock(); WriteBlock(outputFile, block); writeQueue.PreparedBlocks++; } catch (Exception e) { writeQueue.Stop(); _logger.Error(e, "An error occurred while writing block of compressed file"); } } _logger.Info(string.Format("File compressed")); }
private void Decompress(string filename, string outputFile) { var readQueue = GetReadQueue(filename); var writeQueue = new BlocksQueue(readQueue.TotalBlocksCount); var writerThread = new Thread(() => { Write(writeQueue, outputFile); }); writerThread.Start(); var maxThreadForRead = _maxThreadCount - 1; var semaphore = new Semaphore(maxThreadForRead, maxThreadForRead); while (readQueue.PreparedBlocks < readQueue.TotalBlocksCount && !readQueue.IsStoped && semaphore.WaitOne()) { var readThread = new Thread(() => { var block = readQueue.PopBlock(); ReadBlock(filename, block); if (block.HasError) { readQueue.Stop(); writeQueue.Stop(); } else { writeQueue.PushBlock(block); } semaphore.Release(); }); readThread.Start(); readQueue.PreparedBlocks++; } }