Exemplo n.º 1
0
        /// <summary>
        /// Opens or creates an instance of <see cref="ICompressedArchiveAccess"/> based on the value of <paramref name="mode"/> from a stream.
        /// </summary>
        /// <param name="stream">The stream to use for the compressed archive accessor.</param>
        /// <param name="format">The format of the compressed archive.</param>
        /// <param name="mode">The access mode to use for operations on the compressed archive.</param>
        /// <param name="implementation">If not <c>null</c>, use a specific implementation if possible. Otherwise, use default, or any.</param>
        /// <returns>An instance of a compressed archive accessor for the given format.</returns>
        /// <remarks>The archive takes ownership of <paramref name="stream"/> and will dispose it.</remarks>
        /// <exception cref="System.NotSupportedException">Thrown if it is not possible to locate a factory for the given <paramref name="format"/>, or
        /// if <paramref name="stream"/> was opened with an unsupported file sharing mode in use.</exception>
        /// <exception cref="System.ArgumentNullException">Thrown if <paramref name="stream"/> is <c>null</c></exception>
        /// <exception cref="System.ArgumentOutOfRangeException">Thrown if an invalid combination of file access and mode is used.</exception>
        /// <exception cref="System.ArgumentException">Thrown if invalid file access, sharing, and mode combinations are used.</exception>
        /// <exception cref="System.IO.FileFormatException">Thrown if archive was opened for reading, but is of zero size.</exception>
        /// <exception cref="System.IO.IOException">Thrown if <paramref name="stream"/> is not empty and archive was opened in Create mode.</exception>
        public static ICompressedArchiveAccess Open(Stream stream, CompressedArchiveFormat format, CompressedArchiveAccessMode mode, CompressedArchiveAccessImplementation?implementation = null)
        {
            if (!implementation.HasValue)
            {
                implementation = format.GetPreferredCompressedArchiveImplementation();
            }
            var identifier = new CompressedArchiveIdentifier(format, implementation.Value);

            CompressedArchiveAccessFactory factory;

            if (!Factories.Value.TryGetValue(identifier, out factory))
            {
                identifier = new CompressedArchiveIdentifier(format, CompressedArchiveAccessImplementation.Any);
                Factories.Value.TryGetValue(identifier, out factory);
            }

            if (factory == null)
            {
                var message = string.Format(CultureInfo.CurrentCulture, Resources.Strings.CompressedArchiveAccess_UnsupportedFormatErrorMessage_Format, format);
                throw new NotSupportedException(message);
            }
            var archive = factory(stream, mode);

            return(archive);
        }
Exemplo n.º 2
0
        public void CompressedArchiveFormat_GetPreferredCompressedArchiveImplementation_ReturnsExpectedPreferredImplementation(CompressedArchiveFormat format, CompressedArchiveAccessImplementation expectedPreferredImplementation)
        {
            var preferredImplementation = format.GetPreferredCompressedArchiveImplementation();

            Assert.Equal(expectedPreferredImplementation, preferredImplementation);
        }