< Summary

Line coverage
41%
Covered lines: 51
Uncovered lines: 71
Coverable lines: 122
Total lines: 393
Line coverage: 41.8%
Branch coverage
37%
Covered branches: 18
Total branches: 48
Branch coverage: 37.5%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

File(s)

C:\projects\jeremyansel-io-locator\JeremyAnsel.IO.Locator\JeremyAnsel.IO.Locator\ArchiveWritableFileLocator.cs

#LineLine coverage
 1// <copyright file="ArchiveWritableFileLocator.cs" company="Jérémy Ansel">
 2// Copyright (c) 2015, 2019 Jérémy Ansel
 3// </copyright>
 4// <license>
 5// Licensed under the MIT license. See LICENSE.txt
 6// </license>
 7
 8namespace JeremyAnsel.IO.Locator
 9{
 10    using System;
 11    using System.Collections.Generic;
 12    using System.IO;
 13    using SharpCompress.Common;
 14    using SharpCompress.Writers;
 15
 16    /// <summary>
 17    /// A writable archive file locator.
 18    /// </summary>
 19    internal sealed class ArchiveWritableFileLocator : IWritableFileLocator
 20    {
 21        /// <summary>
 22        /// A file stream.
 23        /// </summary>
 24        private Stream fileStream;
 25
 26        /// <summary>
 27        /// An archive.
 28        /// </summary>
 29        private IWriter archive;
 30
 31        /// <summary>
 32        /// The file keys.
 33        /// </summary>
 34        private SortedSet<string> keys;
 35
 36        /// <summary>
 37        /// Initializes a new instance of the <see cref="ArchiveWritableFileLocator"/> class.
 38        /// </summary>
 39        /// <param name="root">The root path.</param>
 40        /// <param name="archiveType">The archive type.</param>
 41        /// <param name="compressionType">The compression type.</param>
 042        public ArchiveWritableFileLocator(string root, ArchiveType archiveType, CompressionType compressionType)
 43        {
 044            this.keys = new SortedSet<string>();
 45
 046            using (var memory = new MemoryFileLocator())
 47            {
 048                if (File.Exists(root))
 49                {
 050                    long length = 0;
 51
 052                    using (var file = File.OpenRead(root))
 53                    {
 054                        length = file.Length;
 055                    }
 56
 057                    if (length != 0)
 58                    {
 059                        using (var reader = FileLocatorFactory.Create(root))
 60                        {
 061                            memory.WriteAll(reader);
 062                        }
 63                    }
 64                }
 65
 066                this.fileStream = File.OpenWrite(root);
 067                this.archive = WriterFactory.Open(this.fileStream, archiveType, new WriterOptions(compressionType));
 68
 069                if (!memory.IsEmpty)
 70                {
 071                    this.WriteAll(memory);
 72                }
 073            }
 074        }
 75
 76        /// <summary>
 77        /// Initializes a new instance of the <see cref="ArchiveWritableFileLocator"/> class.
 78        /// </summary>
 79        /// <param name="root">A stream.</param>
 80        /// <param name="archiveType">The archive type.</param>
 81        /// <param name="compressionType">The compression type.</param>
 082        public ArchiveWritableFileLocator(Stream root, ArchiveType archiveType, CompressionType compressionType)
 83        {
 084            this.keys = new SortedSet<string>();
 85
 086            this.archive = WriterFactory.Open(root, archiveType, new WriterOptions(compressionType));
 087        }
 88
 89        /// <summary>
 90        /// Immediately releases the unmanaged resources used by the <see cref="ArchiveWritableFileLocator"/> object.
 91        /// </summary>
 92        public void Dispose()
 93        {
 094            if (this.archive != null)
 95            {
 096                this.archive.Dispose();
 097                this.archive = null;
 98            }
 99
 0100            if (this.fileStream != null)
 101            {
 0102                this.fileStream.Dispose();
 0103                this.fileStream = null;
 104            }
 105
 0106            GC.SuppressFinalize(this);
 0107        }
 108
 109        /// <summary>
 110        /// Create a file.
 111        /// </summary>
 112        /// <param name="path">A path.</param>
 113        public void Create(string path)
 114        {
 0115            if (string.IsNullOrEmpty(path))
 116            {
 0117                throw new ArgumentNullException(nameof(path));
 118            }
 119
 0120            path = Utilities.PathNormalize(path);
 121
 0122            if (this.keys.Contains(path))
 123            {
 0124                throw new ArgumentOutOfRangeException(nameof(path));
 125            }
 126
 0127            using (var buffer = new MemoryStream())
 128            {
 0129                buffer.WriteByte(0);
 0130                buffer.Seek(0, SeekOrigin.Begin);
 131
 0132                this.archive.Write(path, buffer);
 0133            }
 134
 0135            this.keys.Add(path);
 0136        }
 137
 138        /// <summary>
 139        /// Write a file.
 140        /// </summary>
 141        /// <param name="path">A path.</param>
 142        /// <param name="data">The data.</param>
 143        public void Write(string path, Stream data)
 144        {
 0145            if (string.IsNullOrEmpty(path))
 146            {
 0147                throw new ArgumentNullException(nameof(path));
 148            }
 149
 0150            if (data == null)
 151            {
 0152                throw new ArgumentNullException(nameof(data));
 153            }
 154
 0155            path = Utilities.PathNormalize(path);
 156
 0157            if (this.keys.Contains(path))
 158            {
 0159                throw new ArgumentOutOfRangeException(nameof(path));
 160            }
 161
 0162            this.archive.Write(path, data);
 0163            this.keys.Add(path);
 0164        }
 165
 166        /// <summary>
 167        /// Write the files from a file locator.
 168        /// </summary>
 169        /// <param name="locator">A file locator.</param>
 170        public void WriteAll(IFileLocator locator)
 171        {
 0172            this.WriteAll(locator, string.Empty);
 0173        }
 174
 175        /// <summary>
 176        /// Write the files from a file locator.
 177        /// </summary>
 178        /// <param name="locator">A file locator.</param>
 179        /// <param name="root">The root path.</param>
 180        public void WriteAll(IFileLocator locator, string root)
 181        {
 0182            if (locator == null)
 183            {
 0184                throw new ArgumentNullException(nameof(locator));
 185            }
 186
 0187            foreach (var file in locator.EnumerateFiles(root))
 188            {
 0189                using (var stream = locator.Open(file))
 190                {
 0191                    this.Write(file, stream);
 0192                }
 193            }
 0194        }
 195    }
 196}

