Repository: dotnet/runtime
File: src/libraries/System.IO.Compression/src/System/IO/Compression/ZipArchiveEntry.cs (C#)

Code before:
                ZipGenericExtraField.WriteAllBlocks(_lhUnknownExtraFields, _lhTrailingExtraFieldData ?? Array.Empty<byte>(), _archive.ArchiveStream);
            }

            return zip64ExtraField != null;
        }

        private void WriteLocalFileHeaderAndDataIfNeeded(bool forceWrite)
        {
            // Check if the entry's stored data was actually modified (StoredData flag is set).
            // If _storedUncompressedData is loaded but StoredData is not set, it means the entry
            // was opened for update but no writes occurred - we should use the original compressed bytes.
            bool storedDataModified = (Changes & ZipArchive.ChangeState.StoredData) != 0;

            // If _storedUncompressedData is loaded but not modified, clear it so we use _compressedBytes
            if (_storedUncompressedData != null && !storedDataModified)
            {
                _storedUncompressedData.Dispose();
                _storedUncompressedData = null;
            }

            // _storedUncompressedData gets frozen here, and is what gets written to the file
            if (_storedUncompressedData != null || _compressedBytes != null)
            {
                if (_storedUncompressedData != null)
                {
                    _uncompressedSize = _storedUncompressedData.Length;

                    //The compressor fills in CRC and sizes
                    //The DirectToArchiveWriterStream writes headers and such
                    using (DirectToArchiveWriterStream entryWriter = new(
                                                    GetDataCompressor(_archive.ArchiveStream, true, null),
                                                    this))
                    {
                        _storedUncompressedData.Seek(0, SeekOrigin.Begin);
                        _storedUncompressedData.CopyTo(entryWriter);
                        _storedUncompressedData.Dispose();
                        _storedUncompressedData = null;
                    }
                }
                else
                {
                    if (_uncompressedSize == 0)
                    {
                        // reset size to ensure proper central directory size header
                        _compressedSize = 0;
                    }

                    WriteLocalFileHeader(isEmptyFile: _uncompressedSize == 0, forceWrite: true);

                    // according to ZIP specs, zero-byte files MUST NOT include file data
                    if (_uncompressedSize != 0)

Review comment (suggestion):
Clearing `_storedUncompressedData` and falling back to `_compressedBytes` when `StoredData` isn't set can corrupt archives for entries whose metadata was mutated during `GetUncompressedData*()`. In particular, `GetUncompressedData*()` changes `CompressionMethod` to Deflate for non-Stored entries; if an entry was opened in Update mode (no writes) and later the archive is rewritten due to other changes, the header may be written with `CompressionMethod=Deflate` while the original `_compressedBytes` are still Deflate64, producing an invalid entry. To avoid mismatches, ensure metadata like `CompressionMethod` is not changed unless the entry is actually modified (e.g., defer normalization to `MarkAsModified`/first write), or restore the original method when discarding `_storedUncompressedData`.
```suggestion

```

Code after:
                _archive.ArchiveStream.Write(lfStaticHeader);
                _archive.ArchiveStream.Write(_storedEntryNameBytes);

                // Only when handling zip64
                zip64ExtraField?.WriteBlock(_archive.ArchiveStream);

                ZipGenericExtraField.WriteAllBlocks(_lhUnknownExtraFields, _lhTrailingExtraFieldData ?? Array.Empty<byte>(), _archive.ArchiveStream);
            }

            return zip64ExtraField != null;
        }

        private void WriteLocalFileHeaderAndDataIfNeeded(bool forceWrite)
        {
            // Check if the entry's stored data was actually modified (StoredData flag is set).
            // If _storedUncompressedData is loaded but StoredData is not set, it means the entry
            // was opened for update but no writes occurred - we should use the original compressed bytes.
            bool storedDataModified = (Changes & ZipArchive.ChangeState.StoredData) != 0;

            // If _storedUncompressedData is loaded but not modified, clear it so we use _compressedBytes
            if (_storedUncompressedData != null && !storedDataModified)
            {
                _storedUncompressedData.Dispose();
                _storedUncompressedData = null;
            }

            // _storedUncompressedData gets frozen here, and is what gets written to the file
            if (_storedUncompressedData != null || _compressedBytes != null)
            {
                if (_storedUncompressedData != null)
                {
                    _uncompressedSize = _storedUncompressedData.Length;

                    //The compressor fills in CRC and sizes
                    //The DirectToArchiveWriterStream writes headers and such
                    using (DirectToArchiveWriterStream entryWriter = new(
                                                    GetDataCompressor(_archive.ArchiveStream, true, null),
                                                    this))
                    {
                        _storedUncompressedData.Seek(0, SeekOrigin.Begin);
                        _storedUncompressedData.CopyTo(entryWriter);
                        _storedUncompressedData.Dispose();
                        _storedUncompressedData = null;
                    }
                }
                else
                {
                    if (_uncompressedSize == 0)
                    {
                        // reset size to ensure proper central directory size header
                        _compressedSize = 0;