Exemple #1
0
        public void TestCreate()
        {
            ElementFactory ef = new ElementFactory();

            ef.AddType(new global::Jabber.protocol.iq.Factory());

            string g = new Guid().ToString();
            FileMap <DiscoInfo> fm = new FileMap <DiscoInfo>("test.xml", ef);

            fm.Clear();
            Assert.AreEqual(0, fm.Count);

            fm[g] = Element;
            Assert.IsTrue(fm.Contains(g));
            Assert.IsFalse(fm.Contains("foo"));
            Assert.IsInstanceOf <DiscoInfo>(fm[g]);
            Assert.AreEqual(1, fm.Count);

            // re-read, to reparse
            fm = new FileMap <DiscoInfo>("test.xml", ef);
            Assert.IsTrue(fm.Contains(g));
            Assert.IsInstanceOf <DiscoInfo>(fm[g]);

            fm[g] = null;
            Assert.AreEqual(1, fm.Count);

            fm.Remove(g);
            Assert.AreEqual(0, fm.Count);
        }
Exemple #2
0
        public void TestCreate()
        {
            ElementFactory ef = new ElementFactory();
            ef.AddType(new global::jabber.protocol.iq.Factory());

            string g = new Guid().ToString();
            FileMap<DiscoInfo> fm = new FileMap<DiscoInfo>("test.xml", ef);
            fm.Clear();
            Assert.AreEqual(0, fm.Count);

            fm[g] = Element;
            Assert.IsTrue(fm.Contains(g));
            Assert.IsFalse(fm.Contains("foo"));
            Assert.IsInstanceOfType(typeof(DiscoInfo), fm[g]);
            Assert.AreEqual(1, fm.Count);

            // re-read, to reparse
            fm = new FileMap<DiscoInfo>("test.xml", ef);
            Assert.IsTrue(fm.Contains(g));
            Assert.IsInstanceOfType(typeof(DiscoInfo), fm[g]);

            fm[g] = null;
            Assert.AreEqual(1, fm.Count);

            fm.Remove(g);
            Assert.AreEqual(0, fm.Count);
        }
        /// <summary>
        /// Appends the Files of a map to an existing Archive
        /// </summary>
        /// <param name="map">the map that contains files which need to be added to an archive</param>
        /// <param name="wrappedName">the name of the taret wrapper-file</param>
        public override void AppendFiles(FileMap map, string wrappedName)
        {
            bool existing = false;

            if (File.Exists(wrappedName))
            {
                File.Move(wrappedName, $"{wrappedName}.lock");
                existing = true;
            }

            using (TarStreamHelper helper = new TarStreamHelper(true, useGz, wrappedName))
            {
                foreach (FileMapEntry entry in map)
                {
                    TarEntry ent = TarEntry.CreateEntryFromFile(entry.LocationInFileSystem);
                    ent.Name = entry.ArchiveFileName;
                    helper.OutputStream.PutNextEntry(ent);
                    using (Stream fs = entry.Open())
                    {
                        fs.CopyTo(helper.OutputStream);
                    }

                    helper.OutputStream.CloseEntry();
                }

                if (existing)
                {
                    try
                    {
                        using (var exhelper = new TarStreamHelper(false, useGz, $"{wrappedName}.lock"))
                        {
                            UnTarFiles(".", exhelper, true, e =>
                            {
                                if (!map.Contains(e.ArchiveFileName))
                                {
                                    TarEntry ent = TarEntry.CreateTarEntry(e.ArchiveFileName);
                                    helper.OutputStream.PutNextEntry(ent);
                                    return(helper.OutputStream);
                                }

                                return(null);
                            }, (input, output) =>
                            {
                                try
                                {
                                    input.CopyTo(output);
                                }
                                finally
                                {
                                    helper.OutputStream.CloseEntry();
                                }
                            }, true);
                        }
                    }
                    finally
                    {
                        File.Delete($"{wrappedName}.lock");
                    }
                }
            }
        }
        /// <summary>
        /// Appends the Files of a map to an existing Archive
        /// </summary>
        /// <param name="map">the map that contains files which need to be added to an archive</param>
        /// <param name="wrappedName">the name of the taret wrapper-file</param>
        /// <param name="password">the password for the existing and the new zip</param>
        public void AppendFiles(FileMap map, string wrappedName, string password)
        {
            bool existing = false;

            if (File.Exists(wrappedName))
            {
                File.Move(wrappedName, $"{wrappedName}.lock");
                existing = true;
            }


            using (FileStream fst = new FileStream(wrappedName, FileMode.Create, FileAccess.ReadWrite))
            {
                var keySize = password != null ? 256 : 0;
                using (ZipOutputStream zos = new ZipOutputStream(fst))
                {
                    if (password != null)
                    {
                        zos.Password = password;
                    }

                    zos.SetLevel(compressionLevel);
                    ZipEntryFactory fac = new ZipEntryFactory {
                        IsUnicodeText = true
                    };
                    LogEnvironment.LogDebugEvent(null, (from t in map
                                                        select
                                                        WriteEntry(t,
                                                                   fac.MakeFileEntry(t.LocationInFileSystem, t.ArchiveFileName, true),
                                                                   zos, keySize)).Count().ToString(), (int)LogSeverity.Report, null);

                    if (existing)
                    {
                        using (FileStream fsti = new FileStream($"{wrappedName}.lock", FileMode.Open, FileAccess.Read))
                        {
                            UnwrapZip(fsti, ".", true, oent =>
                            {
                                if (!map.Contains(oent.ArchiveFileName))
                                {
                                    var net = fac.MakeFileEntry(oent.ArchiveFileName, false);
                                    if (keySize > 0)
                                    {
                                        net.AESKeySize = keySize;
                                    }

                                    zos.PutNextEntry(net);
                                    return(zos);
                                }

                                return(null);
                            }, (input, output) =>
                            {
                                try
                                {
                                    input.CopyTo(output);
                                }
                                finally
                                {
                                    zos.CloseEntry();
                                }
                            }, true, null);
                        }
                    }
                }
            }
        }