< Summary

Line coverage
25%
Covered lines: 21
Uncovered lines: 63
Coverable lines: 84
Total lines: 345
Line coverage: 25%
Branch coverage
26%
Covered branches: 15
Total branches: 56
Branch coverage: 26.7%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
File 1: .ctor(...)100%210%
File 1: .ctor(...)0%620%
File 1: .ctor(...)100%210%
File 1: Dispose()0%2040%
File 1: Exists(...)0%7280%
File 1: Open(...)0%7280%
File 1: EnumerateFiles()100%210%
File 1: EnumerateFiles()0%4260%
File 2: .ctor(...)100%11100%
File 2: .ctor(...)0%620%
File 2: .ctor(...)100%210%
File 2: Dispose()75%4.25475%
File 2: Exists(...)0%7280%
File 2: Open(...)75%9875%
File 2: EnumerateFiles()100%11100%
File 2: EnumerateFiles()100%66100%

File(s)

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

#LineLine coverage
 1// <copyright file="ArchiveFileLocator.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.Archives;
 14
 15    /// <summary>
 16    /// An archive file locator.
 17    /// </summary>
 18    internal sealed class ArchiveFileLocator : IFileLocator
 19    {
 20        /// <summary>
 21        /// A file stream.
 22        /// </summary>
 23        private Stream? fileStream;
 24
 25        /// <summary>
 26        /// an archive.
 27        /// </summary>
 28        private IArchive? archive;
 29
 30        /// <summary>
 31        /// Initializes a new instance of the <see cref="ArchiveFileLocator"/> class.
 32        /// </summary>
 33        /// <param name="root">The root path.</param>
 034        public ArchiveFileLocator(string root)
 35        {
 036            this.archive = ArchiveFactory.Open(root);
 037        }
 38
 39        /// <summary>
 40        /// Initializes a new instance of the <see cref="ArchiveFileLocator"/> class.
 41        /// </summary>
 42        /// <param name="locator">A file locator.</param>
 43        /// <param name="root">The root path.</param>
 044        public ArchiveFileLocator(IFileLocator? locator, string root)
 45        {
 046            if (locator != null)
 47            {
 048                this.fileStream = locator.Open(root);
 049                this.archive = ArchiveFactory.Open(this.fileStream);
 50            }
 51            else
 52            {
 053                this.archive = ArchiveFactory.Open(root);
 54            }
 055        }
 56
 57        /// <summary>
 58        /// Initializes a new instance of the <see cref="ArchiveFileLocator"/> class.
 59        /// </summary>
 60        /// <param name="root">A stream.</param>
 061        public ArchiveFileLocator(Stream root)
 62        {
 063            this.archive = ArchiveFactory.Open(root);
 064        }
 65
 66        /// <summary>
 67        /// Immediately releases the unmanaged resources used by the <see cref="ArchiveFileLocator"/> object.
 68        /// </summary>
 69        public void Dispose()
 70        {
 071            if (this.fileStream != null)
 72            {
 073                this.fileStream.Dispose();
 074                this.fileStream = null;
 75            }
 76
 077            if (this.archive != null)
 78            {
 079                this.archive.Dispose();
 080                this.archive = null;
 81            }
 82
 083            GC.SuppressFinalize(this);
 084        }
 85
 86        /// <summary>
 87        /// Indicates whether the specified path exists.
 88        /// </summary>
 89        /// <param name="path">A path.</param>
 90        /// <returns>A boolean.</returns>
 91        public bool Exists(string path)
 92        {
 093            if (string.IsNullOrEmpty(path))
 94            {
 095                return false;
 96            }
 97
 098            foreach (var entry in this.archive!.Entries)
 99            {
 0100                if (entry.IsDirectory)
 101                {
 102                    continue;
 103                }
 104
 0105                if (Utilities.PathEquals(entry.Key, path))
 106                {
 0107                    return true;
 108                }
 109            }
 110
 0111            return false;
 0112        }
 113
 114        /// <summary>
 115        /// Open a file.
 116        /// </summary>
 117        /// <param name="path">A path.</param>
 118        /// <returns>A stream.</returns>
 119        public Stream Open(string path)
 120        {
 0121            if (string.IsNullOrEmpty(path))
 122            {
 0123                throw new ArgumentNullException(nameof(path));
 124            }
 125
 0126            foreach (var entry in this.archive!.Entries)
 127            {
 0128                if (entry.IsDirectory)
 129                {
 130                    continue;
 131                }
 132
 0133                if (Utilities.PathEquals(entry.Key, path))
 134                {
 0135                    return entry.OpenEntryStream();
 136                }
 137            }
 138
 0139            throw new FileNotFoundException();
 0140        }
 141
 142        /// <summary>
 143        /// Enumerate the files.
 144        /// </summary>
 145        /// <returns>An enumeration.</returns>
 146        public IEnumerable<string> EnumerateFiles()
 147        {
 0148            return this.EnumerateFiles(string.Empty);
 149        }
 150
 151        /// <summary>
 152        /// Enumerate the files.
 153        /// </summary>
 154        /// <param name="root">The root path.</param>
 155        /// <returns>An enumeration.</returns>
 156        public IEnumerable<string> EnumerateFiles(string root)
 157        {
 0158            foreach (var entry in this.archive!.Entries)
 159            {
 0160                if (entry.IsDirectory)
 161                {
 162                    continue;
 163                }
 164
 0165                if (Utilities.PathStartsWith(entry.Key, root))
 166                {
 0167                    yield return Utilities.PathNormalize(entry.Key);
 168                }
 169            }
 0170        }
 171    }
 172}

