Esempio n. 1
0
        public void PutAllCompressedFilesTogether()
        {
            var previousBuffer = new Dictionary <int, int>();

            for (int i = 0; i < _partsCount; i++)
            {
                Console.Write(".");
                long offset = i * (long)_compressBufferSize;
                var  file   = Archiver.FormNewFileName(_targetCompressedFile, offset);
                using (FileStream nextFile = new FileStream(file, FileMode.Open))
                {
                    var bufferMessage = new byte[nextFile.Length];
                    nextFile.Read(bufferMessage, 0, bufferMessage.Length);
                    previousBuffer.Add(i, bufferMessage.Length);
                    using (FileStream targetStream = new FileStream(_targetCompressedFile, FileMode.OpenOrCreate))
                    {
                        var localOffset = CountAllPreviouslyOffset(previousBuffer, i);
                        targetStream.Seek(localOffset, SeekOrigin.Begin);
                        targetStream.Write(bufferMessage, 0, bufferMessage.Length);
                    }
                }
                File.Delete(file);
            }
        }
Esempio n. 2
0
        public void Compressing()
        {
            try
            {
                Console.Write(".");
                using (var sourceStream =
                           new FileStream(_sourceFile, FileMode.OpenOrCreate, FileAccess.Read, FileShare.Read))
                {
                    var newFile = Archiver.FormNewFileName(_targetCompressedFile, _offset);
                    using (var targetStream =
                               new FileStream(newFile, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Write))
                    {
                        sourceStream.Seek(_offset, SeekOrigin.Begin);
                        var remainingLength = sourceStream.Length - _offset;
                        var bufferMessage   = remainingLength < _compressBufferSize
                            ? new byte[remainingLength]
                            : new byte[_compressBufferSize];
                        sourceStream.Read(bufferMessage, 0, bufferMessage.Length);
                        using (var memoryStream = new MemoryStream())
                        {
                            using (var compressionStream = new GZipStream(memoryStream, CompressionMode.Compress))
                            {
                                compressionStream.Write(bufferMessage, 0, bufferMessage.Length);
                            }

                            var compressedBlock = memoryStream.ToArray();
                            AddMetaDataToBlock(ref compressedBlock);
                            targetStream.Write(compressedBlock, 0, compressedBlock.Length);
                        }
                    }
                }
            }
            catch (IOException e)
            {
                if (Validator.IsDiskFull(e))
                {
                    Console.WriteLine(
                        "Not enought disk space where your source file located. Please, free up some memory on this disk.");
                    Console.WriteLine("App completed with code 0");
                    Environment.Exit(0);
                }
            }
            catch (OutOfMemoryException)
            {
                Console.WriteLine("Lack of RAM. Please, close other application and try again.");
                Console.WriteLine("App completed with code 0");
                Environment.Exit(0);
            }
            catch (UnauthorizedAccessException)
            {
                Console.WriteLine($"App hasn't acces to file {_sourceFile} or {_targetCompressedFile}");
                Console.WriteLine("App completed with code 0");
                Environment.Exit(0);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine("App completed with code 0");
                Environment.Exit(0);
            }
        }