Пример #1
0
        /// <summary>
        /// 匹配文件格式是否属于扩展名
        /// </summary>
        /// <param name="ext">文件名或文件扩展名</param>
        /// <param name="stream">文件流</param>
        /// <param name="allowExts">允许的扩展名(.txt;.doc)</param>
        /// <returns></returns>
        public static bool IsFormat(string ext, Stream stream, string allowExts)
        {
            if (stream == null || ext.IsNullOrEmpty() == true)
            {
                return(false);
            }


            var extArray = allowExts.Matches(@"\.\w+").Select(item => item.ToLower()).ToArray();

            if (extArray.Length == 0)
            {
                return(false);
            }

            var formatValue = ResFormat.ReadStreamFormatValue(stream);

            if (formatValue == null)
            {
                return(false);
            }

            var format = new ResFormat(Path.GetExtension(ext).ToLower(), formatValue);

            var formats = from k in ResFormat.KNOW_FORMAT_LIST
                          join e in extArray
                          on k.Extension equals e
                          select k;

            return(formats.Any(f => f.Equals(format)));
        }
Пример #2
0
        /// <summary>
        /// 流是否为指定的格式一种
        /// </summary>
        /// <param name="stream">流</param>
        /// <param name="exts">格式</param>
        /// <returns></returns>
        public static bool IsInFormat(Stream stream, string exts)
        {
            if (stream == null)
            {
                return(false);
            }

            var extArray = exts.Matches(@"\.\w+").Select(item => item.ToLower()).ToArray();

            if (extArray.Length == 0)
            {
                return(false);
            }

            var formats = from k in ResFormat.KNOW_FORMAT_LIST
                          join e in extArray
                          on k.Extension equals e
                          select k;

            var formatValue = ResFormat.ReadStreamFormatValue(stream);

            return(formats.Any(f => formatValue.SequenceEqual(f.FormatValue)));
        }