Пример #1
0
        public void TestCanDeserializeSerializedHeader()
        {
            MultiStreamHeaderItem[] items = new[]
            {
                new MultiStreamHeaderItem
                {
                    Length = ushort.MaxValue
                },
                new MultiStreamHeaderItem
                {
                    Length = ushort.MaxValue
                },
                new MultiStreamHeaderItem
                {
                    Length = ushort.MaxValue
                },
                new MultiStreamHeaderItem
                {
                    Length = ushort.MaxValue / 2
                }
            };
            GZipMultiStreamHeader header = new GZipMultiStreamHeader();

            foreach (MultiStreamHeaderItem item in items)
            {
                header.Items.Add(item);
            }

            byte[] buffer = header.Serialize();

            GZipMultiStreamHeader newHeader = new GZipMultiStreamHeader();

            newHeader.Deserialize(buffer);

            Assert.AreEqual(items.Length, newHeader.Items.Count, "Counts of items are not equal");

            for (int i = 0; i < items.Length; i++)
            {
                Assert.AreEqual(
                    items[i],
                    newHeader.Items[i],
                    string.Format("Items # {0} are not equal", i + 1)
                    );
            }
        }
Пример #2
0
        public void Write(string outputFilePath, OutputQueue outputQueue, CancellationToken cancellationToken)
        {
            if (string.IsNullOrEmpty(outputFilePath))
            {
                throw new ArgumentException("Output file path must be non-empty");
            }
            if (outputQueue == null)
            {
                throw new ArgumentNullException("Output queue must be non-empty");
            }

            GZipMultiStreamHeader multiStreamHeader = new GZipMultiStreamHeader();

            for (int i = 0; i < outputQueue.Count; i++)
            {
                if (cancellationToken.IsCancellationRequested)
                {
                    break;
                }

                OutputWorkItem        workItem = outputQueue[i];
                MultiStreamHeaderItem multiStreamHeaderItem = new MultiStreamHeaderItem
                {
                    Length = workItem.OutputStream.Stream.Length
                };

                multiStreamHeader.Items.Add(multiStreamHeaderItem);
            }

            OutputWorkItem firstOutputWorkItem = outputQueue[0];
            IBlockReader   blockReader         = new BlockReader();

            firstOutputWorkItem.OutputStream.Stream.Position = 0;

            Block block = blockReader.Read(firstOutputWorkItem.OutputStream.Stream, BlockFlags.All);

            if (cancellationToken.IsCancellationRequested)
            {
                return;
            }

            block.ExtraField = multiStreamHeader.Serialize();
            multiStreamHeader.Items[0].Length = block.Length;
            block.ExtraField = multiStreamHeader.Serialize();
            block.Flags     |= GZipFlags.FEXTRA;

            IGZipBlockWriter blockWriter = new BlockWriter();

            using (FileStream outputFileStream = File.Create(outputFilePath))
            {
                blockWriter.Write(outputFileStream, block, BlockFlags.All);

                for (int i = 1; i < outputQueue.Count; i++)
                {
                    if (cancellationToken.IsCancellationRequested)
                    {
                        break;
                    }

                    OutputWorkItem workItem = outputQueue[i];

                    using (Stream compressedStream = workItem.OutputStream.Stream)
                    {
                        compressedStream.Position = 0;
                        compressedStream.CopyTo(outputFileStream);
                    }
                }
            }
        }