private void SingleWriteReadMatchingSTREAMED(int size) { MemoryStream ms = new MemoryStream(size); CompoundFile cf = new CompoundFile(); CFStorage st = cf.RootStorage.AddStorage("MyStorage"); CFStream sm = st.AddStream("MyStream"); byte[] b = Helpers.GetBuffer(size); sm.SetData(b); cf.Save(ms); cf.Close(); CompoundFile cf2 = new CompoundFile(ms); CFStorage st2 = cf2.RootStorage.GetStorage("MyStorage"); CFStream sm2 = st2.GetStream("MyStream"); Assert.IsNotNull(sm2); Assert.IsTrue(sm2.Size == size); Assert.IsTrue(Helpers.CompareBuffer(sm2.GetData(), b)); cf2.Close(); }
public void Test_TRANSACTED_ADD_STREAM_TO_EXISTING_FILE() { String srcFilename = "report.xls"; String dstFilename = "reportOverwrite.xls"; File.Copy(srcFilename, dstFilename, true); CompoundFile cf = new CompoundFile(dstFilename, CFSUpdateMode.Update, CFSConfiguration.Default); byte[] buffer = Helpers.GetBuffer(5000); CFStream addedStream = cf.RootStorage.AddStream("MyNewStream"); addedStream.SetData(buffer); cf.Commit(); cf.Close(); if (File.Exists("reportOverwrite.xls")) { File.Delete("reportOverwrite.xls"); } }
public void Test_WRITE_READ_CFS_VERSION_4_STREAM() { String filename = "WRITE_COMMIT_READ_CFS_V4.cfs"; CompoundFile cf = new CompoundFile(CFSVersion.Ver_4, CFSConfiguration.SectorRecycle | CFSConfiguration.EraseFreeSectors); CFStorage st = cf.RootStorage.AddStorage("MyStorage"); CFStream sm = st.AddStream("MyStream"); byte[] b = Helpers.GetBuffer(227); sm.SetData(b); cf.Save(filename); cf.Close(); CompoundFile cf2 = new CompoundFile(filename); CFStorage st2 = cf2.RootStorage.GetStorage("MyStorage"); CFStream sm2 = st2.GetStream("MyStream"); Assert.IsNotNull(sm2); Assert.IsTrue(sm2.Size == b.Length); cf2.Close(); }
public void Test_WRITE_AND_READ_CFS_VERSION_4() { String filename = "WRITE_AND_READ_CFS_V4.cfs"; CompoundFile cf = new CompoundFile(CFSVersion.Ver_4, CFSConfiguration.EraseFreeSectors | CFSConfiguration.SectorRecycle); CFStorage st = cf.RootStorage.AddStorage("MyStorage"); CFStream sm = st.AddStream("MyStream"); byte[] b = new byte[220]; sm.SetData(b); cf.Save(filename); cf.Close(); CompoundFile cf2 = new CompoundFile(filename); CFStorage st2 = cf2.RootStorage.GetStorage("MyStorage"); CFStream sm2 = st2.GetStream("MyStream"); Assert.IsNotNull(sm2); Assert.IsTrue(sm2.Size == 220); cf2.Close(); }
public void Test_WRITE_MINISTREAM_READ_REWRITE_STREAM() { const int BIGGER_SIZE = 350; //const int SMALLER_SIZE = 290; const int MEGA_SIZE = 18000000; byte[] ba1 = Helpers.GetBuffer(BIGGER_SIZE, 1); byte[] ba2 = Helpers.GetBuffer(BIGGER_SIZE, 2); byte[] ba3 = Helpers.GetBuffer(BIGGER_SIZE, 3); byte[] ba4 = Helpers.GetBuffer(BIGGER_SIZE, 4); byte[] ba5 = Helpers.GetBuffer(BIGGER_SIZE, 5); //WRITE 5 (mini)streams in a compound file -- CompoundFile cfa = new CompoundFile(); CFStream myStream = cfa.RootStorage.AddStream("MyFirstStream"); Assert.IsNotNull(myStream); myStream.SetData(ba1); Assert.IsTrue(myStream.Size == BIGGER_SIZE); CFStream myStream2 = cfa.RootStorage.AddStream("MySecondStream"); Assert.IsNotNull(myStream2); myStream2.SetData(ba2); Assert.IsTrue(myStream2.Size == BIGGER_SIZE); CFStream myStream3 = cfa.RootStorage.AddStream("MyThirdStream"); Assert.IsNotNull(myStream3); myStream3.SetData(ba3); Assert.IsTrue(myStream3.Size == BIGGER_SIZE); CFStream myStream4 = cfa.RootStorage.AddStream("MyFourthStream"); Assert.IsNotNull(myStream4); myStream4.SetData(ba4); Assert.IsTrue(myStream4.Size == BIGGER_SIZE); CFStream myStream5 = cfa.RootStorage.AddStream("MyFifthStream"); Assert.IsNotNull(myStream5); myStream5.SetData(ba5); Assert.IsTrue(myStream5.Size == BIGGER_SIZE); cfa.Save("WRITE_MINISTREAM_READ_REWRITE_STREAM.cfs"); cfa.Close(); // Now get the second stream and rewrite it smaller byte[] bb = Helpers.GetBuffer(MEGA_SIZE); CompoundFile cfb = new CompoundFile("WRITE_MINISTREAM_READ_REWRITE_STREAM.cfs"); CFStream myStreamB = cfb.RootStorage.GetStream("MySecondStream"); Assert.IsNotNull(myStreamB); myStreamB.SetData(bb); Assert.IsTrue(myStreamB.Size == MEGA_SIZE); byte[] bufferB = myStreamB.GetData(); cfb.Save("WRITE_MINISTREAM_READ_REWRITE_STREAM_2ND.cfs"); cfb.Close(); CompoundFile cfc = new CompoundFile("WRITE_MINISTREAM_READ_REWRITE_STREAM_2ND.cfs"); CFStream myStreamC = cfc.RootStorage.GetStream("MySecondStream"); Assert.IsTrue(myStreamC.Size == MEGA_SIZE, "DATA SIZE FAILED"); byte[] bufferC = myStreamC.GetData(); Assert.IsTrue(Helpers.CompareBuffer(bufferB, bufferC), "DATA INTEGRITY FAILED"); cfc.Close(); if (File.Exists("WRITE_MINISTREAM_READ_REWRITE_STREAM.cfs")) { File.Delete("WRITE_MINISTREAM_READ_REWRITE_STREAM.cfs"); } if (File.Exists("WRITE_MINISTREAM_READ_REWRITE_STREAM_2ND.cfs")) { File.Delete("WRITE_MINISTREAM_READ_REWRITE_STREAM_2ND.cfs"); } }
static void Main(string[] args) { bool updating = false; int index; string lang; string installDir; string bakfile; string file; string config = Path.GetFullPath("panyi.ini"); Console.WriteLine( @",-------------------------------------------------. | panyi 1.1 | | Copyright 2018 zeffy <https://github.com/zeffy> | | Simple Understanding Public License v1 (SUPL) | `+---------------------------------------------+-' | OpenMcdf 2.1 | | Copyright (c) 2010-2018, Federico Blaseotto | | Mozilla Public License 2.0 (MPL-2.0) | `---------------------------------------------' "); if (!File.Exists(config)) { Console.Write("panyi.ini is missing! Download the latest copy? [y/n] "); string input = Console.ReadLine().Trim().ToLowerInvariant(); if (!string.IsNullOrEmpty(input) && new[] { "yes", "y" }.Contains(input)) { updating = true; } else { Console.Write("\nCancelled by user! Press any key to exit... "); Console.ReadKey(true); return; } } else { updating = Array.IndexOf(args, "-update") != -1; } if (updating) { using (var wc = new WebClient()) { string part = config + ".part"; Console.Write("Downloading latest panyi.ini... "); try { wc.DownloadFile( "https://raw.githubusercontent.com/zeffy/panyi/master/panyi/panyi.ini", part); Console.WriteLine("Finished!\n"); } catch (WebException) { Console.WriteLine("Failed!\n\n" + "You can download it manually from https://git.io/fAdWN"); Console.Write("\nPress any key to exit... "); Console.ReadKey(true); return; } if (File.Exists(config)) { bakfile = config + DateTime.Now.ToString("_yyyy-MM-dd_HH-mm-ss.bak"); File.Copy(config, bakfile); Console.WriteLine("Copied old panyi.ini to \"{0}\"", bakfile); } File.Move(part, config); Console.Write("\nDone! Press any key to exit... "); Console.ReadKey(true); return; } } if ((index = Array.IndexOf(args, "-lang")) != -1) { if (++index >= args.Length) { return; } lang = args[index]; } else { lang = "en"; } if ((index = Array.IndexOf(args, "-file")) != -1) { if (++index >= args.Length) { return; } file = args[index]; } else { installDir = Registry.GetValue( @"HKEY_CURRENT_USER\Software\Baidu\BaiduYunGuanjia", "installDir", null) as string; if (string.IsNullOrEmpty(installDir)) { return; } file = installDir + "\\resource.db"; } using (var fs = new FileStream(file, FileMode.Open, FileAccess.ReadWrite, FileShare.Read)) using (var cf = new CompoundFile(fs, CFSUpdateMode.Update, CFSConfiguration.SectorRecycle | CFSConfiguration.EraseFreeSectors)) { CFStream cfs = cf.RootStorage.GetStream("StringTable.xml"); XDocument xd = XDocument.Parse(Encoding.UTF8.GetString(cfs.GetData())); string id; if (Array.IndexOf(args, "-x") != -1) { Console.WriteLine("Extracting cn string table to panyi.ini..."); foreach (var element in xd.Root.Elements("String")) { if (!NativeMethods.WritePrivateProfileString("cn", (string)element.Attribute("id"), (string)element.Attribute("value"), config)) { Console.WriteLine("Failed to write string!"); } } } else { bakfile = file + DateTime.Now.ToString("_yyyy-MM-dd_HH-mm-ss.bak"); File.Copy(file, bakfile); Console.WriteLine("Copied old resource.db to \"{0}\"", bakfile); Console.WriteLine("Translating string table to {0}...", lang); var sb = new StringBuilder(0x200); foreach (var element in xd.Root.Elements("String")) { id = (string)element.Attribute("id"); if (NativeMethods.GetPrivateProfileString(lang, id, "", sb, (uint)sb.MaxCapacity, config) > 0) { element.Attribute("value").SetValue(sb.ToString()); } } cfs.SetData(Encoding.UTF8.GetBytes(xd.ToString())); cf.Commit(true); } } Console.Write("\nDone! Press any key to exit... "); Console.ReadKey(true); return; }
/// <summary> /// Applies changes. /// </summary> public void ApplyChanges() { _hasChanges = false; _modifiedStream.SetData(this._bytes.ToArray()); }
//Writes binary to storage void WriteBinaryFile(byte[] b, CFItem Itm) { CFStream Strm = (CFStream)Itm; Strm.SetData(b); }
private void AddStreamToStorage(CFStorage storage, CFStream stream) { CFStream cfStream = storage.AddStream(stream.Name); cfStream.SetData(stream.GetData()); }