Example #1
0
        private void AddEntry(ReadOnlyZipArchiveEntry entry)
        {
            _entries.Add(entry);

            string entryName = entry.FullName;

            if (!_entriesDictionary.ContainsKey(entryName))
            {
                _entriesDictionary.Add(entryName, entry);
            }
        }
Example #2
0
        private void Init(Stream stream, ZipArchiveMode mode, bool leaveOpen)
        {
            if (mode == ZipArchiveMode.Update || mode == ZipArchiveMode.Create)
            {
                throw new InvalidOperationException();
            }
            Stream extraTempStream = null;

            try
            {
                _backingStream = null;

                // check stream against mode
                switch (mode)
                {
                case ZipArchiveMode.Read:
                    if (!stream.CanRead)
                    {
                        throw new ArgumentException("ReadModeCapabilities");
                    }
                    if (!stream.CanSeek)
                    {
                        _backingStream  = stream;
                        extraTempStream = stream = new MemoryStream();
                        _backingStream.CopyTo(stream);
                        stream.Seek(0, SeekOrigin.Begin);
                    }
                    break;

                default:
                    // still have to throw this, because stream constructor doesn't do mode argument checks
                    throw new ArgumentOutOfRangeException(nameof(mode));
                }

                _mode = mode;

                _archiveStream      = stream;
                _archiveStreamOwner = null;

                _archiveReader         = new BinaryReader(_archiveStream);
                _entries               = new List <ReadOnlyZipArchiveEntry>();
                _entriesCollection     = new ReadOnlyCollection <ReadOnlyZipArchiveEntry>(_entries);
                _entriesDictionary     = new Dictionary <string, ReadOnlyZipArchiveEntry>();
                _readEntries           = false;
                _leaveOpen             = leaveOpen;
                _centralDirectoryStart = 0; // invalid until ReadCentralDirectory
                _isDisposed            = false;
                _numberOfThisDisk      = 0; // invalid until ReadCentralDirectory
                _archiveComment        = null;

                switch (mode)
                {
                case ZipArchiveMode.Read:
                    ReadEndOfCentralDirectory();
                    break;

                default:

                    break;
                }
            }
            catch
            {
                if (extraTempStream != null)
                {
                    extraTempStream.Dispose();
                }

                throw;
            }
        }