IdentifyCategory() публичный статический Метод

Given the first few bytes of content, decide what the FileCategory should be, based on what's in the Content.
This should be called before deciding how to set the content.
public static IdentifyCategory ( byte firstBytes ) : FileCategory
firstBytes byte At least the first four bytes of the file read in binary form - can be longer if you wish
Результат FileCategory
Пример #1
0
        public static byte[] ConvertFromWindowsNewlines(byte[] bytes)
        {
            FileCategory category = PersistenceUtilities.IdentifyCategory(bytes);

            if (!PersistenceUtilities.IsBinary(category))
            {
                string asString = FileContent.DecodeString(bytes);
                // Only evil windows gets evil windows line breaks, and only if this is some sort of ASCII:
                asString = asString.Replace("\r\n", "\n");
                return(FileContent.EncodeString(asString));
            }

            return(bytes);
        }
Пример #2
0
        /// <summary>
        /// Get a file given its name
        /// </summary>
        /// <param name="name">filename to get.  if it has no filename extension, one will be guessed at, ".ks" usually.</param>
        /// <param name="ksmDefault">true if a filename of .ksm is preferred in contexts where the extension was left off.  The default is to prefer .ks</param>
        /// <returns>the file</returns>
        public override ProgramFile GetByName(string name, bool ksmDefault = false)
        {
            try
            {
                SafeHouse.Logger.Log("Archive: Getting File By Name: " + name);
                var fileInfo = FileSearch(name, ksmDefault);
                if (fileInfo == null)
                {
                    return(null);
                }

                using (var infile = new BinaryReader(File.Open(fileInfo.FullName, FileMode.Open)))
                {
                    byte[] fileBody = ProcessBinaryReader(infile);

                    var          retFile  = new ProgramFile(fileInfo.Name);
                    FileCategory whatKind = PersistenceUtilities.IdentifyCategory(fileBody);
                    if (whatKind == FileCategory.KSM)
                    {
                        retFile.BinaryContent = fileBody;
                    }
                    else
                    {
                        retFile.StringContent = System.Text.Encoding.UTF8.GetString(fileBody);
                    }

                    if (retFile.Category == FileCategory.ASCII || retFile.Category == FileCategory.KERBOSCRIPT)
                    {
                        retFile.StringContent = retFile.StringContent.Replace("\r\n", "\n");
                    }

                    base.Add(retFile, true);

                    return(retFile);
                }
            }
            catch (Exception e)
            {
                SafeHouse.Logger.Log(e);
                return(null);
            }
        }