示例#1
0
        /// <summary>
        /// Writes a message file.
        /// </summary>
        /// <param name="path">The file path.</param>
        /// <param name="msg">The message.</param>
        /// <param name="msgInfo">The message metadata.</param>
        internal void WriteMessage(string path, QueuedMsg msg, QueuedMsgInfo msgInfo)
        {
            byte[] md5Hash;

            using (var fsMsg = new EnhancedFileStream(path, FileMode.Create, FileAccess.ReadWrite))
            {
                // Write the file header

                fsMsg.WriteInt32(MsgMagic);         // Magic Number
                fsMsg.WriteInt32(0);                // Format Version
                fsMsg.WriteInt32(0);                // Reserved
                fsMsg.WriteBytesNoLen(md5Zeros);    // MD5 hash placeholder

                // Write the message body.

                fsMsg.WriteBytes32(msg.BodyRaw);

                // Write the metadata.

                fsMsg.WriteString16(msgInfo.ToString());

                // Compute and save the MD5 hash

                fsMsg.Position = MsgHeaderSize;
                md5Hash        = MD5Hasher.Compute(fsMsg, fsMsg.Length - fsMsg.Position);
                fsMsg.Position = MsgMD5Offset;
                fsMsg.WriteBytesNoLen(md5Hash);
            }
        }
示例#2
0
        public void Package_AddFile()
        {
            Package              package;
            EnhancedStream       fs;
            EnhancedMemoryStream es = new EnhancedMemoryStream();
            PackageEntry         entry;

            byte[] buf;
            string inputFile;
            string outputFile;

            inputFile  = Path.GetTempFileName();
            outputFile = Path.GetTempFileName();
            try
            {
                buf = new byte[37000];
                for (int i = 0; i < buf.Length; i++)
                {
                    buf[i] = (byte)i;
                }

                fs = new EnhancedFileStream(inputFile, FileMode.OpenOrCreate);
                fs.WriteBytes32(buf);
                fs.Close();

                //-------------------------

                package = new Package();
                package.Create(es);

                entry = package.AddFile("/Foo/Bar/Test1.dat", inputFile);
                Assert.IsTrue(entry.IsFile);
                Assert.IsTrue(package["/Foo"].IsFolder);
                Assert.IsTrue(package["/Foo/Bar"].IsFolder);
                Assert.IsTrue(package["/Foo/Bar/Test1.dat"].IsFile);

                package.Close(true);

                //-------------------------

                package = new Package(es);

                Assert.IsTrue(entry.IsFile);
                Assert.IsTrue(package["/Foo"].IsFolder);
                Assert.IsTrue(package["/Foo/Bar"].IsFolder);
                Assert.IsTrue(package["/Foo/Bar/Test1.dat"].IsFile);

                package["/Foo/Bar/Test1.dat"].GetContents(outputFile);

                fs = new EnhancedFileStream(outputFile, FileMode.Open);
                try
                {
                    CollectionAssert.AreEqual(buf, fs.ReadBytes32());
                }
                finally
                {
                    fs.Close();
                }

                package.Close();
            }
            finally
            {
                File.Delete(inputFile);
                File.Delete(outputFile);
            }
        }