public override void Decompress() { initializePlan(); Thread processThread = new Thread(consumeCompressedFilePart); Thread writeThread = new Thread(writeDecompressedParts); try { using (FileStream reader = new FileStream(m_inputFile, FileMode.Open, FileAccess.Read, FileShare.None, c_blockSize)) { processThread.Start(); writeThread.Start(); // Skip custom seporator var customSeporatorBytes = CustomBlockSeporator.GetSeporatorBytes(); reader.Seek(customSeporatorBytes.LongLength, SeekOrigin.Begin); // Get the whole block count var wholeBlockCountBytes = new byte[sizeof(long)]; var wholeBlockCountBytesFetched = reader.Read(wholeBlockCountBytes, 0, wholeBlockCountBytes.Length); if (wholeBlockCountBytesFetched != sizeof(long)) { throw new Exception("Invalid file header"); } m_fileBlocksCount = BitConverter.ToInt64(wholeBlockCountBytes, 0); // Read until file doesnt end long blockIndex = 0; int readBytes = 0; do { // Read block header - block length byte[] blockHeader = new byte[sizeof(int)]; int blockHeaderLength = reader.Read(blockHeader, 0, sizeof(int)); if (blockHeaderLength == 0) { break; } if (blockHeaderLength != sizeof(int)) { throw new InvalidOperationException("Invalid chunk header"); } // Check the length of the following block payload is it the same with previous readed int blockSize = BitConverter.ToInt32(blockHeader, 0); byte[] block = new byte[blockSize]; readBytes = reader.Read(block, 0, blockSize); if (readBytes <= 0) { break; } FilePart filePart = new FilePart(block, blockIndex); m_rawBlockQueue.Enqueue(filePart); blockIndex++; }while (readBytes > 0); } writeThread.Join(); } catch (Exception ex) { throw new Exception("Error was occured in decompress method"); } }
private void writeCustomHeader(FileStream fileStream) { var header = CustomBlockSeporator.GetSeporatorBytes(); fileStream.Write(header, 0, header.Length); var blocksCountBytes = BitConverter.GetBytes(m_fileBlocksCount); fileStream.Write(blocksCountBytes, 0, blocksCountBytes.Length); }