https://raw.githubusercontent.com/JeremyAnsel/JeremyAnsel.IO.Locator/90359a183919dae2d6e9fc4cabae797b0df998d4/JeremyAnsel.IO.Locator/JeremyAnsel.IO.Locator/ArchiveFileLocator.cs

#LineLine coverage
 1// <copyright file="ArchiveFileLocator.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.Archives;
 14
 15    /// <summary>
 16    /// An archive file locator.
 17    /// </summary>
 18    internal sealed class ArchiveFileLocator : IFileLocator
 19    {
 20        /// <summary>
 21        /// A file stream.
 22        /// </summary>
 23        private Stream? fileStream;
 24
 25        /// <summary>
 26        /// an archive.
 27        /// </summary>
 28        private IArchive? archive;
 29
 30        /// <summary>
 31        /// Initializes a new instance of the <see cref="ArchiveFileLocator"/> class.
 32        /// </summary>
 33        /// <param name="root">The root path.</param>
 634        public ArchiveFileLocator(string root)
 35        {
 636            this.archive = ArchiveFactory.Open(root);
 637        }
 38
 39        /// <summary>
 40        /// Initializes a new instance of the <see cref="ArchiveFileLocator"/> class.
 41        /// </summary>
 42        /// <param name="locator">A file locator.</param>
 43        /// <param name="root">The root path.</param>
 044        public ArchiveFileLocator(IFileLocator? locator, string root)
 45        {
 046            if (locator != null)
 47            {
 048                this.fileStream = locator.Open(root);
 049                this.archive = ArchiveFactory.Open(this.fileStream);
 50            }
 51            else
 52            {
 053                this.archive = ArchiveFactory.Open(root);
 54            }
 055        }
 56
 57        /// <summary>
 58        /// Initializes a new instance of the <see cref="ArchiveFileLocator"/> class.
 59        /// </summary>
 60        /// <param name="root">A stream.</param>
 061        public ArchiveFileLocator(Stream root)
 62        {
 063            this.archive = ArchiveFactory.Open(root);
 064        }
 65
 66        /// <summary>
 67        /// Immediately releases the unmanaged resources used by the <see cref="ArchiveFileLocator"/> object.
 68        /// </summary>
 69        public void Dispose()
 70        {
 671            if (this.fileStream != null)
 72            {
 073                this.fileStream.Dispose();
 074                this.fileStream = null;
 75            }
 76
 677            if (this.archive != null)
 78            {
 679                this.archive.Dispose();
 680                this.archive = null;
 81            }
 82
 683            GC.SuppressFinalize(this);
 684        }
 85
 86        /// <summary>
 87        /// Indicates whether the specified path exists.
 88        /// </summary>
 89        /// <param name="path">A path.</param>
 90        /// <returns>A boolean.</returns>
 91        public bool Exists(string path)
 92        {
 093            if (string.IsNullOrEmpty(path))
 94            {
 095                return false;
 96            }
 97
 098            foreach (var entry in this.archive!.Entries)
 99            {
 0100                if (entry.IsDirectory)
 101                {
 102                    continue;
 103                }
 104
 0105                if (Utilities.PathEquals(entry.Key, path))
 106                {
 0107                    return true;
 108                }
 109            }
 110
 0111            return false;
 0112        }
 113
 114        /// <summary>
 115        /// Open a file.
 116        /// </summary>
 117        /// <param name="path">A path.</param>
 118        /// <returns>A stream.</returns>
 119        public Stream Open(string path)
 120        {
 3121            if (string.IsNullOrEmpty(path))
 122            {
 0123                throw new ArgumentNullException(nameof(path));
 124            }
 125
 9126            foreach (var entry in this.archive!.Entries)
 127            {
 3128                if (entry.IsDirectory)
 129                {
 130                    continue;
 131                }
 132
 3133                if (Utilities.PathEquals(entry.Key, path))
 134                {
 3135                    return entry.OpenEntryStream();
 136                }
 137            }
 138
 0139            throw new FileNotFoundException();
 3140        }
 141
 142        /// <summary>
 143        /// Enumerate the files.
 144        /// </summary>
 145        /// <returns>An enumeration.</returns>
 146        public IEnumerable<string> EnumerateFiles()
 147        {
 3148            return this.EnumerateFiles(string.Empty);
 149        }
 150
 151        /// <summary>
 152        /// Enumerate the files.
 153        /// </summary>
 154        /// <param name="root">The root path.</param>
 155        /// <returns>An enumeration.</returns>
 156        public IEnumerable<string> EnumerateFiles(string root)
 157        {
 30158            foreach (var entry in this.archive!.Entries)
 159            {
 9160                if (entry.IsDirectory)
 161                {
 162                    continue;
 163                }
 164
 9165                if (Utilities.PathStartsWith(entry.Key, root))
 166                {
 9167                    yield return Utilities.PathNormalize(entry.Key);
 168                }
 169            }
 6170        }
 171    }
 172}
 173