public Step7ProjectV5(string projectfile, bool showDeleted, Encoding prEn) { _projectfilename = projectfile; if (projectfile.ToLower().EndsWith("zip")) { _projectfilename = ZipHelper.GetFirstZipEntryWithEnding(projectfile, ".s7p"); if (string.IsNullOrEmpty(_projectfilename)) _projectfilename = ZipHelper.GetFirstZipEntryWithEnding(projectfile, ".s7l"); if (string.IsNullOrEmpty(_projectfilename)) throw new Exception("Zip-File contains no valid Step7 Project !"); this._ziphelper = new ZipHelper(projectfile); } ProjectFile = projectfile; ProjectFolder = _projectfilename.Substring(0, _projectfilename.LastIndexOf(_DirSeperator)) + _DirSeperator; ProjectEncoding = (prEn ?? Encoding.GetEncoding("ISO-8859-1")); var lngFile = _ziphelper.GetReadStream(ProjectFolder + "Global" + _DirSeperator + "Language"); if (prEn == null && lngFile != null) { var rd = new StreamReader(lngFile); string line; while ((line = rd.ReadLine()) != null) { if (line != "0") { int code; if (int.TryParse(line, out code)) { var enc = Encoding.GetEncodings().FirstOrDefault(x => x.CodePage == code); if (enc != null) { ProjectEncoding = enc.GetEncoding(); break; } } } } } LoadProjectHeader(projectfile, showDeleted); }
public Step5Project(string filename, bool showDeleted) { _showDeleted = showDeleted; _projectfilename = filename; if (filename.ToLower().EndsWith("zip")) { _projectfilename = ZipHelper.GetFirstZipEntryWithEnding(filename, ".s5d"); if (string.IsNullOrEmpty(_projectfilename)) throw new Exception("Zip-File contains no valid Step5 Project !"); this._ziphelper = new ZipHelper(filename); } ProjectFile = filename; LoadProject(); }
public Step7ProjectV5(string projectfile, bool showDeleted, Encoding prEn) { _projectfilename = projectfile; ProjectEncoding = (prEn ?? Encoding.GetEncoding("ISO-8859-1")) ; if (projectfile.ToLower().EndsWith("zip")) { _projectfilename = ZipHelper.GetFirstZipEntryWithEnding(projectfile, ".s7p"); if (string.IsNullOrEmpty(_projectfilename)) _projectfilename = ZipHelper.GetFirstZipEntryWithEnding(projectfile, ".s7l"); if (string.IsNullOrEmpty(_projectfilename)) throw new Exception("Zip-File contains no valid Step7 Project !"); this._ziphelper = new ZipHelper(projectfile); } LoadProjectHeader(projectfile, showDeleted); }
public Step7ProjectV11(string projectfile) { AppDomain currentDomain = AppDomain.CurrentDomain; currentDomain.AssemblyResolve += currentDomain_AssemblyResolve; ProjectFile = projectfile; if (ProjectFile.ToLower().EndsWith("zip")) { ProjectFile = ZipHelper.GetFirstZipEntryWithEnding(ProjectFile, ".ap11"); if (string.IsNullOrEmpty(ProjectFile)) ProjectFile = ZipHelper.GetFirstZipEntryWithEnding(ProjectFile, ".ap12"); if (string.IsNullOrEmpty(projectfile)) throw new Exception("Zip-File contains no valid TIA Project !"); this._ziphelper = new ZipHelper(projectfile); } try { var xmlDoc = new XmlDocument(); xmlDoc.Load(_ziphelper.GetReadStream(projectfile)); XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlDoc.NameTable); nsmgr.AddNamespace("x", "http://www.siemens.com/2007/07/Automation/CommonServices/DataInfoValueData"); var nd = xmlDoc.SelectSingleNode("x:Data", nsmgr); this.ProjectName = nd.Attributes["Name"].Value; } catch (Exception) { } DataFile = Path.GetDirectoryName(projectfile) + "\\System\\PEData.plf"; ProjectFolder = projectfile.Substring(0, projectfile.LastIndexOf(Path.DirectorySeparatorChar)) + Path.DirectorySeparatorChar; LoadProject(); currentDomain.AssemblyResolve -= currentDomain_AssemblyResolve; }
public MDXFile(string dbfFile, ZipHelper _ziphelper, char DirSeperator, bool ReadOnly) { this.boolReadOnly = ReadOnly; this.objMDXEntrys = new List<MDX>(MAX_TAGS); short i; this.strName = Path.GetDirectoryName(dbfFile) + DirSeperator + Path.GetFileNameWithoutExtension(dbfFile) + ".mdx"; if (_ziphelper.FileExists(this.strName)) { BinaryReader mdxReader = null; try { byte[] buffer; GCHandle handle; this.objFileStream = _ziphelper.GetReadStream(this.strName); //Create a Binary Reader for the MDX file mdxReader = new BinaryReader(objFileStream); byte[] completeBuffer = mdxReader.ReadBytes((int) _ziphelper.GetStreamLength(this.strName, objFileStream)); mdxReader.Close(); mdxReader = new BinaryReader(new MemoryStream(completeBuffer), ASCIIEncoding.ASCII); // Marshall the header into a MDXHeader structure buffer = mdxReader.ReadBytes(Marshal.SizeOf(typeof (MDXHeader))); handle = GCHandle.Alloc(buffer, GCHandleType.Pinned); this.objHeader = (MDXHeader) Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof (MDXHeader)); handle.Free(); //Read the Key Nodes for (i = 0; i < objHeader.numberOfTagsInUse; i++) { int StreamStartPosition = Marshal.SizeOf(typeof (MDXHeader)); StreamStartPosition += (this.objHeader.lengthOfTag*i); if (i < objHeader.numberOfTagsInUse) { MDX newMDX = new MDX(i); this.objMDXEntrys.Add(newMDX); newMDX.Read(mdxReader, this.objHeader.lengthOfTag, StreamStartPosition); } else { objMDXEntrys.Add(null); } } } catch (Exception e) { Debug.WriteLine(e.Message); } finally { mdxReader.Close(); } } }
private string UnzipProject(string ProjectPath, string ExtractedProjectPath) { if (ProjectPath.ToLower().EndsWith(".zip")) { // Clear extracted folder DeleteFolder(ExtractedProjectPath); try { ZipHelper zip = new ZipHelper(""); zip.UnpackToFolder(ProjectPath, ExtractedProjectPath); var Projects = Directory.EnumerateFiles(ExtractedProjectPath, "*.s7p", SearchOption.AllDirectories); if (Projects.Count() > 0) { // Return path to the s7p file return Projects.First(); } } catch (Exception ex) { EventFire.Error("S7.UnzipProject. Exception: " + ex.ToString()); return ""; } } return ""; }
private void saveZippedProject(string ExtractedProjectPath, string ProjectPath) { if (ProjectPath.ToLower().EndsWith(".s7p")) { // Unpacked project - do nothing } else if (!ProjectPath.ToLower().EndsWith(".zip")) throw new ArgumentException("Path must be .s7p or .zip!", "Path"); else { try { //if (_Simatic != null) //{ // _Simatic.Close(); // _Simatic = null; //} // Archive with SharpZipLib, it seems to be faster File.Delete(ProjectPath); ZipHelper zip = new ZipHelper(""); zip.ArchiveFolder(ExtractedProjectPath, ProjectPath); ExtractRightProjectPath = ""; ExtractLeftProjectPath = ""; } catch (Exception ex) { EventFire.Error("S7.saveZippedProject. Exception: " + ex.ToString()); } } }
private static void ReadMdxFile(string dbfFile, ZipHelper ziphelper, char dirSeperator) { string mdxFile = Path.GetDirectoryName(dbfFile) + dirSeperator + Path.GetFileNameWithoutExtension(dbfFile) + ".mdx"; //TEST MDXFile //MDXFile mdxFileObject = new MDXFile(dbfFile, ziphelper, dirSeperator, true); }
public static DataTable ReadDBF(string dbfFile, ZipHelper ziphelper, char dirSeperator) { long start = DateTime.Now.Ticks; var dt = new DataTable(); // If there isn't even a file, just return an empty DataTable if ((false == ziphelper.FileExists(dbfFile))) { return dt; } OpenMemoFile(dbfFile, ziphelper, dirSeperator); ReadMdxFile(dbfFile, ziphelper, dirSeperator); //Dictionary<int, byte[]> memoLookup = ReadDBT(dbfFile); try { // Read the header into a buffer using (Stream tmpStream = ziphelper.GetReadStream(dbfFile)) { //BinaryReader zipbr = new BinaryReader(tmpStream); using (var br = new BinaryReader(tmpStream)) { DBFHeader header = ReadDBFHeader(br); // Read in all the field descriptors. Per the spec, 13 (0D) marks the end of the field descriptors ArrayList fields = ReadDBFFields(br); dt = FillColumnsInDataTable(dt, br, header, fields); // Skip past the end of the header. (br.BaseStream).Seek(header.headerLen, SeekOrigin.Begin); // Read in all the records for (int counter = 0; counter <= header.numRecords - 1; counter++) { // 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 byte[] buffer = br.ReadBytes(header.recordLen); using (var recReader = new BinaryReader(new MemoryStream(buffer))) { FillRow(ref dt, fields, recReader); } } } } } catch { throw; } finally { if (dbtReader != null) { dbtReader.Close(); dbtReader = null; } } long count = DateTime.Now.Ticks - start; return dt; }
private static void OpenMemoFile(string dbfFile, ZipHelper ziphelper, char dirSeperator) { string dbtFile = Path.GetDirectoryName(dbfFile) + dirSeperator + Path.GetFileNameWithoutExtension(dbfFile) + ".dbt"; if (ziphelper.FileExists(dbtFile)) { dbtReader = null; try { //dbtReader = new BinaryReader(new FileStream(dbtFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)); //dbtReader=new BinaryReader(ZipHelper.GetReadStream(zipfile, dbtFile)); Stream tmpStream = ziphelper.GetReadStream(dbtFile); dbtReader = new BinaryReader(tmpStream); byte[] completeBuffer = dbtReader.ReadBytes((int)ziphelper.GetStreamLength(dbtFile, tmpStream)); dbtReader.Close(); dbtReader = new BinaryReader(new MemoryStream(completeBuffer)); // Read the header into a buffer byte[] buffer = dbtReader.ReadBytes(Marshal.SizeOf(typeof(DBTHeader))); // Marshall the header into a DBTHeader structure GCHandle handle = GCHandle.Alloc(buffer, GCHandleType.Pinned); DBTHeader header = (DBTHeader) Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof (DBTHeader)); handle.Free(); memoBlockLength = header.blockLength; } catch(Exception) { throw; } } }
/// <summary> /// This Function Writes directly to a DBF File. /// It reads the Field list, and writes to the correct position. /// To access the deleted flag, use DELETED_FLAG as column Name /// </summary> /// <param name="dbfFile"></param> /// <param name="column"></param> /// <param name="row"></param> /// <param name="value"></param> /// <param name="ziphelper"></param> /// <param name="dirSeperator"></param> /// <returns></returns> public static bool WriteValue(string dbfFile, string column, int row, object value, ZipHelper ziphelper, char dirSeperator) { //if (zipfile != null) // throw new Exception("Write to Zipped Files is not supported!"); int bytesToRecordStart = 0; long start = DateTime.Now.Ticks; // If there isn't even a file, just return an empty DataTable if ((false == ziphelper.FileExists(dbfFile))) { return false; } BinaryReader br = null; BinaryWriter bw = null; try { // Read the header into a buffer Stream tmpStream = ziphelper.GetReadStream(dbfFile); br = new BinaryReader(tmpStream); byte[] completeBuffer = br.ReadBytes((int)ziphelper.GetStreamLength(dbfFile, tmpStream)); tmpStream.Close(); br.Close(); br = new BinaryReader(new MemoryStream(completeBuffer)); byte[] buffer = br.ReadBytes(Marshal.SizeOf(typeof(DBFHeader))); // Marshall the header into a DBFHeader structure GCHandle handle = GCHandle.Alloc(buffer, GCHandleType.Pinned); DBFHeader header = (DBFHeader)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(DBFHeader)); handle.Free(); // Read in all the field descriptors. Per the spec, 13 (0D) marks the end of the field descriptors ArrayList fields = new ArrayList(); while ((13 != br.PeekChar())) { buffer = br.ReadBytes(Marshal.SizeOf(typeof(FieldDescriptor))); handle = GCHandle.Alloc(buffer, GCHandleType.Pinned); fields.Add((FieldDescriptor)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(FieldDescriptor))); handle.Free(); } char writeFieldType = ' '; int writeFieldLength = 0; foreach (FieldDescriptor field in fields) { writeFieldType = (char)field.fieldType; writeFieldLength = field.fieldLen; if (field.fieldName == column) break; bytesToRecordStart += field.fieldLen; } br.Close(); Stream strm = ziphelper.GetWriteStream(dbfFile); bw = new BinaryWriter(strm); if (column != "DELETED_FLAG") bytesToRecordStart++; else bytesToRecordStart = 0; (/*(FileStream)*/ bw.BaseStream).Seek(header.headerLen + row*header.recordLen + bytesToRecordStart, SeekOrigin.Begin); if (column == "DELETED_FLAG") bw.Write((bool) value ? Encoding.ASCII.GetBytes("*") : Encoding.ASCII.GetBytes(" ")); else { /* number = Encoding.ASCII.GetString(recReader.ReadBytes(field.fieldLen)); switch (field.fieldType) { case (byte)'N': if (number.IndexOf(".") > -1) { col = new DataColumn(field.fieldName, typeof(decimal)); } else { col = new DataColumn(field.fieldName, typeof(int)); } break; case (byte)'C': col = new DataColumn(field.fieldName, typeof(string)); break; case (byte)'T': // You can uncomment this to see the time component in the grid //col = new DataColumn(field.fieldName, typeof(string)); col = new DataColumn(field.fieldName, typeof(DateTime)); break; case (byte)'D': col = new DataColumn(field.fieldName, typeof(DateTime)); break; case (byte)'L': col = new DataColumn(field.fieldName, typeof(bool)); break; case (byte)'F': col = new DataColumn(field.fieldName, typeof(Double)); break; case (byte)'M': //Field Type Memo... col = new DataColumn(field.fieldName, typeof(byte[])); //col = new DataColumn(field.fieldName, typeof(string)); break; }*/ switch (writeFieldType) { case 'N': bw.Write(Encoding.ASCII.GetBytes(value.ToString().PadLeft(writeFieldLength, ' '))); break; case 'C': bw.Write(Encoding.ASCII.GetBytes(value.ToString().PadRight(writeFieldLength, ' '))); break; default: //br.Close(); return false; } } ziphelper.WriteBackStream(dbfFile, strm); bw.Close(); } finally { if (br != null) br.Close(); if (bw != null) bw.Close(); } return true; }
public static DataTable ReadDBF(string dbfFile, ZipHelper _ziphelper, char DirSeperator) { long start = DateTime.Now.Ticks; DataTable dt = new DataTable(); BinaryReader recReader; DataRow row; int fieldIndex; if (_ziphelper == null) { _ziphelper = new ZipHelper(dbfFile); } // If there isn't even a file, just return an empty DataTable if ((false == _ziphelper.FileExists(dbfFile))) { return dt; } BinaryReader br = null; openMemoFile(dbfFile, _ziphelper, DirSeperator); readMDXFile(dbfFile, _ziphelper, DirSeperator); //Dictionary<int, byte[]> memoLookup = ReadDBT(dbfFile); try { // Read the header into a buffer //br = new BinaryReader(new FileStream(dbfFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)); Stream tmpStream = _ziphelper.GetReadStream(dbfFile); br = new BinaryReader(tmpStream); byte[] completeBuffer = br.ReadBytes((int)_ziphelper.GetStreamLength(dbfFile, tmpStream)); tmpStream.Close(); br.Close(); br = new BinaryReader(new MemoryStream(completeBuffer)); byte[] buffer = br.ReadBytes(Marshal.SizeOf(typeof(DBFHeader))); // Marshall the header into a DBFHeader structure GCHandle handle = GCHandle.Alloc(buffer, GCHandleType.Pinned); DBFHeader header = (DBFHeader)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(DBFHeader)); handle.Free(); // Read in all the field descriptors. Per the spec, 13 (0D) marks the end of the field descriptors ArrayList fields = new ArrayList(); while ((13 != br.PeekChar())) { buffer = br.ReadBytes(Marshal.SizeOf(typeof(FieldDescriptor))); handle = GCHandle.Alloc(buffer, GCHandleType.Pinned); fields.Add((FieldDescriptor)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(FieldDescriptor))); handle.Free(); } // Read in the first row of records, we need this to help determine column types below (br.BaseStream).Seek(header.headerLen + 1, SeekOrigin.Begin); buffer = br.ReadBytes(header.recordLen); recReader = new BinaryReader(new MemoryStream(buffer)); // Create the columns in our new DataTable DataColumn col = null; dt.Columns.Add(new DataColumn("DELETED_FLAG", typeof(bool))); foreach (FieldDescriptor field in fields) { byte[] NumberByteArray = recReader.ReadBytes(field.fieldLen); switch (field.fieldType) { case dBaseType.N: if (dBaseConverter.N_IsDecimal(NumberByteArray)){ col = new DataColumn(field.fieldName, typeof(decimal)); }else{ col = new DataColumn(field.fieldName, typeof(int)); } break; case dBaseType.C: col = new DataColumn(field.fieldName, typeof(string)); break; case dBaseType.T: col = new DataColumn(field.fieldName, typeof(DateTime)); break; case dBaseType.D: col = new DataColumn(field.fieldName, typeof(DateTime)); break; case dBaseType.L: col = new DataColumn(field.fieldName, typeof(bool)); break; case dBaseType.F: col = new DataColumn(field.fieldName, typeof(Double)); break; case dBaseType.M: //Field Type Memo... col = new DataColumn(field.fieldName, typeof(byte[])); break; } dt.Columns.Add(col); } // Skip past the end of the header. (br.BaseStream).Seek(header.headerLen, SeekOrigin.Begin); // Read in all the records for (int counter = 0; counter <= header.numRecords - 1; counter++) { // 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 = br.ReadBytes(header.recordLen); recReader = new BinaryReader(new MemoryStream(buffer)); // All dbf field records begin with a deleted flag field. Deleted - 0x2A (asterisk) else 0x20 (space) //if (recReader.ReadChar() == '*') //{ // continue; //} // Loop through each field in a record fieldIndex = 0; row = dt.NewRow(); char delflg = recReader.ReadChar(); if (delflg == '*') row[0] = true; else row[0] = false; foreach (FieldDescriptor field in fields) { switch (field.fieldType) { case dBaseType.N: // Number byte[] NumberBytes = recReader.ReadBytes(field.fieldLen); if (dBaseConverter.N_IsDecimal(NumberBytes)) { row[fieldIndex + 1] = dBaseConverter.N_ToDecimal(NumberBytes); } else { row[fieldIndex + 1] = dBaseConverter.N_ToInt(NumberBytes); } break; case dBaseType.C: // String row[fieldIndex + 1] = dBaseConverter.C_ToString( recReader.ReadBytes(field.fieldLen)); break; case dBaseType.M: // Memo row[fieldIndex + 1] = ReadMemoBlock(dBaseConverter.N_ToInt(recReader.ReadBytes(field.fieldLen))); break; case dBaseType.D: // Date (YYYYMMDD) DateTime DTFromFile = dBaseConverter.D_ToDateTime(recReader.ReadBytes(8)); if (DTFromFile == DateTime.MinValue) { row[fieldIndex + 1] = System.DBNull.Value; } else { row[fieldIndex] = DTFromFile; } break; case dBaseType.T: row[fieldIndex + 1] = dBaseConverter.T_ToDateTime(recReader.ReadBytes(8)); break; case dBaseType.L: // Boolean (Y/N) row[fieldIndex + 1] = dBaseConverter.L_ToBool(recReader.ReadByte()); break; case dBaseType.F: row[fieldIndex + 1] = dBaseConverter.F_ToDouble(recReader.ReadBytes(field.fieldLen)); break; } fieldIndex++; } recReader.Close(); dt.Rows.Add(row); } } catch { throw; } finally { if (null != br) { br.Close(); } if (dbtReader != null) { dbtReader.Close(); dbtReader = null; } } long count = DateTime.Now.Ticks - start; return dt; }