public void Close() { if (_BookStream != null) { _BookStream.Dispose(); _BookStream = null; } InnerAuthor = null; InnerTitle = null; _Catalogue = null; _MD5 = null; }
private void Open(String path) { try { _BookStream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.None); var infoBuffer = new Byte[78]; if (_BookStream.Read(infoBuffer, 0, 78) != 78 && infoBuffer[64] != 0x4D || infoBuffer[65] != 0x54 || infoBuffer[66] != 0x49 || infoBuffer[67] != 0x55) { throw new NotSupportedException(); } InnerAuthor = GetInfo(infoBuffer, 0, 34, true); var record = ByteArrayToNumber(infoBuffer, 76, 2); _Catalogue = new CatalogueInfo(record - 2); //读取文件索引 infoBuffer = new Byte[record << 3]; if (_BookStream.Read(infoBuffer, 0, infoBuffer.Length) != infoBuffer.Length) { throw new NotSupportedException(); } //根据索引读取电子书属性区内容 var bookInfoStop = ByteArrayToNumber(infoBuffer, 8, 4); var dataBuffer = new Byte[bookInfoStop - ByteArrayToNumber(infoBuffer, 0, 4)]; if (_BookStream.Read(dataBuffer, 0, dataBuffer.Length) != dataBuffer.Length) { throw new NotSupportedException(); } var startIndex = 10; var flag = dataBuffer.QSIndexOf(THREEESC, startIndex);//书名,必须有,所以8+2 if (flag == -1) { throw new NotSupportedException(); } InnerTitle = GetInfo(dataBuffer, 8, flag - 8); startIndex = flag + 6; //章节数 flag = dataBuffer.QSIndexOf(ESC, startIndex); if (flag == -1) { throw new NotSupportedException(); } var numStr = Encoding.ASCII.GetString(dataBuffer, startIndex, flag - startIndex); if (!Int32.TryParse(numStr, out var num) || num != record - 2) { throw new NotSupportedException(); } startIndex = flag + 2; //目录 var indexBuffer = GetInfo(dataBuffer, startIndex, dataBuffer.Length - startIndex); // var tempStringArray =Encoding.Unicode.GetString(indexBuffer).Split('\r'); //目录 Int32 PosA = bookInfoStop, PosB; var maxIndex = _Catalogue.Capacity - 1; var bookmark = (Int32)_BookStream.Length - 2; var indexStartPos = 0; var indexStopPos = 0; //读取目录信息 for (Int32 i = 0; i < _Catalogue.Capacity; i++) { PosB = (i < maxIndex) ? ByteArrayToNumber(infoBuffer, 16 + (i << 3), 4) : bookmark; indexStopPos = indexBuffer.QSIndexOf(CRNL, indexStartPos); if (indexStopPos < 0) { indexStopPos = indexBuffer.Length; } _Catalogue.Add(new ChapterInfo(PosA, PosB - PosA, Encoding.Convert(Encoding.Unicode, Encoding.UTF8, indexBuffer, indexStartPos, indexStopPos - indexStartPos))); // var q= Encoding.Unicode.GetString(indexBuffer,indexStartPos, indexStopPos - indexStartPos); indexStartPos = indexStopPos + 4; PosA = PosB; } //判断书籍长度是否合格 if (_Catalogue[maxIndex].Length + _Catalogue[maxIndex].Offset > bookmark) { throw new NotSupportedException(); } // 计算摘要 using (var md5 = new MD5CryptoServiceProvider()) { _MD5 = md5.ComputeHash(dataBuffer, 0, dataBuffer.Length); } } catch { this.Close(); } }