/// <summary> /// Returns the complete tree with all the <see cref="CFStorage"/> and <see cref="CFStream"/> children /// </summary> /// <param name="rootStorage"></param> /// <param name="storage"></param> private static void GetStorageChain(CFStorage rootStorage, CFStorage storage) { Logger.WriteToLog("Copying storage to compound file"); void Entries(CFItem item) { if (item.IsStorage) { var newRootStorage = rootStorage.AddStorage(item.Name); GetStorageChain(newRootStorage, item as CFStorage); } else if (item.IsStream) { var childStream = item as CFStream; if (childStream == null) { return; } var stream = rootStorage.AddStream(item.Name); var bytes = childStream.GetData(); stream.SetData(bytes); } } storage.VisitEntries(Entries, false); }
/// <summary> /// Writes the <see cref="Recipient"/> objects to the given <paramref name="rootStorage"/> /// and it will set all the needed properties /// </summary> /// <param name="rootStorage">The root <see cref="CFStorage"/></param> /// <returns> /// Total size of the written <see cref="Recipient"/> objects and it's <see cref="Properties"/> /// </returns> internal long WriteToStorage(CFStorage rootStorage) { long size = 0; for (var index = 0; index < Count; index++) { var recipient = this[index]; var storage = rootStorage.AddStorage(PropertyTags.RecipientStoragePrefix + index.ToString("X8").ToUpper()); size += recipient.WriteProperties(storage); } return(size); }
public void Test_VISIT_STORAGE() { String FILENAME = "testVisiting.xls"; // Remove... if (File.Exists(FILENAME)) { File.Delete(FILENAME); } //Create... CompoundFile ncf = new CompoundFile(); CFStorage l1 = ncf.RootStorage.AddStorage("Storage Level 1"); l1.AddStream("l1ns1"); l1.AddStream("l1ns2"); l1.AddStream("l1ns3"); CFStorage l2 = l1.AddStorage("Storage Level 2"); l2.AddStream("l2ns1"); l2.AddStream("l2ns2"); ncf.Save(FILENAME); ncf.Close(); // Read... CompoundFile cf = new CompoundFile(FILENAME); FileStream output = new FileStream("reportVisit.txt", FileMode.Create); TextWriter sw = new StreamWriter(output); Console.SetOut(sw); Action <CFItem> va = delegate(CFItem target) { sw.WriteLine(target.Name); }; cf.RootStorage.VisitEntries(va, true); cf.Close(); sw.Close(); }
private void CopyStorage(CFStorage source, CFStorage dest) { source.VisitEntries(i => { if (i.IsStorage) { CopyStorage((CFStorage)i, dest.AddStorage(i.Name)); } else if (i.IsStream) { var newStream = dest.AddStream(i.Name); var currStream = (CFStream)i; newStream.SetData(currStream.GetData()); } }, false); }
public void AddToCompoundFile(CompoundFile cf) { CFStorage projectStorage = cf.RootStorage.AddStorage("_VBA_PROJECT_CUR"); CFStorage vbaStorage = projectStorage.AddStorage("VBA"); AddStreamToStorage(projectStorage, this.ProjectWmStream); AddStreamToStorage(projectStorage, this.ProjectStream); AddStreamToStorage(vbaStorage, this.ThisWorkbookStream); AddStreamToStorage(vbaStorage, this.VbaProjectStream); AddStreamToStorage(vbaStorage, this.dirStream); foreach (var modStream in ModuleStreams) { AddStreamToStorage(vbaStorage, modStream); } }
static void CopyCfStreamsExcept(CFStorage src, CFStorage dest, string excludeName) { src.VisitEntries(i => { if (i.Name?.Equals(excludeName, StringComparison.InvariantCultureIgnoreCase) ?? false) { return; } if (i.IsStorage) { dest.AddStorage(i.Name); CopyCfStreamsExcept((CFStorage)i, dest.GetStorage(i.Name), null); } else { dest.AddStream(i.Name); dest.GetStream(i.Name).SetData(((CFStream)i).GetData()); } }, false); }
/// <summary> /// Returns the complete tree with all the <see cref="CFStorage"/> and <see cref="CFStream"/> children /// </summary> /// <param name="rootStorage"></param> /// <param name="storage"></param> private static void GetStorageChain(CFStorage rootStorage, CFStorage storage) { foreach (var child in storage.Children) { if (child.IsStorage) { var newRootStorage = rootStorage.AddStorage(child.Name); GetStorageChain(newRootStorage, child as CFStorage); } else if (child.IsStream) { var childStream = child as CFStream; if (childStream == null) { continue; } var stream = rootStorage.AddStream(child.Name); var bytes = childStream.GetData(); stream.SetData(bytes); } } }
/// <summary> /// Copies the given <paramref name="source"/> to the given <paramref name="destination"/> /// </summary> /// <param name="source"></param> /// <param name="destination"></param> public static void Copy(CFStorage source, CFStorage destination) { source.VisitEntries(action => { if (action.IsStorage) { var destionationStorage = destination.AddStorage(action.Name); destionationStorage.CLSID = action.CLSID; destionationStorage.CreationDate = action.CreationDate; destionationStorage.ModifyDate = action.ModifyDate; Copy(action as CFStorage, destionationStorage); } else { var sourceStream = action as CFStream; var destinationStream = destination.AddStream(action.Name); if (sourceStream != null) { destinationStream.SetData(sourceStream.GetData()); } } }, false); }
/// <summary> /// Returns the complete tree with all the <see cref="CFStorage"/> and <see cref="CFStream"/> children /// </summary> /// <param name="rootStorage"></param> /// <param name="storage"></param> private static void GetStorageChain(CFStorage rootStorage, CFStorage storage) { Action <CFItem> entries = item => { if (item.IsStorage) { var newRootStorage = rootStorage.AddStorage(item.Name); GetStorageChain(newRootStorage, item as CFStorage); } else if (item.IsStream) { var childStream = item as CFStream; if (childStream == null) { return; } var stream = rootStorage.AddStream(item.Name); var bytes = childStream.GetData(); stream.SetData(bytes); } }; storage.VisitEntries(entries, false); }
public void Test_FUNCTIONAL_BEHAVIOUR() { //System.Diagnostics.Trace.Listeners.Add(new ConsoleTraceListener()); const int N_FACTOR = 1; byte[] bA = Helpers.GetBuffer(20 * 1024 * N_FACTOR, 0x0A); byte[] bB = Helpers.GetBuffer(5 * 1024, 0x0B); byte[] bC = Helpers.GetBuffer(5 * 1024, 0x0C); byte[] bD = Helpers.GetBuffer(5 * 1024, 0x0D); byte[] bE = Helpers.GetBuffer(8 * 1024 * N_FACTOR + 1, 0x1A); byte[] bF = Helpers.GetBuffer(16 * 1024 * N_FACTOR, 0x1B); byte[] bG = Helpers.GetBuffer(14 * 1024 * N_FACTOR, 0x1C); byte[] bH = Helpers.GetBuffer(12 * 1024 * N_FACTOR, 0x1D); byte[] bE2 = Helpers.GetBuffer(8 * 1024 * N_FACTOR, 0x2A); byte[] bMini = Helpers.GetBuffer(1027, 0xEE); Stopwatch sw = new Stopwatch(); sw.Start(); //############ // Phase 1 var cf = new CompoundFile(CFSVersion.Ver_3, CFSConfiguration.SectorRecycle); cf.RootStorage.AddStream("A").SetData(bA); cf.Save("OneStream.cfs"); cf.Close(); // Test Phase 1 var cfTest = new CompoundFile("OneStream.cfs"); CFStream testSt = cfTest.RootStorage.GetStream("A"); Assert.IsNotNull(testSt); Assert.IsTrue(testSt.Size == bA.Length); Assert.IsTrue(Helpers.CompareBuffer(bA, testSt.GetData())); cfTest.Close(); //########### //Phase 2 cf = new CompoundFile("OneStream.cfs", CFSUpdateMode.ReadOnly, CFSConfiguration.SectorRecycle); cf.RootStorage.AddStream("B").SetData(bB); cf.RootStorage.AddStream("C").SetData(bC); cf.RootStorage.AddStream("D").SetData(bD); cf.RootStorage.AddStream("E").SetData(bE); cf.RootStorage.AddStream("F").SetData(bF); cf.RootStorage.AddStream("G").SetData(bG); cf.RootStorage.AddStream("H").SetData(bH); cf.Save("8_Streams.cfs"); cf.Close(); // Test Phase 2 cfTest = new CompoundFile("8_Streams.cfs"); testSt = cfTest.RootStorage.GetStream("B"); Assert.IsNotNull(testSt); Assert.IsTrue(testSt.Size == bB.Length); Assert.IsTrue(Helpers.CompareBuffer(bB, testSt.GetData())); testSt = cfTest.RootStorage.GetStream("C"); Assert.IsNotNull(testSt); Assert.IsTrue(testSt.Size == bC.Length); Assert.IsTrue(Helpers.CompareBuffer(bC, testSt.GetData())); testSt = cfTest.RootStorage.GetStream("D"); Assert.IsNotNull(testSt); Assert.IsTrue(testSt.Size == bD.Length); Assert.IsTrue(Helpers.CompareBuffer(bD, testSt.GetData())); testSt = cfTest.RootStorage.GetStream("E"); Assert.IsNotNull(testSt); Assert.IsTrue(testSt.Size == bE.Length); Assert.IsTrue(Helpers.CompareBuffer(bE, testSt.GetData())); testSt = cfTest.RootStorage.GetStream("F"); Assert.IsNotNull(testSt); Assert.IsTrue(testSt.Size == bF.Length); Assert.IsTrue(Helpers.CompareBuffer(bF, testSt.GetData())); testSt = cfTest.RootStorage.GetStream("G"); Assert.IsNotNull(testSt); Assert.IsTrue(testSt.Size == bG.Length); Assert.IsTrue(Helpers.CompareBuffer(bG, testSt.GetData())); testSt = cfTest.RootStorage.GetStream("H"); Assert.IsNotNull(testSt); Assert.IsTrue(testSt.Size == bH.Length); Assert.IsTrue(Helpers.CompareBuffer(bH, testSt.GetData())); cfTest.Close(); File.Copy("8_Streams.cfs", "6_Streams.cfs", true); File.Delete("8_Streams.cfs"); //########### // #if !NETCOREAPP2_0 Trace.Listeners.Add(new ConsoleTraceListener()); #endif // Phase 3 cf = new CompoundFile("6_Streams.cfs", CFSUpdateMode.Update, CFSConfiguration.SectorRecycle | CFSConfiguration.EraseFreeSectors); cf.RootStorage.Delete("D"); cf.RootStorage.Delete("G"); cf.Commit(); cf.Close(); //Test Phase 3 cfTest = new CompoundFile("6_Streams.cfs"); bool catched = false; try { testSt = cfTest.RootStorage.GetStream("D"); } catch (Exception ex) { if (ex is CFItemNotFound) { catched = true; } } Assert.IsTrue(catched); catched = false; try { testSt = cfTest.RootStorage.GetStream("G"); } catch (Exception ex) { if (ex is CFItemNotFound) { catched = true; } } Assert.IsTrue(catched); cfTest.Close(); //########## // Phase 4 File.Copy("6_Streams.cfs", "6_Streams_Shrinked.cfs", true); CompoundFile.ShrinkCompoundFile("6_Streams_Shrinked.cfs"); // Test Phase 4 Assert.IsTrue(new FileInfo("6_Streams_Shrinked.cfs").Length < new FileInfo("6_Streams.cfs").Length); cfTest = new CompoundFile("6_Streams_Shrinked.cfs"); Action <CFItem> va = delegate(CFItem item) { if (item.IsStream) { CFStream ia = item as CFStream; Assert.IsNotNull(ia); Assert.IsTrue(ia.Size > 0); byte[] d = ia.GetData(); Assert.IsNotNull(d); Assert.IsTrue(d.Length > 0); Assert.IsTrue(d.Length == ia.Size); } }; cfTest.RootStorage.VisitEntries(va, true); cfTest.Close(); //########## //Phase 5 cf = new CompoundFile("6_Streams_Shrinked.cfs", CFSUpdateMode.Update, CFSConfiguration.SectorRecycle); cf.RootStorage.AddStream("ZZZ").SetData(bF); cf.RootStorage.GetStream("E").Append(bE2); cf.Commit(); cf.Close(); cf = new CompoundFile("6_Streams_Shrinked.cfs", CFSUpdateMode.Update, CFSConfiguration.SectorRecycle); cf.RootStorage.CLSID = new Guid("EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE"); cf.Commit(); cf.Close(); cf = new CompoundFile("6_Streams_Shrinked.cfs", CFSUpdateMode.Update, CFSConfiguration.SectorRecycle); cf.RootStorage.AddStorage("MyStorage").AddStream("ZIP").Append(bE); cf.Commit(); cf.Close(); cf = new CompoundFile("6_Streams_Shrinked.cfs", CFSUpdateMode.Update, CFSConfiguration.SectorRecycle); cf.RootStorage.AddStorage("AnotherStorage").AddStream("ANS").Append(bE); cf.RootStorage.Delete("MyStorage"); cf.Commit(); cf.Close(); //Test Phase 5 //##### //Phase 6 cf = new CompoundFile("6_Streams_Shrinked.cfs", CFSUpdateMode.Update, CFSConfiguration.SectorRecycle); CFStorage root = cf.RootStorage; root.AddStorage("MiniStorage").AddStream("miniSt").Append(bMini); cf.RootStorage.GetStorage("MiniStorage").AddStream("miniSt2").Append(bMini); cf.Commit(); cf.Close(); cf = new CompoundFile("6_Streams_Shrinked.cfs", CFSUpdateMode.Update, CFSConfiguration.SectorRecycle); cf.RootStorage.GetStorage("MiniStorage").Delete("miniSt"); cf.RootStorage.GetStorage("MiniStorage").GetStream("miniSt2").Append(bE); cf.Commit(); cf.Close(); //Test Phase 6 cfTest = new CompoundFile("6_Streams_Shrinked.cfs"); byte[] d2 = cfTest.RootStorage.GetStorage("MiniStorage").GetStream("miniSt2").GetData(); Assert.IsTrue(d2.Length == (bE.Length + bMini.Length)); int cnt = 1; byte[] buf = new byte[cnt]; cnt = cfTest.RootStorage.GetStorage("MiniStorage").GetStream("miniSt2").Read(buf, bMini.Length, cnt); Assert.IsTrue(cnt == 1); Assert.IsTrue(buf[0] == 0x1A); cnt = 1; cnt = cfTest.RootStorage.GetStorage("MiniStorage").GetStream("miniSt2").Read(buf, bMini.Length - 1, cnt); Assert.IsTrue(cnt == 1); Assert.IsTrue(buf[0] == 0xEE); try { cfTest.RootStorage.GetStorage("MiniStorage").GetStream("miniSt"); } catch (Exception ex) { Assert.IsTrue(ex is CFItemNotFound); } cfTest.Close(); //############## //Phase 7 cf = new CompoundFile("6_Streams_Shrinked.cfs", CFSUpdateMode.Update, CFSConfiguration.SectorRecycle); cf.RootStorage.GetStorage("MiniStorage").GetStream("miniSt2").SetData(bA); cf.Commit(); cf.Close(); //Test Phase 7 cf = new CompoundFile("6_Streams_Shrinked.cfs", CFSUpdateMode.Update, CFSConfiguration.SectorRecycle); d2 = cf.RootStorage.GetStorage("MiniStorage").GetStream("miniSt2").GetData(); Assert.IsNotNull(d2); Assert.IsTrue(d2.Length == bA.Length); cf.Close(); //############## cf = new CompoundFile("6_Streams_Shrinked.cfs", CFSUpdateMode.ReadOnly, CFSConfiguration.SectorRecycle); var myStream = cf.RootStorage.GetStream("C"); var data = myStream.GetData(); Console.WriteLine(data[0] + " : " + data[data.Length - 1]); myStream = cf.RootStorage.GetStream("B"); data = myStream.GetData(); Console.WriteLine(data[0] + " : " + data[data.Length - 1]); cf.Close(); sw.Stop(); Console.WriteLine(sw.ElapsedMilliseconds); }
/// <summary> /// Returns the complete tree with all the <see cref="CFStorage"/> and <see cref="CFStream"/> children /// </summary> /// <param name="rootStorage"></param> /// <param name="storage"></param> private static void GetStorageChain(CFStorage rootStorage, CFStorage storage) { foreach (var child in storage.Children) { if (child.IsStorage) { var newRootStorage = rootStorage.AddStorage(child.Name); GetStorageChain(newRootStorage, child as CFStorage); } else if (child.IsStream) { var childStream = child as CFStream; if (childStream == null) continue; var stream = rootStorage.AddStream(child.Name); var bytes = childStream.GetData(); stream.SetData(bytes); } } }
/// <summary> /// Получить ссылку на объект контейнера с заполненными данными /// </summary> /// <param name="mapXml">Карта</param> /// <param name="szv3Xml">Сводная ведомость</param> /// <param name="szv2XmlArray">Описи пакетов</param> /// <param name="szv1XmlArray">Документы СЗВ-1</param> /// <param name="diskKey">Ключ</param> /// <param name="diskTable">Таблица</param> /// <returns></returns> public static CompoundFile MakeContainer(XmlDocument mapXml, XmlDocument szv3Xml, IEnumerable <XmlDocument> szv2XmlArray, IEnumerable <IEnumerable <XmlDocument> > szv1XmlArray, byte[] diskKey, byte[] diskTable) { XmlElement rootMap = mapXml[MapXml.tagTopics]; XmlElement svodRootMap = rootMap[MapXml.tagSvod]; XmlNodeList lists = rootMap.GetElementsByTagName(MapXml.tagTopics); if (lists.Count != szv2XmlArray.Count() || lists.Count != szv1XmlArray.Count()) { return(null); } CompoundFile container = new CompoundFile(CFSVersion.Ver_3, false, false); CFStorage dir4 = container.RootStorage.AddStorage(rootMap.GetAttribute(MapXml.paramID)); for (int i = 0; i < szv2XmlArray.Count(); i++) { XmlElement curList = lists[i] as XmlElement; XmlElement curOpis = curList[MapXml.tagOpis]; CFStorage curDir = dir4.AddStorage(curList.GetAttribute(MapXml.paramID)); XmlDocument szv2Xml = szv2XmlArray.ElementAt(i); CFStream opisStream = AddStream(curDir, szv2Xml, diskKey, diskTable); curOpis[MapXml.tagFilename].InnerText = opisStream.Name; XmlNodeList docs = curList.GetElementsByTagName(MapXml.tagTopic); if (docs.Count != szv1XmlArray.ElementAt(i).Count()) { continue; } for (int j = 0; j < szv1XmlArray.ElementAt(i).Count(); j++) { /*if (i >= 10 && j >= 51) * { * Console.WriteLine("Packet {0}, Doc {1}", i, j); * }*/ XmlDocument szv1Xml = szv1XmlArray.ElementAt(i).ElementAt(j); XmlElement curDoc = docs[j] as XmlElement; CFStream docStream = AddStream(curDir, szv1Xml, diskKey, diskTable); curDoc[MapXml.tagFilename].InnerText = docStream.Name; } } CFStream svod = AddStream(dir4, szv3Xml, diskKey, diskTable); svodRootMap[MapXml.tagFilename].InnerText = svod.Name; CFStream map = container.RootStorage.AddStream("map"); SetDataToStream(map, mapXml.InnerXml, diskKey, diskTable); CFStorage styleDir = container.RootStorage.AddStorage("styles"); CFStream mapStyleStream = styleDir.AddStream("map_style"); CFStream szv1StyleStream = styleDir.AddStream("szv_style"); CFStream szv3StyleStream = styleDir.AddStream("svod_style"); CFStream szv2StyleStream = styleDir.AddStream("szv_opis_style"); SetDataToStream(mapStyleStream, File.ReadAllBytes(MapXml.GetXslUrl()), diskKey, diskTable); SetDataToStream(szv1StyleStream, File.ReadAllBytes(Szv1Xml.GetXslUrl()), diskKey, diskTable); SetDataToStream(szv3StyleStream, File.ReadAllBytes(Szv3Xml.GetXslUrl()), diskKey, diskTable); SetDataToStream(szv2StyleStream, File.ReadAllBytes(Szv2Xml.GetXslUrl()), diskKey, diskTable); return(container); }
public static CFStorage GetOrAddStorage(this CFStorage storage, string storageName) { if (storage == null) { throw new ArgumentNullException(nameof(storage)); } return(storage.TryGetStorage(storageName, out var childStorage) ? childStorage : storage.AddStorage(storageName)); }