public static SlfFile Create(DirectoryInfo aDir, string aSearchPattern, SearchOption aSearchOptions) { FileInfo[] _files = aDir.GetFiles(aSearchPattern, aSearchOptions); if (_files == null || _files.Length == 0) { return(null); } SlfFile.Header FHeader = new Header(); FHeader.LibName = String.Format("{0}.SLF", aDir.Name); if (aSearchOptions == SearchOption.AllDirectories) { FHeader.PathToLibrary = String.Format("{0}\\", aDir.Name); } else { FHeader.PathToLibrary = String.Empty; } FHeader.iEntries = _files.Length; FHeader.iUsed = _files.Length; FHeader.iSort = -1; FHeader.iVersion = 512; DirectoryInfo[] _subDirs = aDir.GetDirectories(); FHeader.fContainsSubDirectories = (byte)(_subDirs.Length > 0 ? 1 : 0); int _dirLength = aDir.FullName.Length + 1; SlfFile.Record[] _records = new SlfFile.Record[_files.Length]; SlfFile.Record[] FRecords = new SlfFile.Record[FHeader.iEntries]; for (int i = 0; i < _files.Length; i++) { FileInfo _file = _files[i]; SlfFile.Record.Header _recHeader = new Record.Header(); _recHeader.FileName = _file.FullName.Substring(_dirLength); _recHeader.FileTime = _file.CreationTimeUtc; _recHeader.uiLength = (uint)_file.Length; SlfFile.Record _rec = new Record(_recHeader); using (FileStream _fs = new FileStream(_file.FullName, FileMode.Open)) { _rec.LoadData(_fs); } FRecords[i] = _rec; string _key = _rec.FileName; if (_rec.State != FileOk) { _key = String.Format("{0}_{1}", _rec.FileName, _rec.State); } } SlfFile _slf = new SlfFile(FHeader, FRecords); return(_slf); }
// Перед сравнением имена файлов приводим к нижнему регистру. public int CompareTo(object obj) { SlfFile.Record _rec = (SlfFile.Record)obj; byte[] _xBytes = this.FHeader.sFileName; byte[] _yBytes = _rec.FHeader.sFileName; int _byteCompareResult = 0; int _minLength = Math.Min(_xBytes.Length, _yBytes.Length); for (int i = 0; i < _minLength; i++) { // to lower x byte _xByte = _xBytes[i]; if (_xByte > 64 && _xByte < 91) { _xByte += 32; } // to lower y byte _yByte = _yBytes[i]; if (_yByte > 64 && _yByte < 91) { _yByte += 32; } _byteCompareResult = _xByte.CompareTo(_yByte); if (_byteCompareResult != 0) { break; } } return(_byteCompareResult); }
public void Load() { if (File.Exists(this.FFileName)) { using (FileStream _fs = new FileStream(this.FFileName, FileMode.Open)) { this.Load(_fs); } } else { string _mapsFolder = Path.GetDirectoryName(this.FFileName); string _dataFolder = Path.GetDirectoryName(_mapsFolder); string _mapsSlfFile = Path.Combine(_dataFolder, "Maps.slf"); if (File.Exists(_mapsSlfFile)) { SlfFile _mapsSlf = new SlfFile(_mapsSlfFile); _mapsSlf.LoadRecords(); string _mapName = Path.GetFileName(this.FFileName); //SlfFile.Record _record = _mapsSlf.Records. // SingleOrDefault(x => x.FileName.ToUpperInvariant() == _mapName.ToUpperInvariant()); SlfFile.Record _record = null; foreach (SlfFile.Record _rec in _mapsSlf.Records) { if (_rec.FileName.ToUpperInvariant() == _mapName.ToUpperInvariant()) { _record = _rec; break; } } if (_record != null) { using (MemoryStream _ms = new MemoryStream(_record.Data)) { this.Load(_ms); } } else { throw new FileNotFoundException( String.Format("Record {0} is not found in file {1}", _mapName, _mapsSlfFile)); } } else { throw new FileNotFoundException( String.Format("Files {0}, {1} are not found.", this.FFileName, _mapsSlfFile)); } } }
public int Extract(bool aIsRewrite) { this.LoadRecords(); string _dirName = Path.Combine(Path.GetDirectoryName(this.FSlfFullFileName), this.PathToLibrary); if (!Directory.Exists(_dirName)) { Directory.CreateDirectory(_dirName); } int _count = 0; for (int i = 0; i < this.Entries; i++) { SlfFile.Record _entry = this.FRecords[i]; string _fileName = Path.Combine(_dirName, _entry.FileName); if (Path.GetFileNameWithoutExtension(_fileName) == Path.GetFileName(_fileName)) { continue; } string _subDirName = Path.GetDirectoryName(_fileName); if (!Directory.Exists(_subDirName)) { Directory.CreateDirectory(_subDirName); } if (!aIsRewrite && File.Exists(_fileName)) { continue; } using (FileStream _ofs = new FileStream(_fileName, FileMode.Create)) { _entry.WriteData(_ofs); } _count++; } return(_count); }