Exemplo n.º 1
0
        public void CreateZipAndTpz()
        {
            using (ZipFile zf = ZipFile.Create(QQnPath.Combine(TmpPath, "TestZip.zip")))
            {
                AddSomeFiles(zf);
            }

            AssuredStreamCreateArgs assuredArgs = new AssuredStreamCreateArgs();

            assuredArgs.StrongNameKey = StrongNameKey.LoadFrom(SnkFile);
            assuredArgs.FileType      = "TPZ-Test";

            MultipleStreamCreateArgs mutlArgs = new MultipleStreamCreateArgs();

            mutlArgs.VerificationMode = VerificationMode.Full;

            using (FileStream fileStream = File.Create(QQnPath.Combine(TmpPath, "TestTpz.zip")))
                using (AssuredStream assuredStream = new AssuredStream(fileStream, assuredArgs))
                    using (MultipleStreamWriter msw = new MultipleStreamWriter(assuredStream, mutlArgs))
                    {
                        using (Stream s = msw.CreateStream())
                        {
                            s.WriteByte(255);
                        }

                        using (Stream s = msw.CreateStream())
                            using (ZipFile zf = ZipFile.Create(s))
                            {
                                AddSomeFiles(zf);
                            }
                    }
        }
Exemplo n.º 2
0
        public void TestMultiStreamWriter()
        {
            string fileName = Path.GetTempFileName() + "q";

            using (FileStream fs = File.Create(fileName))
                using (MultipleStreamWriter writer = new MultipleStreamWriter(fs))
                {
                    for (int i = 0; i < 10; i++)
                    {
                        using (Stream s = writer.CreateStream())
                        {
                            using (StreamWriter sw = new StreamWriter(s))
                            {
                                sw.WriteLine("This is stream {0}", i);
                            }
                        }
                    }

                    using (MultipleStreamWriter subWriter = new MultipleStreamWriter(writer.CreateStream()))
                    {
                        for (int i = 0; i < 10; i++)
                        {
                            using (Stream s = subWriter.CreateStream())
                            {
                                using (StreamWriter sw = new StreamWriter(s))
                                {
                                    sw.WriteLine("This is stream x-{0}", fileName);
                                }
                            }
                        }
                    }
                }

            using (FileStream fs = File.OpenRead(fileName))
                using (MultipleStreamReader reader = new MultipleStreamReader(fs))
                {
                    for (int i = 0; i < 10; i++)
                    {
                        using (Stream s = reader.GetNextStream())
                        {
                            using (StreamReader sw = new StreamReader(s))
                            {
                                string line     = sw.ReadLine();
                                string shouldBe = string.Format("This is stream {0}", i);

                                Assert.That(line, Is.EqualTo(shouldBe));
                            }
                        }
                    }
                }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Creates the specified package
        /// </summary>
        /// <param name="fileName">Name of the file.</param>
        /// <param name="definition">The definition.</param>
        /// <returns></returns>
        public static TPack Create(string fileName, Pack definition)
        {
            if (fileName == null)
            {
                throw new ArgumentNullException("fileName");
            }
            else if (definition == null)
            {
                throw new ArgumentNullException("definition");
            }

            AssuredStreamCreateArgs args = new AssuredStreamCreateArgs();

            if (definition.StrongNameKey != null)
            {
                args.StrongNameKey = definition.StrongNameKey;
            }

            SortedFileList added = new SortedFileList();

            added.BaseDirectory = "c:\\" + Guid.NewGuid();
            foreach (PackContainer container in definition.Containers)
            {
                foreach (PackFile file in container.Files)
                {
                    if (!QQnPath.IsRelativeSubPath(file.StreamName) || added.Contains(file.StreamName))
                    {
                        string name = Path.GetFileNameWithoutExtension(file.StreamName);
                        string ext  = Path.GetExtension(file.StreamName);

                        string attempt = "_/" + name + ext;
                        int    n       = 0;
                        do
                        {
                            if (!added.Contains(attempt))
                            {
                                file.StreamName = attempt;
                                break;
                            }

                            attempt = string.Format("_/{0}.{1}.{2}", name, n++, ext);
                        }while (true);
                    }

                    if (file.StreamName.Contains("\\"))
                    {
                        file.StreamName = file.StreamName.Replace('\\', '/');
                    }
                }
            }

            args.FileType = PackageFileType;

            MultipleStreamCreateArgs msca = new MultipleStreamCreateArgs();

            msca.MaximumNumberOfStreams = 4;
            msca.VerificationMode       = VerificationMode.None;

            using (FileStream fs = File.Create(fileName, 32768))
                using (AssuredStream assurance = new AssuredStream(fs, args))
                    using (MultipleStreamWriter msw = new MultipleStreamWriter(assurance, msca))
                    {
                        MultipleStreamArgs msa = new MultipleStreamArgs();
                        msa.StreamType = 0x10;
                        msa.Assured    = true;
                        msa.GZipped    = true;
                        using (XmlWriter xw = new XmlTextWriter(msw.CreateStream(msa), Encoding.UTF8))
                        {
                            xw.WriteStartDocument();
                            xw.WriteStartElement("TurtlePackage", "http://schemas.qqn.nl/2007/TurtlePackage");
                            Tokenizer.TryWriteXml(xw, definition);
                            xw.WriteEndDocument();
                        }

                        msa            = new MultipleStreamArgs();
                        msa.StreamType = 0x11;

                        using (XmlWriter xw = new XmlTextWriter(msw.CreateStream(msa), Encoding.UTF8))
                        {
                            // TODO: Write tblog file
                        }


                        // Last stream: We add a zip file
                        msa            = new MultipleStreamArgs();
                        msa.StreamType = ZipFileId;  // Defined
                        msa.Assured    = false;      // Use the whole file assurance for the zip
                        msa.GZipped    = false;      // Don't compress again

                        using (Stream ms = msw.CreateStream(msa))
                            using (ZipFile zipFile = ZipFile.Create(ms))
                            {
                                zipFile.BeginUpdate();
                                zipFile.UseZip64 = UseZip64.Dynamic;

                                SetName setName = new SetName();

                                zipFile.NameTransform = setName;

                                foreach (PackContainer container in definition.Containers)
                                {
                                    foreach (PackFile file in container.Files)
                                    {
                                        setName.NextName = file.StreamName;

                                        Debug.Assert(File.Exists(file.FullName));

                                        zipFile.Add(file.FullName);
                                    }
                                }

                                zipFile.CommitUpdate();
                            }
                    }

            return(TPack.OpenFrom(fileName, VerificationMode.None));
        }