Esempio n. 1
0
        private void StartReadAndCompress()
        {
            //var threadName = Thread.CurrentThread.Name ?? "";
            //Console.WriteLine($"{threadName} start");
            using (var sourceStream = new FileStream(SourceFileName, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                try
                {
                    var    maxPosition = sourceStream.Length;
                    byte[] buffer      = new byte[Settings.ChunkSizeBytes];

                    while (true)
                    {
                        var currentPosition = PositionToRead.GetLastPositionAndIncrement();
                        if (currentPosition < 0)
                        {
                            break;
                        }
                        //Console.WriteLine($"{threadName} processing {currentPosition}");

                        var chunkSize = (int)Math.Min(Settings.ChunkSizeBytes, maxPosition - currentPosition);
                        sourceStream.Seek(currentPosition, SeekOrigin.Begin);
                        sourceStream.Read(buffer, 0, chunkSize);
                        using (MemoryStream ms = new MemoryStream())
                        {
                            using (GZipStream gs = new GZipStream(ms, CompressionMode.Compress))
                                gs.Write(buffer, 0, chunkSize);

                            QueueToWrite.Enqueue(new Task(currentPosition, ms.ToArray()));
                        }
                    }
                }
                catch (OutOfMemoryException)
                {
                    QueueToWrite.Enqueue(new Task("There is no enough memory to continue"));
                }
                catch (Exception ex)
                {
                    QueueToWrite.Enqueue(new Task(ex.Message));
                }
                finally
                {
                    QueueToWrite.FinishEnqueue();
                }
            }
            //Console.WriteLine($"{threadName} end");
        }
Esempio n. 2
0
        private void StartReadAndDecompress()
        {
            //var threadName = Thread.CurrentThread.Name ?? "Main";
            //Console.WriteLine($"{threadName} start");
            using (var sourceStream = new FileStream(SourceFileName, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                try
                {
                    while (true)
                    {
                        var currentPosition = (int)PositionToRead.GetLastPositionAndIncrement();
                        if (currentPosition < 0)
                        {
                            break;
                        }
                        var chunk = Metadata.ChunkList[currentPosition];
                        //Console.WriteLine($"{threadName} processing {chunk.CompressedOffset}");

                        sourceStream.Seek(chunk.CompressedOffset, SeekOrigin.Begin);
                        using (MemoryStream ms = new MemoryStream())
                        {
                            using (GZipStream gs = new GZipStream(sourceStream, CompressionMode.Decompress, true))
                                gs.CopyTo(ms, Settings.ChunkSizeBytes);
                            QueueToWrite.Enqueue(new Task(chunk.UncompressedOffset, ms.ToArray()));
                        }
                    }
                }
                catch (Exception ex)
                {
                    QueueToWrite.Enqueue(new Task(ex.Message));
                }
                finally
                {
                    QueueToWrite.FinishEnqueue();
                }
            }
            //Console.WriteLine($"{threadName} end");
        }