Example #1
0
        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++;
                        blocksCountTotal = counter;
                        ConsoleProgress.ProgressBar(_compressedFile.Position, _compressedFile.Length);
                    }
                    _queueReader.Stop();
                }
            }

            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
Example #2
0
        private void Read()
        {
            try
            {
                using (FileStream _fileToBeCompressed = new FileStream(sourceFile, FileMode.Open))
                {
                    int    bytesRead;
                    byte[] lastBuffer;
                    blocksCountTotal = (int)(_fileToBeCompressed.Length / (long)blockSize);
                    if ((int)(_fileToBeCompressed.Length % (long)blockSize) > 0)
                    {
                        blocksCountTotal++;
                    }

                    while (_fileToBeCompressed.Position < _fileToBeCompressed.Length)
                    {
                        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);
            }
        }