Пример #1
0
        private static bool VerifyEntry(ArcEntry entry, IProgress?progress)
        {
            var buffer = ArrayPool <byte> .Shared.Rent(BufferSize);

            try
            {
                using var inputStream = entry.Open();
                var hash = new Adler32();
                while (true)
                {
                    var bytesInBuffer = inputStream.Read(buffer);
                    if (bytesInBuffer == 0)
                    {
                        break;
                    }
                    hash.ComputeHash(buffer, 0, bytesInBuffer);

                    progress?.AddValue(bytesInBuffer);
                }
                return(entry.Hash == hash.Hash);
            }
            finally
            {
                ArrayPool <byte> .Shared.Return(buffer);
            }
        }
Пример #2
0
        private void WriteFile(string path, Stream stream, DateTimeOffset?lastWriteTime, IProgress?progress)
        {
            {
                using var outputStream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None);

                var buffer = ArrayPool <byte> .Shared.Rent(16 * 1024);

                while (true)
                {
                    var bytesRead = stream.Read(buffer, 0, buffer.Length);
                    if (bytesRead == 0)
                    {
                        break;
                    }

                    outputStream.Write(buffer, 0, bytesRead);

                    progress?.AddValue(bytesRead);
                }

                ArrayPool <byte> .Shared.Return(buffer);
            }

            if (lastWriteTime != null)
            {
                try
                {
                    new FileInfo(path).LastWriteTimeUtc = lastWriteTime.Value.UtcDateTime;
                }
                catch (Exception e)
                {
                    Console.Error.WriteLine("Failed to set last write time for file: " + e.Message);
                }
            }
        }
Пример #3
0
        public static MemoryStream ReadFile(string path, IProgress?progress)
        {
            using var inputStream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, FileOptions.SequentialScan);

            progress?.SetMaxValue(inputStream.Length);

            // Want to free previously occupied memory by memory stream.
            CollectGarbageIf(256 * 1024 * 1024 - inputStream.Length);

            var memoryStream = new MemoryStream(checked ((int)inputStream.Length));
            var buffer       = ArrayPool <byte> .Shared.Rent(BufferSize);

            while (true)
            {
                var bytesRead = inputStream.Read(buffer);
                if (bytesRead == 0)
                {
                    break;
                }
                memoryStream.Write(buffer, 0, bytesRead);

                progress?.AddValue(bytesRead);
            }

            ArrayPool <byte> .Shared.Return(buffer);

            memoryStream.Position = 0;
            return(memoryStream);
        }
Пример #4
0
        protected override void PostProcessArchive(ArcArchive archive, IProgress?progress)
        {
            progress?.SetMaxValue(archive.Count);

            foreach (var entry in archive.GetEntries())
            {
                if (!_entryNamesToKeep.Contains(entry.Name))
                {
                    entry.Remove();

                    // TODO: Write action into log
                }

                progress?.AddValue(1);
            }
        }
Пример #5
0
        private void CopyStream(Stream inputStream, Stream outputStream, IProgress?progress)
        {
            var buffer = ArrayPool <byte> .Shared.Rent(16 * 1024);

            while (true)
            {
                var bytesRead = inputStream.Read(buffer);
                if (bytesRead == 0)
                {
                    break;
                }
                outputStream.Write(buffer, 0, bytesRead);

                progress?.AddValue(bytesRead);
            }
            ArrayPool <byte> .Shared.Return(buffer);
        }
Пример #6
0
        protected void CopyFileToStream(string path, Stream outputStream, IProgress?progress)
        {
            using var inputStream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, FileOptions.SequentialScan);

            var buffer = ArrayPool <byte> .Shared.Rent(16 * 1024);

            while (true)
            {
                var bytesRead = inputStream.Read(buffer);
                if (bytesRead == 0)
                {
                    break;
                }
                outputStream.Write(buffer, 0, bytesRead);

                progress?.AddValue(bytesRead);
            }

            ArrayPool <byte> .Shared.Return(buffer);
        }
Пример #7
0
        public static void WriteFile(string path, MemoryStream stream, IProgress?progress)
        {
            stream.Position = 0;
            var bytesToWrite = stream.Length;
            var buffer       = stream.GetBuffer();
            var position     = 0;

            using var outputStream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None);

            progress?.SetValue(0);
            progress?.SetMaxValue(bytesToWrite);

            while (bytesToWrite > 0)
            {
                var blockLength = checked ((int)Math.Min(bytesToWrite, BufferSize));

                outputStream.Write(buffer, position, blockLength);
                position     += blockLength;
                bytesToWrite -= blockLength;

                progress?.AddValue(blockLength);
            }
        }