private void CompressBlocks() { //Random rnd = new Random(Thread.CurrentThread.ManagedThreadId); byte[] buffer = new byte[_blockSize]; int readedData = 0; int blockId = 0; string originalFileName = InFileInfo.Name; while (true) { lock (_lockPoint) { readedData = _inFileStream.Read(buffer, 0, buffer.Length); if (readedData == 0 && blockIter != 0) { break; } blockId = blockIter; blockIter++; } using (var mem = new MemoryStream()) { using (var gzStream = new GZipStreamEx(mem, CompressionMode.Compress)) { gzStream.Write(buffer, 0, readedData); } //Console.WriteLine("Thread #{0} write block #{1, -10} to queue #{2}", Thread.CurrentThread.ManagedThreadId, blockId, blockId % _numReadThreads); //Console.WriteLine("Producer #{0} has {1, 10} elements", blockId % _numReadThreads, _compressedBlocksProd[blockId % _numReadThreads].Count()); //Thread.Sleep(rnd.Next(500, 1500)); _compressedBlocksProd[blockId % _numReadThreads].Enqueue(new DataBlock(mem.ToArray(), (int)mem.Length, blockId)); } } }
private void DecompressBlocks() { int readedData = 0; int blockId = 0; string origFileName = InFileInfo.Name; byte[] buffer = null; int size = 0; while (true) { lock (_lockPoint) { if (_inFileStream.Length == _inFileStream.Position) { break; } int blockSize = 0; if (GZipStreamEx.TryGetBlockSize(_inFileStream, out blockSize) == false) { throw new InvalidDataException("архив повреждён"); } buffer = new byte[blockSize]; readedData = _inFileStream.Read(buffer, 0, buffer.Length); blockId = blockIter; blockIter++; } size = BitConverter.ToInt32(buffer, buffer.Length - 4); using (MemoryStream output = new MemoryStream()) { using (var input = new MemoryStream(buffer)) { using (var gzStream = new GZipStreamEx(input, CompressionMode.Decompress)) { byte[] localBuffer = new byte[size]; int decompCountBytes = 0; do { decompCountBytes = gzStream.Read(localBuffer, 0, size); output.Write(localBuffer, 0, decompCountBytes); }while (decompCountBytes != 0); _decompressedBlocksProd[blockId % _numReadThreads].Enqueue(new DataBlock(output.ToArray(), (int)output.Length, blockId)); } } } } }