Пример #1
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));
        }