https://raw.githubusercontent.com/JeremyAnsel/JeremyAnsel.IO.Locator/16c8f4344390c6376cd2208cc1ae63a39862df37/JeremyAnsel.IO.Locator/JeremyAnsel.IO.Locator/ArchiveWritableFileLocator.cs

#LineLine coverage
 1// <copyright file="ArchiveWritableFileLocator.cs" company="Jérémy Ansel">
 2// Copyright (c) 2015, 2019 Jérémy Ansel
 3// </copyright>
 4// <license>
 5// Licensed under the MIT license. See LICENSE.txt
 6// </license>
 7
 8namespace JeremyAnsel.IO.Locator
 9{
 10    using System;
 11    using System.Collections.Generic;
 12    using System.IO;
 13    using SharpCompress.Common;
 14    using SharpCompress.Writers;
 15
 16    /// <summary>
 17    /// A writable archive file locator.
 18    /// </summary>
 19    internal sealed class ArchiveWritableFileLocator : IWritableFileLocator
 20    {
 21        /// <summary>
 22        /// A file stream.
 23        /// </summary>
 24        private Stream fileStream;
 25
 26        /// <summary>
 27        /// An archive.
 28        /// </summary>
 29        private IWriter archive;
 30
 31        /// <summary>
 32        /// The file keys.
 33        /// </summary>
 34        private SortedSet<string> keys;
 35
 36        /// <summary>
 37        /// Initializes a new instance of the <see cref="ArchiveWritableFileLocator"/> class.
 38        /// </summary>
 39        /// <param name="root">The root path.</param>
 40        /// <param name="archiveType">The archive type.</param>
 41        /// <param name="compressionType">The compression type.</param>
 442        public ArchiveWritableFileLocator(string root, ArchiveType archiveType, CompressionType compressionType)
 43        {
 444            this.keys = new SortedSet<string>();
 45
 446            using (var memory = new MemoryFileLocator())
 47            {
 448                if (File.Exists(root))
 49                {
 450                    long length = 0;
 51
 452                    using (var file = File.OpenRead(root))
 53                    {
 454                        length = file.Length;
 455                    }
 56
 457                    if (length != 0)
 58                    {
 259                        using (var reader = FileLocatorFactory.Create(root))
 60                        {
 261                            memory.WriteAll(reader);
 262                        }
 63                    }
 64                }
 65
 466                this.fileStream = File.OpenWrite(root);
 467                this.archive = WriterFactory.Open(this.fileStream, archiveType, new WriterOptions(compressionType));
 68
 469                if (!memory.IsEmpty)
 70                {
 271                    this.WriteAll(memory);
 72                }
 473            }
 474        }
 75
 76        /// <summary>
 77        /// Initializes a new instance of the <see cref="ArchiveWritableFileLocator"/> class.
 78        /// </summary>
 79        /// <param name="root">A stream.</param>
 80        /// <param name="archiveType">The archive type.</param>
 81        /// <param name="compressionType">The compression type.</param>
 082        public ArchiveWritableFileLocator(Stream root, ArchiveType archiveType, CompressionType compressionType)
 83        {
 084            this.keys = new SortedSet<string>();
 85
 086            this.archive = WriterFactory.Open(root, archiveType, new WriterOptions(compressionType));
 087        }
 88
 89        /// <summary>
 90        /// Immediately releases the unmanaged resources used by the <see cref="ArchiveWritableFileLocator"/> object.
 91        /// </summary>
 92        public void Dispose()
 93        {
 494            if (this.archive != null)
 95            {
 496                this.archive.Dispose();
 497                this.archive = null;
 98            }
 99
 4100            if (this.fileStream != null)
 101            {
 4102                this.fileStream.Dispose();
 4103                this.fileStream = null;
 104            }
 105
 4106            GC.SuppressFinalize(this);
 4107        }
 108
 109        /// <summary>
 110        /// Create a file.
 111        /// </summary>
 112        /// <param name="path">A path.</param>
 113        public void Create(string path)
 114        {
 4115            if (string.IsNullOrEmpty(path))
 116            {
 0117                throw new ArgumentNullException(nameof(path));
 118            }
 119
 4120            path = Utilities.PathNormalize(path);
 121
 4122            if (this.keys.Contains(path))
 123            {
 0124                throw new ArgumentOutOfRangeException(nameof(path));
 125            }
 126
 4127            using (var buffer = new MemoryStream())
 128            {
 4129                buffer.WriteByte(0);
 4130                buffer.Seek(0, SeekOrigin.Begin);
 131
 4132                this.archive.Write(path, buffer);
 4133            }
 134
 4135            this.keys.Add(path);
 4136        }
 137
 138        /// <summary>
 139        /// Write a file.
 140        /// </summary>
 141        /// <param name="path">A path.</param>
 142        /// <param name="data">The data.</param>
 143        public void Write(string path, Stream data)
 144        {
 2145            if (string.IsNullOrEmpty(path))
 146            {
 0147                throw new ArgumentNullException(nameof(path));
 148            }
 149
 2150            if (data == null)
 151            {
 0152                throw new ArgumentNullException(nameof(data));
 153            }
 154
 2155            path = Utilities.PathNormalize(path);
 156
 2157            if (this.keys.Contains(path))
 158            {
 0159                throw new ArgumentOutOfRangeException(nameof(path));
 160            }
 161
 2162            this.archive.Write(path, data);
 2163            this.keys.Add(path);
 2164        }
 165
 166        /// <summary>
 167        /// Write the files from a file locator.
 168        /// </summary>
 169        /// <param name="locator">A file locator.</param>
 170        public void WriteAll(IFileLocator locator)
 171        {
 2172            this.WriteAll(locator, string.Empty);
 2173        }
 174
 175        /// <summary>
 176        /// Write the files from a file locator.
 177        /// </summary>
 178        /// <param name="locator">A file locator.</param>
 179        /// <param name="root">The root path.</param>
 180        public void WriteAll(IFileLocator locator, string root)
 181        {
 2182            if (locator == null)
 183            {
 0184                throw new ArgumentNullException(nameof(locator));
 185            }
 186
 8187            foreach (var file in locator.EnumerateFiles(root))
 188            {
 2189                using (var stream = locator.Open(file))
 190                {
 2191                    this.Write(file, stream);
 2192                }
 193            }
 2194        }
 195    }
 196}
 197