public void Write(ArchiveListing listing, out ArchiveListingBlockInfo[] blocksInfo, out ArchiveListingEntryInfoV2[] entriesInfoV2)
        {
            using (MemoryStream ms = new MemoryStream(32768))
            {
                int blockNumber = 0, unpackedBlockOffset = 0;
                entriesInfoV2 = new ArchiveListingEntryInfoV2[listing.Count];
                List <ArchiveListingBlockInfo> blocks = new List <ArchiveListingBlockInfo>(128);
                using (FormattingStreamWriter sw = new FormattingStreamWriter(ms, Encoding.ASCII, 4096, true, CultureInfo.InvariantCulture))
                {
                    sw.AutoFlush = true;
                    for (int i = 0; i < listing.Count; i++)
                    {
                        ArchiveEntry entry = listing[i];

                        ArchiveListingEntryInfoV2 info = new ArchiveListingEntryInfoV2 {
                            BlockNumber = (short)(ms.Position / 8192)
                        };
                        info.Flag = (info.BlockNumber % 2 != 0);

                        entriesInfoV2[i] = info;

                        if (blockNumber != info.BlockNumber)
                        {
                            int blockSize = (int)(ms.Position - unpackedBlockOffset);
                            ms.Position = unpackedBlockOffset;
                            ArchiveListingBlockInfo block = new ArchiveListingBlockInfo {
                                Offset = (int)_output.Position, UncompressedSize = blockSize
                            };
                            block.CompressedSize = ZLibHelper.Compress(ms, _output, block.UncompressedSize);
                            blocks.Add(block);

                            blockNumber++;
                            unpackedBlockOffset = (int)ms.Position;
                            sw.Write("{0:x}:{1:x}:{2:x}:{3}\0", entry.Sector, entry.UncompressedSize, entry.Size, entry.Name);
                        }
                        else if (i == listing.Count - 1)
                        {
                            info.Offset = (short)(ms.Position - unpackedBlockOffset);
                            sw.Write("{0:x}:{1:x}:{2:x}:{3}\0end\0", entry.Sector, entry.UncompressedSize, entry.Size, entry.Name);
                            int blockSize = (int)(ms.Position - unpackedBlockOffset);
                            ms.Position = unpackedBlockOffset;
                            ArchiveListingBlockInfo block = new ArchiveListingBlockInfo {
                                Offset = (int)_output.Position, UncompressedSize = blockSize
                            };
                            block.CompressedSize = ZLibHelper.Compress(ms, _output, block.UncompressedSize);
                            blocks.Add(block);
                        }
                        else
                        {
                            info.Offset = (short)(ms.Position - unpackedBlockOffset);
                            sw.Write("{0:x}:{1:x}:{2:x}:{3}\0", entry.Sector, entry.UncompressedSize, entry.Size, entry.Name);
                        }
                    }
                }
                blocksInfo = blocks.ToArray();
            }
        }
        public void Write(ArchiveListing listing, out ArchiveListingBlockInfo[] blocksInfo, out ArchiveListingEntryInfoV1[] entriesInfoV1)
        {
            using (MemoryStream ms = new MemoryStream(32768))
            {
                int blockNumber = 0, unpackedBlockOffset = 0;
                entriesInfoV1 = new ArchiveListingEntryInfoV1[listing.Count];
                List<ArchiveListingBlockInfo> blocks = new List<ArchiveListingBlockInfo>(128);
                using (FormattingStreamWriter sw = new FormattingStreamWriter(ms, Encoding.ASCII, 4096, true, CultureInfo.InvariantCulture))
                {
                    sw.AutoFlush = true;
                    for (int i = 0; i < listing.Count; i++)
                    {
                        ArchiveEntry entry = listing[i];
                        entriesInfoV1[i] = new ArchiveListingEntryInfoV1 {BlockNumber = (short)(ms.Position / 8192)};

                        if (blockNumber != entriesInfoV1[i].BlockNumber)
                        {
                            //sw.Write("end\0");
                            int blockSize = (int)(ms.Position - unpackedBlockOffset);
                            ms.Position = unpackedBlockOffset;
                            ArchiveListingBlockInfo block = new ArchiveListingBlockInfo {Offset = (int)_output.Position, UncompressedSize = blockSize};
                            block.CompressedSize = ZLibHelper.Compress(ms, _output, block.UncompressedSize);
                            blocks.Add(block);

                            blockNumber++;
                            unpackedBlockOffset = (int)ms.Position;
                            sw.Write("{0:x}:{1:x}:{2:x}:{3}\0", entry.Sector, entry.UncompressedSize, entry.Size, entry.Name);
                        }
                        else if (i == listing.Count - 1)
                        {
                            entriesInfoV1[i].Offset = (short)(ms.Position - unpackedBlockOffset);
                            sw.Write("{0:x}:{1:x}:{2:x}:{3}\0end\0", entry.Sector, entry.UncompressedSize, entry.Size, entry.Name);
                            int blockSize = (int)(ms.Position - unpackedBlockOffset);
                            ms.Position = unpackedBlockOffset;
                            ArchiveListingBlockInfo block = new ArchiveListingBlockInfo {Offset = (int)_output.Position, UncompressedSize = blockSize};
                            block.CompressedSize = ZLibHelper.Compress(ms, _output, block.UncompressedSize);
                            blocks.Add(block);
                        }
                        else
                        {
                            entriesInfoV1[i].Offset = (short)(ms.Position - unpackedBlockOffset);
                            sw.Write("{0:x}:{1:x}:{2:x}:{3}\0", entry.Sector, entry.UncompressedSize, entry.Size, entry.Name);
                        }

                        //lengths[i] = (int)_output.Position - offsets[i];
                        //blockSize += lengths[i];
                    }
                }
                blocksInfo = blocks.ToArray();
            }
        }
Пример #3
0
        private void UncompressBlocks(ArchiveListingBlockInfo[] blocks, Stream stream)
        {
            try
            {
                for (int i = 0; i < blocks.Length; i++)
                {
                    ArchiveListingBlockInfo block = blocks[i];
                    int uncompressedSize          = block.UncompressedSize;

                    stream.Position        = _header.InfoOffset + block.Offset;
                    _uncompressedBlocks[i] = ZLibHelper.Uncompress(stream, uncompressedSize);
                }
            }
            catch (ObjectDisposedException)
            {
            }
            catch (Exception ex)
            {
                _exception = ex;
            }
        }
        private void UncompressBlocks(ArchiveListingBlockInfo[] blocks, Stream stream)
        {
            try
            {
                for (int i = 0; i < blocks.Length; i++)
                {
                    ArchiveListingBlockInfo block = blocks[i];
                    int uncompressedSize = block.UncompressedSize;

                    stream.Position = _header.InfoOffset + block.Offset;
                    _uncompressedBlocks[i] = ZLibHelper.Uncompress(stream, uncompressedSize);
                }
            }
            catch (ObjectDisposedException)
            {
            }
            catch (Exception ex)
            {
                _exception = ex;
            }
        }