private void Read() { try { using (FileStream compressedFile = new FileStream(sourceFile, FileMode.Open)) { while (compressedFile.Position < compressedFile.Length) { byte[] lengthBuffer = new byte[8]; compressedFile.Read(lengthBuffer, 0, lengthBuffer.Length); int blockLength = BitConverter.ToInt32(lengthBuffer, 4); byte[] compressedData = new byte[blockLength]; lengthBuffer.CopyTo(compressedData, 0); compressedFile.Read(compressedData, 8, blockLength - 8); int _dataSize = BitConverter.ToInt32(compressedData, blockLength - 4); byte[] lastBuffer = new byte[_dataSize]; ByteBlock _block = new ByteBlock(counter, lastBuffer, compressedData); _queueReader.EnqueueForWriting(_block); counter++; ConsoleProgress.ProgressBar(compressedFile.Position, compressedFile.Length); } _queueReader.Stop(); } } catch (Exception ex) { Console.WriteLine(ex.Message); _cancelled = true; } }
private void Read() { try { using (FileStream fileToBeCompressed = new FileStream(sourceFile, FileMode.Open)) { int bytesRead; byte[] lastBuffer; while (fileToBeCompressed.Position < fileToBeCompressed.Length && !_cancelled) { if (fileToBeCompressed.Length - fileToBeCompressed.Position <= blockSize) { bytesRead = (int)(fileToBeCompressed.Length - fileToBeCompressed.Position); } else { bytesRead = blockSize; } lastBuffer = new byte[bytesRead]; fileToBeCompressed.Read(lastBuffer, 0, bytesRead); _queueReader.EnqueueForCompressing(lastBuffer); ConsoleProgress.ProgressBar(fileToBeCompressed.Position, fileToBeCompressed.Length); } _queueReader.Stop(); } } catch (Exception ex) { Console.WriteLine(ex.Message); _cancelled = true; } }