예제 #1
0
        /// <summary>
        /// Saves this instance, aka overwrites the file header.
        /// </summary>
        public void Save()
        {
            var head = new FileHeader
            {
                Signature  = PackCommon.Header,
                Revision   = (uint)Revision,
                EntryCount = Count,
                Path       = Root,
                Ft1        = Created.ToFileTime(),
                Ft2        = Modified.ToFileTime()
            };

            var pkgHead = new PackageHeader
            {
                EntryCount      = Count,
                InfoHeaderSize  = 0,
                DataSectionSize = (uint)(_source.Length - _dataStart),
                BlankSize       = 0,
                Zero            = new byte[16]
            };

            var infos = new List <Tuple <byte[], PackageItemInfo> >();

            foreach (var e in _entries.Values)
            {
                var info = new PackageItemInfo
                {
                    CompressedSize   = e.SizeInPack,
                    DecompressedSize = e.DecompressedSize,
                    IsCompressed     = e.IsCompressed,
                    Offset           = (uint)e.DataOffset,
                    Seed             = e.CryptoSeed,
                    CreationTime     = e.CreatedDate.ToFileTime(),
                    CreationTime2    = e.CreatedDate.ToFileTime(),
                    LastAccessTime   = e.AccessDate.ToFileTime(),
                    ModifiedTime     = e.ModifiedDate.ToFileTime(),
                    ModifiedTime2    = e.ModifiedDate.ToFileTime(),
                };

                var bytes = PackCommon.EncodeName(e.Name.PadRight(e.MaxNameLength, '\0'));

                infos.Add(Tuple.Create(bytes, info));
                pkgHead.InfoHeaderSize += bytes.Length + StructUtil.SizeOf <PackageItemInfo>();
            }

            pkgHead.BlankSize = (int)(_dataStart -
                                      (StructUtil.SizeOf <FileHeader>() + StructUtil.SizeOf <PackageHeader>() +
                                       pkgHead.InfoHeaderSize));

            pkgHead.InfoHeaderSize += pkgHead.BlankSize;

            _source.Position = 0;
            head.WriteToStream(_source);
            pkgHead.WriteToStream(_source);
            foreach (var i in infos)
            {
                _source.Write(i.Item1, 0, i.Item1.Length);
                i.Item2.WriteToStream(_source);
            }
        }
예제 #2
0
        /// <summary>
        /// Outputs the pack file to the given stream.
        /// </summary>
        /// <param name="dst">The DST.</param>
        public void SaveTo(Stream dst)
        {
            var head = new FileHeader
            {
                Signature  = PackCommon.Header,
                Revision   = Revision,
                EntryCount = _entries.Count,
                Path       = Root,
                Ft1        = DateTime.UtcNow.ToFileTimeUtc(),
                Ft2        = DateTime.UtcNow.ToFileTimeUtc()
            };

            var pkgHead = new PackageHeader
            {
                EntryCount      = _entries.Count,
                InfoHeaderSize  = 0,
                DataSectionSize = (uint)_bodyStream.Position,
                BlankSize       = 0,
                Zero            = new byte[16]
            };

            var infos = new List <Tuple <byte[], PackageItemInfo> >();

            foreach (var e in _entries)
            {
                var info = new PackageItemInfo()
                {
                    CompressedSize   = e.SizeInPack,
                    DecompressedSize = e.DecompressedSize,
                    IsCompressed     = e.IsCompressed,
                    Offset           = (uint)e.DataOffset,
                    Seed             = e.CryptoSeed,
                    CreationTime     = e.CreatedDate.ToFileTime(),
                    CreationTime2    = e.CreatedDate.ToFileTime(),
                    ModifiedTime     = e.ModifiedDate.ToFileTime(),
                    ModifiedTime2    = e.ModifiedDate.ToFileTime(),
                    LastAccessTime   = e.AccessDate.ToFileTime()
                };

                var bytes = PackCommon.EncodeName(e.Name);

                infos.Add(Tuple.Create(bytes, info));
                pkgHead.InfoHeaderSize += bytes.Length + StructUtil.SizeOf <PackageItemInfo>();
            }

            head.WriteToStream(dst);
            pkgHead.WriteToStream(dst);
            foreach (var i in infos)
            {
                dst.Write(i.Item1, 0, i.Item1.Length);
                i.Item2.WriteToStream(dst);
            }

            _bodyStream.Position = 0;
            _bodyStream.CopyTo(dst);
        }