static void ReadNotCompressedFile(object threadData) { string pathToInputFile = ( string )threadData; FileInfo inputFileInfo = new FileInfo(pathToInputFile); List <ThreadedReader> gZipThreads = new List <ThreadedReader>(); #region Divide input file to blocks using (var outFileStream = new FileStream(pathToInputFile, FileMode.Open, FileAccess.Read, FileShare.Read)) { var writeBlockTotalCount = ( int )(outFileStream.Length / BufferSize) + 1; dataBlocks = new DataBlock [writeBlockTotalCount]; for (int i = 0; i < dataBlocks.Length; i++) { dataBlocks [i] = new DataBlock(); } } #endregion readyToWrite = true; #region Read input file by blocks with threads for (int i = 0; i < threadCount; i++) { //CompressionThread gZipThread = new CompressionThread(inputFileInfo, threadCount, i); ThreadedReader gZipThread = new ThreadedReader( inputFileInfo, threadCount, i, ThreadedReader.ReadBytesBlockForCompression ); gZipThreads.Add(gZipThread); } #endregion //wait for threads while (gZipThreads.Any(v => !v.Finished)) { Thread.Sleep(100); } gZipThreads.Clear(); GC.Collect(); /* Console.WriteLine( "" ); * Console.WriteLine( " Read END" );*/ }
static void ReadCompressedFile(string fileName) { try { FileInfo fileToDecompress = new FileInfo(fileName); if (fileToDecompress.Extension != ".gz") { return; } #region Read Headers from compressed input file Console.WriteLine("Reading GZip Headers, this can take a few minutes for a large file"); List <ThreadedReader> gZipThreads_Headers = new List <ThreadedReader>(); // Create DataBlocks dataBlocks = new DataBlock [fileToDecompress.Length / BufferSize + 1]; for (int i = 0; i < dataBlocks.Length; i++) { dataBlocks [i] = new DataBlock(); } //threads read headers for (int i = 0; i < threadCount; i++) { ThreadedReader gZipThread = new ThreadedReader( fileToDecompress, threadCount, i, ThreadedReader.ReadHeaders //, true ); gZipThreads_Headers.Add(gZipThread); } //wait for threads while (gZipThreads_Headers.Any(v => !v.Finished)) { Thread.Sleep(100); } //check for broken gzip if (headersFound.Count == 0) { throw new Exception("Source file doesn't contains any compressed data"); } //order headers headersFound = headersFound.Distinct().OrderBy(v => v).ToList(); Console.WriteLine("\nHeaders found " + headersFound.Count); Console.WriteLine(string.Format("Completed in {0} seconds", (DateTime.Now - startTime).TotalSeconds) + "\n\n"); #endregion #region Create DataBlocks array from GZipHeaders. Each DB have indexes(start/end) for reading from input file dataBlocks = new DataBlock [headersFound.Count]; for (int i = 0; i < dataBlocks.Length; i++) { long startIndex, endIndex = 0; startIndex = headersFound [i]; endIndex = i + 1 < headersFound.Count ? headersFound [i + 1] : fileToDecompress.Length + 1; //endIndex = headersFound [ i + 1 ]; dataBlocks [i] = new DataBlock() { startIndex = startIndex, endIndex = endIndex }; } #endregion readyToWrite = true; #region Read compressed input file by DataBlocks indexes List <ThreadedReader> gZipThreads = new List <ThreadedReader>(); for (int i = 0; i < threadCount; i++) { ThreadedReader gZipThread = new ThreadedReader( fileToDecompress, threadCount, i, ThreadedReader.ReadBytesBlockForDecompression ); gZipThreads.Add(gZipThread); } //wait for threads while (gZipThreads.Any(v => !v.Finished)) { Thread.Sleep(100); } gZipThreads.Clear(); GC.Collect(); #endregion } catch (Exception ex) { Console.WriteLine("Error is occured!\n Method: {0}\n Error description {1}", ex.TargetSite, ex.Message); } }