-
Notifications
You must be signed in to change notification settings - Fork 5k
Fix ZipArchiveEntry names shown as corrupted on other zip programs #65886
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,7 +42,6 @@ public partial class ZipArchiveEntry | |
private List<ZipGenericExtraField>? _lhUnknownExtraFields; | ||
private byte[] _fileComment; | ||
private readonly CompressionLevel? _compressionLevel; | ||
private bool _hasUnicodeEntryNameOrComment; | ||
|
||
// Initializes a ZipArchiveEntry instance for an existing archive entry. | ||
internal ZipArchiveEntry(ZipArchive archive, ZipCentralDirectoryFileHeader cd) | ||
|
@@ -84,8 +83,6 @@ internal ZipArchiveEntry(ZipArchive archive, ZipCentralDirectoryFileHeader cd) | |
_fileComment = cd.FileComment; | ||
|
||
_compressionLevel = null; | ||
|
||
_hasUnicodeEntryNameOrComment = (_generalPurposeBitFlag & BitFlagValues.UnicodeFileNameAndComment) != 0; | ||
} | ||
|
||
// Initializes a ZipArchiveEntry instance for a new archive entry with a specified compression level. | ||
|
@@ -144,8 +141,6 @@ internal ZipArchiveEntry(ZipArchive archive, string entryName) | |
{ | ||
_archive.AcquireArchiveStream(this); | ||
} | ||
|
||
_hasUnicodeEntryNameOrComment = false; | ||
} | ||
|
||
/// <summary> | ||
|
@@ -197,7 +192,11 @@ public string Comment | |
set | ||
{ | ||
_fileComment = ZipHelper.GetEncodedTruncatedBytesFromString(value, _archive.EntryNameAndCommentEncoding, ushort.MaxValue, out bool isUTF8); | ||
_hasUnicodeEntryNameOrComment |= isUTF8; | ||
|
||
if (isUTF8) | ||
{ | ||
_generalPurposeBitFlag |= BitFlagValues.UnicodeFileNameAndComment; | ||
} | ||
} | ||
} | ||
|
||
|
@@ -218,11 +217,19 @@ private set | |
ArgumentNullException.ThrowIfNull(value, nameof(FullName)); | ||
|
||
_storedEntryNameBytes = ZipHelper.GetEncodedTruncatedBytesFromString( | ||
value, _archive.EntryNameAndCommentEncoding, 0 /* No truncation */, out bool hasUnicodeEntryName); | ||
value, _archive.EntryNameAndCommentEncoding, 0 /* No truncation */, out bool isUTF8); | ||
|
||
_hasUnicodeEntryNameOrComment |= hasUnicodeEntryName; | ||
_storedEntryName = value; | ||
|
||
if (isUTF8) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not sure if I understand. When Utf8 is detected, we enable the Unicode flag? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes. Maybe the name of the flag should be Per the spec: https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
That would definitely help ;) thanks for the pointer to the docs! |
||
{ | ||
_generalPurposeBitFlag |= BitFlagValues.UnicodeFileNameAndComment; | ||
} | ||
else | ||
{ | ||
_generalPurposeBitFlag &= ~BitFlagValues.UnicodeFileNameAndComment; | ||
} | ||
Comment on lines
+225
to
+231
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The problem is that if the comment has utf8 characters, but the filename does not, now you're removing the utf8 flag. My intention was to detect utf8 characters in either filename or comment. If one of the two had them, then the general purpose bit flag bit 11 would be turned on. Hence the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should probably look like the code you added in the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Keep in mind that the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm now that I think of it more, maybe it makes sense to set it to 0 or 1 here, since as I said, this is called in the constructor only, which means Comment has not modified this bit yet. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. adding/removing the flag was being performed before your changes so this is really going back to update _generalPurposeBitFlag in the ctor before it spreads. |
||
|
||
DetectEntryNameVersion(); | ||
} | ||
} | ||
|
@@ -505,11 +512,6 @@ internal void WriteCentralDirectoryFileHeader() | |
extraFieldLength = (ushort)bigExtraFieldLength; | ||
} | ||
|
||
if (_hasUnicodeEntryNameOrComment) | ||
_generalPurposeBitFlag |= BitFlagValues.UnicodeFileNameAndComment; | ||
else | ||
_generalPurposeBitFlag &= ~BitFlagValues.UnicodeFileNameAndComment; | ||
|
||
Comment on lines
-508
to
-512
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm ok with removing this if we are directly modifying the general purpose bit flag when detecting the comment or filename encoding. |
||
writer.Write(ZipCentralDirectoryFileHeader.SignatureConstant); // Central directory file header signature (4 bytes) | ||
writer.Write((byte)_versionMadeBySpecification); // Version made by Specification (version) (1 byte) | ||
writer.Write((byte)CurrentZipPlatform); // Version made by Compatibility (type) (1 byte) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is fine, since you're avoiding removing the bit flag unexpectedly if filename already set it to true.