public static void WriteTo(Sheet sheet, string targetDir) { string filePath = $"{targetDir}/{sheet.Name}.ndb"; byte[] fieldBytes = GetFieldBytes(sheet.Fields, out int dataSize); byte[] strBytes = GetStringBytes(sheet, out Dictionary <string, int> strOffsetDic); byte[] arrayBytes = GetArrayBytes(sheet, strOffsetDic, out Dictionary <string, int> arrayOffsetDic); byte[] dataBytes = GetDataBytes(sheet, strOffsetDic, arrayOffsetDic); NDBHeader header = new NDBHeader(); header.version = 1; header.dataCount = sheet.LineCount; header.fieldCount = sheet.FieldCount; header.dataSize = dataSize; header.dataOffset = NDBHeader.Size + fieldBytes.Length; header.stringOffset = header.dataOffset + dataBytes.Length; header.arrayOffset = header.stringOffset + strBytes.Length; byte[] headerBytes = NDBConst.StructToByte(header, NDBHeader.Size); using (FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None)) { fs.Write(headerBytes, 0, headerBytes.Length); fs.Write(fieldBytes, 0, fieldBytes.Length); fs.Write(dataBytes, 0, dataBytes.Length); fs.Write(strBytes, 0, strBytes.Length); fs.Write(arrayBytes, 0, arrayBytes.Length); fs.Flush(); } }
private static byte[] GetFieldBytes(List <Field> fields, out int dataSize) { dataSize = 0; MemoryStream stream = new MemoryStream(); foreach (var field in fields) { NDBFieldType ndbFieldType = GetNDBFieldType(field); dataSize += NDBConst.GetFieldSize(ndbFieldType); string fieldNameValue = field.Name; byte[] fieldTypeBytes = BitConverter.GetBytes((int)ndbFieldType); byte[] fieldNameBytes = Encoding.UTF8.GetBytes(fieldNameValue); byte[] fieldNameLenBytes = BitConverter.GetBytes(fieldNameBytes.Length); stream.Write(fieldTypeBytes, 0, fieldTypeBytes.Length); stream.Write(fieldNameLenBytes, 0, fieldNameLenBytes.Length); stream.Write(fieldNameBytes, 0, fieldNameBytes.Length); } return(stream.ToArray()); }