Ejemplo n.º 1
0
        public void WriteV10(FileStream mainStream)
        {
            using (var writer = new BinaryWriter(mainStream, new UTF8Encoding(), true))
            {
                var header = new LSPKHeader10();
                header.Version = Version;
                header.NumFiles = (UInt32)package.Files.Count;
                header.FileListSize = (UInt32)(Marshal.SizeOf(typeof(FileEntry13)) * package.Files.Count);
                header.DataOffset = (UInt32)Marshal.SizeOf(typeof(LSPKHeader10)) + 4 + header.FileListSize;
                var paddingLength = PaddingLength();
                if (header.DataOffset % paddingLength > 0)
                {
                    header.DataOffset += (UInt32)(paddingLength - (header.DataOffset % paddingLength));
                }

                // Write a placeholder instead of the actual headers; we'll write them after we 
                // compressed and flushed all files to disk
                var placeholder = new byte[header.DataOffset];
                writer.Write(placeholder);

                long totalSize = package.Files.Sum(p => (long)p.Size());
                long currentSize = 0;
                List<PackagedFileInfo> writtenFiles = new List<PackagedFileInfo>();
                foreach (var file in this.package.Files)
                {
                    writeProgress(file, currentSize, totalSize);
                    writtenFiles.Add(WriteFile(file));
                    currentSize += file.Size();
                }

                mainStream.Seek(0, SeekOrigin.Begin);
                writer.Write(Package.Signature); 
                header.NumParts = (UInt16)streams.Count;
                header.SomePartVar = 0; // ???
                BinUtils.WriteStruct<LSPKHeader10>(writer, ref header);

                foreach (var file in writtenFiles)
                {
                    var entry = file.MakeEntryV13();
                    if (entry.ArchivePart == 0)
                    {
                        entry.OffsetInFile -= header.DataOffset;
                    }

                    // v10 packages don't support compression level in the flags field
                    entry.Flags &= 0x0f;
                    BinUtils.WriteStruct<FileEntry13>(writer, ref entry);
                }
            }
        }