A byte[] array that also stores a current offset. Useful for reading/writing headers, ensuring the offset is updated correctly.
        /// <summary>
        /// Implementation of writing an archive directory to the stream.
        /// </summary>
        /// <param name="writer">The writer stream.</param>
        private void WriteArchiveDirectoryToStream(Stream writer)
        {
            // Write the directory entries.
            long startOfDirectory = writer.Position;
            foreach (ZipArchiveFile entry in _entries.Values)
            {
                entry.WriteArchiveDirectoryEntryToStream(writer);
            }
            long endOfDirectory = writer.Position;

            // Write the trailer
            ByteBuffer trailer = new ByteBuffer(22);
            trailer.WriteUInt32(ZipArchiveFile.SignatureArchiveDirectoryEnd);
            trailer.WriteUInt16(ZipArchiveFile.DiskNumberStart);
            trailer.WriteUInt16(ZipArchiveFile.DiskNumberStart);
            trailer.WriteUInt16((ushort)_entries.Count);
            trailer.WriteUInt16((ushort)_entries.Count);
            trailer.WriteUInt32((uint)(endOfDirectory - startOfDirectory));      // directory size
            trailer.WriteUInt32((uint)startOfDirectory);                         // directory start
            trailer.WriteUInt16((ushort)ZipArchiveFile.FileCommentLength);
            trailer.WriteContentsTo(writer);
        }
        /// <summary>
        /// Writes an archive directory entry to stream.
        /// </summary>
        /// <param name="writer">The writer stream.</param>
        internal void WriteArchiveDirectoryEntryToStream(Stream writer)
        {
            // File header (in central directory):
            //
            // central file header signature   4 bytes  (0x02014b50)
            // version made by                 2 bytes
            // version needed to extract       2 bytes
            // general purpose bit flag        2 bytes
            // compression method              2 bytes
            // last mod file time              2 bytes
            // last mod file date              2 bytes
            // crc-32                          4 bytes
            // compressed size                 4 bytes
            // uncompressed size               4 bytes
            // file name length                2 bytes
            // extra field length              2 bytes
            // file comment length             2 bytes
            // disk number start               2 bytes
            // internal file attributes        2 bytes
            // external file attributes        4 bytes
            // relative offset of local header 4 bytes
            //
            // file name (variable size)
            // extra field (variable size)
            // file comment (variable size)

            byte[] fileNameBytes = Encoding.UTF8.GetBytes(_name);

            ByteBuffer header = new ByteBuffer(46);
            header.WriteUInt32(SignatureArchiveDirectory);
            header.WriteUInt16(VersionMadeBy);
            header.WriteUInt16(VersionNeededToExtract);
            header.WriteUInt16(GeneralPurposeBitFlag);
            header.WriteUInt16((ushort)_compressionMethod);
            header.WriteUInt32(DateTimeToDosTime(_lastWriteTime));
            header.WriteUInt32(CheckSum);
            header.WriteUInt32((uint)_compressedLength);
            header.WriteUInt32((uint)Length);
            header.WriteUInt16((ushort)fileNameBytes.Length);
            header.WriteUInt16(ExtraFieldLength);
            header.WriteUInt16(FileCommentLength);
            header.WriteUInt16(DiskNumberStart);
            header.WriteUInt16(InternalFileAttributes);
            header.WriteUInt32(ExternalFileAttributes);
            header.WriteUInt32(_headerOffset);

            header.WriteContentsTo(writer);

            writer.Write(fileNameBytes, 0, fileNameBytes.Length);
        }
        /// <summary>
        /// Reads a single archiveFile from a Zip Archive. Should only be used
        /// by ZipArchive.
        /// </summary>
        /// <param name="archive">The Zip archive object.</param>
        /// <returns>A ZipArchiveFile representing the archiveFile read from the
        /// archive.</returns>
        internal static ZipArchiveFile Read(ZipArchive archive)
        {
            Stream reader = archive.FromStream;
            ByteBuffer header = new ByteBuffer(30);
            int count = header.ReadContentsFrom(reader);
            if (count == 0)
            {
                return null;
            }

            // Local file header:
            //
            // local file header signature     4 bytes  (0x04034b50)
            // version needed to extract       2 bytes
            // general purpose bit flag        2 bytes
            // compression method              2 bytes
            // last mod file time              2 bytes
            // last mod file date              2 bytes
            // crc-32                          4 bytes
            // compressed size                 4 bytes
            // uncompressed size               4 bytes
            // file name length                2 bytes
            // extra field length              2 bytes
            //
            // file name (variable size)
            // extra field (variable size)

            uint fileSignature = header.ReadUInt32();
            if (fileSignature != SignatureFileEntry)
            {
                if (fileSignature != SignatureArchiveDirectory)
                {
                    throw new InvalidOperationException("Bad ZipFile Header");
                }
                return null;
            }

            ushort versionNeededToExtract = header.ReadUInt16();
            if (versionNeededToExtract > MaximumVersionExtractable)
            {
                throw new NotSupportedException("Zip file requires unsupported features");
            }

            header.SkipBytes(2); // general purpose bit flag

            ZipArchiveFile newEntry = new ZipArchiveFile(archive, null);

            newEntry._compressionMethod = (CompressionMethod)header.ReadUInt16();
            newEntry._lastWriteTime = DosTimeToDateTime(header.ReadUInt32());
            newEntry._crc32 = header.ReadUInt32();
            newEntry._compressedLength = checked((int)header.ReadUInt32());
            newEntry._length = header.ReadUInt32();
            int fileNameLength = checked((int)header.ReadUInt16());

            byte[] fileNameBuffer = new byte[fileNameLength];
            int fileNameCount = reader.Read(fileNameBuffer, 0, fileNameLength);
            newEntry._name = Encoding.UTF8.GetString(fileNameBuffer).Replace('/', Path.DirectorySeparatorChar);
            archive.Entries[newEntry._name] = newEntry;

            if (count != header.Length || fileNameCount != fileNameLength || fileNameLength == 0 || newEntry.LastWriteTime.Ticks == 0)
            {
                throw new InvalidOperationException("Bad Zip File Header");
            }

            if (newEntry.Name.IndexOfAny(invalidPathChars) >= 0)
            {
                throw new InvalidOperationException("Invalid File Name");
            }

            if (newEntry._compressionMethod != CompressionMethod.None && newEntry._compressionMethod != CompressionMethod.Deflate)
            {
                throw new NotSupportedException("Unsupported compression mode " + newEntry._compressionMethod);
            }

            if (archive.IsReadOnly && reader.CanSeek)
            {
                // Optimization: we can defer reading in the data in the common
                // case of a read-only archive by simply remembering where the
                // data is and fetching it on demand.  This is nice because we
                // only hold on to memory of data we are actually operating on,
                // instead of the whole archive.
                // 
                // Because uncompresseData is null, we know that we fetch it
                // from the archive 'fromStream'.
                newEntry._positionOfCompressedDataInArchive = archive.FromStream.Position;
                reader.Seek(newEntry._compressedLength, SeekOrigin.Current);
            }
            else
            {
                // We may be updating the archive in place, so we need to copy
                // the data out.
                newEntry._compressedData = new byte[newEntry._compressedLength];
                reader.Read(newEntry._compressedData, 0, (int)newEntry._compressedLength);
            }

#if DEBUG
            newEntry.Validate();
#endif

            return newEntry;
        }
        /// <summary>
        /// Writes the Zip file header to the stream.
        /// </summary>
        /// <param name="writer">Stream to write to.</param>
        private void WriteZipFileHeader(Stream writer)
        {
            byte[] fileNameBytes = Encoding.UTF8.GetBytes(_name.Replace(Path.DirectorySeparatorChar, '/'));
            if ((uint)_length != _length)
            {
                throw new InvalidOperationException("File length too long.");
            }

            // Local file header:
            //
            // local file header signature     4 bytes  (0x04034b50)
            // version needed to extract       2 bytes
            // general purpose bit flag        2 bytes
            // compression method              2 bytes
            // last mod file time              2 bytes
            // last mod file date              2 bytes
            // crc-32                          4 bytes
            // compressed size                 4 bytes
            // uncompressed size               4 bytes
            // file name length                2 bytes
            // extra field length              2 bytes
            //
            // file name (variable size)
            // extra field (variable size)

            // Save the start of the header file for later use in the dir entry
            _headerOffset = (uint)writer.Position;

            ByteBuffer header = new ByteBuffer(30);
            header.WriteUInt32(SignatureFileEntry);
            header.WriteUInt16(VersionNeededToExtract);
            header.WriteUInt16(GeneralPurposeBitFlag);
            header.WriteUInt16((ushort)_compressionMethod);
            header.WriteUInt32(DateTimeToDosTime(_lastWriteTime));
            header.WriteUInt32(CheckSum);
            header.WriteUInt32((uint)_compressedLength);
            header.WriteUInt32((uint)Length);
            header.WriteUInt16((ushort)fileNameBytes.Length);
            header.WriteUInt16(ExtraFieldLength);                                 // extra field length (unused)

            header.WriteContentsTo(writer);

            writer.Write(fileNameBytes, 0, fileNameBytes.Length);                // write the archiveFile name
        }