public static FileSection ReadFileProperty(this Stream stream, byte[] buf, UmdFile file) { //#开头 int start = stream.ReadByte(); if (start != '#') { stream.Position = stream.Position - 1; return(null); } //两个字节,表示类别 stream.ReadLength(buf, 2); int type = BitConverter.ToInt16(buf, 0); if (!IsFileProperty(type)) { stream.Position = stream.Position - 3; return(null); } //一个无意义字节 stream.ReadByte(); //一个长度的字节 int len = stream.ReadByte() - 5; stream.ReadLength(buf, len); var typeEnum = (MetaDataEnum)type; switch (typeEnum) { case MetaDataEnum.FileStart: return(null); case MetaDataEnum.Title: var title = new FileTitleSection { Title = Encoding.Unicode.GetString(buf, 0, len) }; file.Title = title; return(title); case MetaDataEnum.Author: var author = new AuthorSection { Author = Encoding.Unicode.GetString(buf, 0, len) }; file.Author = author; return(author); case MetaDataEnum.Year: var year = new YearSection { Year = Encoding.Unicode.GetString(buf, 0, len) }; if (file.PublishDate == null) { file.PublishDate = new PublishDateSection(); } file.PublishDate.Year = year; return(year); case MetaDataEnum.Month: var month = new MonthSection { Month = Encoding.Unicode.GetString(buf, 0, len) }; if (file.PublishDate == null) { file.PublishDate = new PublishDateSection(); } file.PublishDate.Month = month; return(month); case MetaDataEnum.Day: var day = new DaySection { Day = Encoding.Unicode.GetString(buf, 0, len) }; if (file.PublishDate == null) { file.PublishDate = new PublishDateSection(); } file.PublishDate.Day = day; return(day); case MetaDataEnum.NovelType: var novelType = new NovelTypeSection { NovelType = Encoding.Unicode.GetString(buf, 0, len) }; file.NovelType = novelType; return(novelType); case MetaDataEnum.Publisher: var publisher = new PublisherSection { Publisher = Encoding.Unicode.GetString(buf, 0, len) }; file.Publisher = publisher; return(publisher); case MetaDataEnum.Saler: var saler = new SalerSection { Saler = Encoding.Unicode.GetString(buf, 0, len) }; file.Saler = saler; return(saler); case MetaDataEnum.ContentLength: var contentLength = new ContentLengthSection { ContentLength = BitConverter.ToInt32(buf, 0) }; file.ContentLength = contentLength; return(contentLength); default: return(null); } }
public UmdFile Parse(Stream stream) { var buf = new byte[33 * 1024]; var file = new UmdFile(); //前四个字节区分文件类型 stream.ReadLength(buf, 4); //然后是5个固定的字节 stream.ReadLength(buf, 5); //下面一个字节指定文件类型 var filetype = stream.ReadByte(); if (filetype != 1) { throw new Exception("只能读取文件类型的umd文件"); } //接下来2个随机数 stream.ReadLength(buf, 2); //接下来读取文件属性02->ob FileSection p = null; do { p = stream.ReadFileProperty(buf, file); } while (p != null); //章节偏移量 file.ChapterOffset = stream.ReadChapterOffset(buf); //章节标题 file.ChapterTitle = stream.ReadChapterTitle(buf, file.ChapterOffset.ChapterOffset.Count); //正文 file.Content = stream.ReadContent(buf); //封面 file.Cover = stream.ReadCover(buf); //页面偏移 PageOffsetItem pageoffsetItem = null; do { pageoffsetItem = stream.ReadPageOffsetItem(buf); if (file.PageOffset == null) { file.PageOffset = new PageOffsetSection { PageOffsetCollection = new List <PageOffsetItem>() }; } if (pageoffsetItem != null) { file.PageOffset.PageOffsetCollection.Add(pageoffsetItem); } } while (pageoffsetItem != null); //文件结束 9个字节 stream.ReadByte(); var end = stream.ReadByte(); if (end != 0x0c) { stream.ReadLength(buf, (int)(stream.Length - stream.Position)); Console.WriteLine("文件大小对不上"); } if (file.Content.Content == null) { file.Content.Content = new string[file.ChapterOffset.ChapterOffset.Count]; } return(file); }