示例#1
0
        public static List <DBFFieldDescriptor> GetDbfSchema(string dbfFileName, Encoding encoding)
        {
            Stream stream = new FileStream(dbfFileName, FileMode.Open);

            BinaryReader reader = new BinaryReader(stream);

            byte[] buffer = reader.ReadBytes(Marshal.SizeOf(typeof(DBFHeader)));

            DBFHeader header = BinaryStream.ByteArrayToStructure <DBFHeader>(buffer);

            List <DBFFieldDescriptor> columns = new List <DBFFieldDescriptor>();

            if ((header.LengthOfHeader - 33) % 32 != 0)
            {
                throw new NotImplementedException();
            }

            int numberOfFields = (header.LengthOfHeader - 33) / 32;

            for (int i = 0; i < numberOfFields; i++)
            {
                buffer = reader.ReadBytes(Marshal.SizeOf(typeof(DBFFieldDescriptor)));

                columns.Add(DBFFieldDescriptor.Parse(buffer, encoding));
            }

            reader.Close();

            stream.Close();

            return(columns);
        }
示例#2
0
        public static void Write(string fileName, System.Data.DataTable table, Encoding encoding)
        {
            Stream stream = new FileStream(fileName, FileMode.Create);

            BinaryWriter writer = new BinaryWriter(stream);

            List <DBFFieldDescriptor> columns = MakeDbfFields(table.Columns);

            DBFHeader header = new DBFHeader(table.Rows.Count, table.Columns.Count, GetRecordLength(columns), encoding);

            writer.Write(BinaryStream.StructureToByteArray(header));

            foreach (var item in columns)
            {
                writer.Write(BinaryStream.StructureToByteArray(item));
            }

            //Terminator
            writer.Write(byte.Parse("0D", System.Globalization.NumberStyles.HexNumber));

            for (int i = 0; i < table.Rows.Count; i++)
            {
                // All dbf field records begin with a deleted flag field. Deleted - 0x2A (asterisk) else 0x20 (space)
                writer.Write(byte.Parse("20", System.Globalization.NumberStyles.HexNumber));

                for (int j = 0; j < table.Columns.Count; j++)
                {
                    byte[] temp = new byte[columns[j].Length];

                    string value = table.Rows[i][j].ToString().Trim();

                    encoding.GetBytes(value, 0, value.Length, temp, 0);

                    writer.Write(temp);
                }
            }

            //End of file
            writer.Write(byte.Parse("1A", System.Globalization.NumberStyles.HexNumber));

            writer.Close();

            stream.Close();
        }
示例#3
0
        public static void Write <T>(string fileName,
                                     List <T> values,
                                     List <Func <T, object> > mapping,
                                     List <DBFFieldDescriptor> columns,
                                     Encoding encoding)
        {
            int control = 0;

            try
            {
                if (columns.Count != mapping.Count)
                {
                    throw new NotImplementedException();
                }

                Stream stream = new FileStream(fileName, FileMode.Create);

                BinaryWriter writer = new BinaryWriter(stream);

                DBFHeader header = new DBFHeader(values.Count, mapping.Count, GetRecordLength(columns), encoding);

                writer.Write(BinaryStream.StructureToByteArray(header));

                foreach (var item in columns)
                {
                    writer.Write(BinaryStream.StructureToByteArray(item));
                }

                //Terminator
                writer.Write(byte.Parse("0D", System.Globalization.NumberStyles.HexNumber));

                for (int i = 0; i < values.Count; i++)
                {
                    control = i;
                    // All dbf field records begin with a deleted flag field. Deleted - 0x2A (asterisk) else 0x20 (space)
                    writer.Write(byte.Parse("20", System.Globalization.NumberStyles.HexNumber));

                    for (int j = 0; j < mapping.Count; j++)
                    {
                        byte[] temp = new byte[columns[j].Length];

                        object value = mapping[j](values[i]);

                        if (value != null)
                        {
                            encoding.GetBytes(value.ToString(), 0, value.ToString().Length, temp, 0);
                        }

                        string tt = encoding.GetString(temp);
                        var    le = tt.Length;
                        writer.Write(temp);
                    }
                }

                //End of file
                writer.Write(byte.Parse("1A", System.Globalization.NumberStyles.HexNumber));

                writer.Close();

                stream.Close();

                File.WriteAllText(GetCpgFileName(fileName), encoding.BodyName);
            }
            catch (Exception ex)
            {
                string message = ex.Message;

                string m2 = message + " " + control.ToString();
            }
        }
示例#4
0
        public static object[][] ReadToObject(string dbfFileName, string tableName)
        {
            Stream stream = new FileStream(dbfFileName, FileMode.Open);

            BinaryReader reader = new BinaryReader(stream);

            byte[] buffer = reader.ReadBytes(Marshal.SizeOf(typeof(DBFHeader)));

            DBFHeader header = BinaryStream.ByteArrayToStructure <DBFHeader>(buffer);

            List <DBFFieldDescriptor> columns = new List <DBFFieldDescriptor>();

            if ((header.LengthOfHeader - 33) % 32 != 0)
            {
                throw new NotImplementedException();
            }

            int numberOfFields = (header.LengthOfHeader - 33) / 32;

            for (int i = 0; i < numberOfFields; i++)
            {
                buffer = reader.ReadBytes(Marshal.SizeOf(typeof(DBFFieldDescriptor)));

                columns.Add(DBFFieldDescriptor.Parse(buffer, DBFFile.fieldsEncoding));
            }

            //System.Data.DataTable result = MakeTableSchema(tableName, columns);
            var result = new object[header.NumberOfRecords][];

            ((FileStream)reader.BaseStream).Seek(header.LengthOfHeader, SeekOrigin.Begin);

            for (int i = 0; i < header.NumberOfRecords; i++)
            {
                // First we'll read the entire record into a buffer and then read each field from the buffer
                // This helps account for any extra space at the end of each record and probably performs better
                buffer = reader.ReadBytes(header.LengthOfEachRecord);
                BinaryReader recordReader = new BinaryReader(new MemoryStream(buffer));

                // All dbf field records begin with a deleted flag field. Deleted - 0x2A (asterisk) else 0x20 (space)
                if (recordReader.ReadChar() == '*')
                {
                    continue;
                }

                object[] values = new object[columns.Count];

                for (int j = 0; j < columns.Count; j++)
                {
                    int fieldLenth = columns[j].Length;

                    values[j] = MapFunction[columns[j].Type](recordReader.ReadBytes(fieldLenth));
                }

                recordReader.Close();

                result[i] = values;
            }

            reader.Close();

            stream.Close();

            return(result);
        }