/// <summary> /// Decopmpress of the block if PC has RAM less than block size /// </summary> /// <param name="id">block id</param> /// <param name="blockSize"> block size in bytes</param> /// <param name="sizeFreeMemory"> size of free RAM</param> /// <param name="deCompressionStream">stream</param> private void DecompressFileInBlocksOnMaxRamMemory(int id, long blockSize, long sizeFreeMemory, GZipStream deCompressionStream) { while (blockSize > 0) { if (blockSize > sizeFreeMemory) { blockSize = blockSize - sizeFreeMemory; } else { sizeFreeMemory = blockSize; blockSize = 0; } var bytes = new LargeByteArray(sizeFreeMemory); ReadingDecompressedStreamInByte(bytes, deCompressionStream); #if DEBUG Console.WriteLine($"Read the block id:{id} size:{sizeFreeMemory / 1024} KB"); #endif lock (_blocks) { _blocks.Enqueue(new Block(id, bytes, sizeFreeMemory)); } if (_threadWDec.IsAlive == false) { _threadWDec.Start(); } sizeFreeMemory = GetSizeFreeMemory(); } }
/// <summary> /// Reading from stream to bytes array of datas /// </summary> /// <param name="bytes">big bytes array</param> /// <param name="deCompressionStream">stream</param> private void ReadingDecompressedStreamInByte(LargeByteArray bytes, GZipStream deCompressionStream) { for (int i = 0; i < bytes.CountOfArray; i++) { int count = deCompressionStream.Read(bytes[i], 0, bytes[i].Length); bytes.CountOfByte[i] = count; } }
/// <summary> /// Reading from stream to bytes array of datas /// </summary> /// <param name="bytes">bytes array for reading</param> /// <param name="fs">stream</param> /// <returns> count read bytes </returns> private static long ReadingStreamInByte(LargeByteArray bytes, FileStream fs) { long countReadingByte = 0; for (int i = 0; i < bytes.CountOfArray; ++i) { int count = fs.Read(bytes[i], 0, bytes[i].Length); bytes.CountOfByte[i] = count; countReadingByte += count; } return(countReadingByte); }
/// <summary> /// Function of Decompressing /// </summary> /// <param name="compressedFile">compressed file</param> /// <param name="deCompressedFile">decompressed file</param> public void Decompress(string compressedFile, string deCompressedFile) { FileIsExist(compressedFile); _compressedFile = compressedFile; _deCompressedFile = deCompressedFile; using (var compressFs = new FileStream(_compressedFile, FileMode.OpenOrCreate, FileAccess.Read)) { using (var deCompressionStream = new GZipStream(compressFs, CompressionMode.Decompress)) { _finish = false; _threadWDec = new Thread(WritingOfDecompressedBlock); var(id, blockSize) = GetIdAndSizeOfBlock(deCompressionStream); while (blockSize > 0) { var sizeFreeMemory = GetSizeFreeMemory(); if (blockSize > sizeFreeMemory) { DecompressFileInBlocksOnMaxRamMemory(id, blockSize, sizeFreeMemory, deCompressionStream); } else { var bytes = new LargeByteArray(blockSize); ReadingDecompressedStreamInByte(bytes, deCompressionStream); lock (_blocks) { _blocks.Enqueue(new Block(id, bytes, blockSize)); } #if DEBUG Console.WriteLine($"Read the block id:{id} size:{blockSize / 1024} KB"); #endif if (_threadWDec.IsAlive == false) { _threadWDec.Start(); } } (id, blockSize) = GetIdAndSizeOfBlock(deCompressionStream); } _finish = true; EndReadingWaitHandler.WaitOne(); } } #if DEBUG Console.WriteLine("Decompressed successfully"); #endif }
/// <summary> /// Function of compressing /// </summary> /// <param name="sourceFile">source file</param> /// <param name="compressedFile">compressed file </param> public void Compress(string sourceFile, string compressedFile) { FileIsExist(sourceFile); _sourceFile = sourceFile; _compressedFile = $"{compressedFile}.gz"; using (var fs = new FileStream(_sourceFile, FileMode.Open)) { var id = 0; _finish = false; var threadW = new Thread(WritingOfBlockCompress); long sizeFreeMemory = GetSizeFreeMemory(); var bytes = new LargeByteArray(sizeFreeMemory); long countReadingByte = ReadingStreamInByte(bytes, fs); while (countReadingByte > 0) { #if DEBUG Console.WriteLine($"Read the block: id:{id} size:{countReadingByte / 1024} KB"); #endif lock (_blocks) { _blocks.Enqueue(new Block(id++, bytes, countReadingByte)); } if (threadW.IsAlive == false) { threadW.Start(); } sizeFreeMemory = GetSizeFreeMemory(); bytes = new LargeByteArray(sizeFreeMemory); countReadingByte = ReadingStreamInByte(bytes, fs); } _finish = true; EndReadingWaitHandler.WaitOne(); } #if DEBUG Console.WriteLine("Сompressed successfully"); #endif }