public StructureHelper RetrieveStructureTree(FileInfo sourceFile) { try { byte[] buffer; using (FileStream fs = new FileStream(sourceFile.FullName, FileMode.Open, FileAccess.Read)) using (BinaryReader br = new BinaryReader(fs)) { buffer = br.ReadBytes((int)sourceFile.Length); } byte[] decompressed = Decompress(buffer); StructureHelper sh = JsonConvert.DeserializeObject <StructureHelper>(Encoding.UTF8.GetString(decompressed)); foreach (ProfileEntry item in sh.StructureEntries) { if (item.RecordNumber == 0) { continue; } foreach (ProfileEntry parentItem in sh.StructureEntries) { if (parentItem.RecordNumber == item.ParentRecordNumber) { item.Parent = parentItem; break; } } } return(sh); } catch { return(null); } }
protected bool PersistStructureTree(StructureHelper source, string fileName) { uint marker = 0; foreach (ProfileEntry item in source.StructureEntries) { item.RecordNumber = marker++; } foreach (ProfileEntry item in source.StructureEntries) { ProfileEntry parent = item.Parent; if (parent != null) { item.ParentRecordNumber = parent.RecordNumber; } } if (!fileName.EndsWith(".gz")) { fileName += ".gz"; } byte[] bytesToCompress = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(source)); using (FileStream fileToCompress = File.Create(fileName)) using (GZipStream compressionStream = new GZipStream(fileToCompress, CompressionMode.Compress)) { compressionStream.Write(bytesToCompress, 0, bytesToCompress.Length); } return(true); }
private void PopulateInfoTree(string structureName) { int structureSize = 0; try { structureSize = (int)_profile.GetStructureSize(structureName); if (structureSize == -1) { return; } } catch (Exception) { return; } _profileEntries.Clear(); // first let's see if it already exists FileInfo cachedFile = new FileInfo(_dataProvider.CacheFolder + "\\structure_" + structureName + ".gz"); if (cachedFile.Exists) { StructureHelper cachedMap = RetrieveStructureTree(cachedFile); if (cachedMap != null) { foreach (ProfileEntry item in cachedMap.StructureEntries) { _profileEntries.Add(item); } } NotifyPropertyChange("ProfileTreeItems"); return; } string realName = structureName.TrimStart(new char[] { '_' }); ProfileEntry root = new ProfileEntry(); root.Name = realName + " [" + structureSize + "]"; root.Parent = null; root.IsExpanded = true; root.Offset = 0; root.Length = (uint)structureSize; _profileEntries.Add(root); PopulateNode(structureName, root, 0); NotifyPropertyChange("ProfileTreeItems"); // persist the tree records to save time next time StructureHelper sh = new StructureHelper(); sh.StructureEntries = new List <ProfileEntry>(); foreach (ProfileEntry e in _profileEntries) { sh.StructureEntries.Add(e); } PersistStructureTree(sh, _dataProvider.CacheFolder + "\\structure_" + structureName); }