@@ -367,7 +367,7 @@ public string Password
367367 }
368368 else
369369 {
370- key = PkzipClassic . GenerateKeys ( ZipStrings . ConvertToArray ( value ) ) ;
370+ key = PkzipClassic . GenerateKeys ( ZipCryptoEncoding . GetBytes ( value ) ) ;
371371 }
372372
373373 rawPassword_ = value ;
@@ -725,6 +725,14 @@ public ZipEntry this[int index]
725725 }
726726 }
727727
728+
729+ /// <inheritdoc cref="StringCodec.ZipCryptoEncoding"/>
730+ public Encoding ZipCryptoEncoding
731+ {
732+ get => _stringCodec . ZipCryptoEncoding ;
733+ set => _stringCodec . ZipCryptoEncoding = value ;
734+ }
735+
728736 #endregion Properties
729737
730738 #region Input Handling
@@ -1191,6 +1199,8 @@ private long TestLocalHeader(ZipEntry entry, HeaderTest tests)
11911199 throw new ZipException ( string . Format ( "Version required to extract this entry is invalid ({0})" , extractVersion ) ) ;
11921200 }
11931201
1202+ var localEncoding = _stringCodec . ZipInputEncoding ( localFlags ) ;
1203+
11941204 // Local entry flags dont have reserved bit set on.
11951205 if ( ( localFlags & ( int ) ( GeneralBitFlags . ReservedPKware4 | GeneralBitFlags . ReservedPkware14 | GeneralBitFlags . ReservedPkware15 ) ) != 0 )
11961206 {
@@ -1283,7 +1293,7 @@ private long TestLocalHeader(ZipEntry entry, HeaderTest tests)
12831293 }
12841294
12851295 // Name data has already been read convert it and compare.
1286- string localName = ZipStrings . ConvertToStringExt ( localFlags , nameData ) ;
1296+ string localName = localEncoding . GetString ( nameData ) ;
12871297
12881298 // Central directory and local entry name match
12891299 if ( localName != entry . Name )
@@ -1581,7 +1591,7 @@ public void CommitUpdate()
15811591 // Create an empty archive if none existed originally.
15821592 if ( entries_ . Length == 0 )
15831593 {
1584- byte [ ] theComment = ( newComment_ != null ) ? newComment_ . RawComment : ZipStrings . ConvertToArray ( comment_ ) ;
1594+ byte [ ] theComment = ( newComment_ != null ) ? newComment_ . RawComment : _stringCodec . ZipArchiveCommentEncoding . GetBytes ( comment_ ) ;
15851595 using ( ZipHelperStream zhs = new ZipHelperStream ( baseStream_ ) )
15861596 {
15871597 zhs . WriteEndOfCentralDirectory ( 0 , 0 , 0 , theComment ) ;
@@ -1619,7 +1629,7 @@ public void SetComment(string comment)
16191629
16201630 CheckUpdating ( ) ;
16211631
1622- newComment_ = new ZipString ( comment ) ;
1632+ newComment_ = new ZipString ( comment , _stringCodec . ZipArchiveCommentEncoding ) ;
16231633
16241634 if ( newComment_ . RawLength > 0xffff )
16251635 {
@@ -2147,7 +2157,8 @@ private void WriteLocalEntryHeader(ZipUpdate update)
21472157 WriteLEInt ( ( int ) entry . Size ) ;
21482158 }
21492159
2150- byte [ ] name = ZipStrings . ConvertToArray ( entry . Flags , entry . Name ) ;
2160+ var entryEncoding = _stringCodec . ZipInputEncoding ( entry . Flags ) ;
2161+ byte [ ] name = entryEncoding . GetBytes ( entry . Name ) ;
21512162
21522163 if ( name . Length > 0xFFFF )
21532164 {
@@ -2254,7 +2265,8 @@ private int WriteCentralDirectoryHeader(ZipEntry entry)
22542265 WriteLEInt ( ( int ) entry . Size ) ;
22552266 }
22562267
2257- byte [ ] name = ZipStrings . ConvertToArray ( entry . Flags , entry . Name ) ;
2268+ var entryEncoding = _stringCodec . ZipInputEncoding ( entry . Flags ) ;
2269+ byte [ ] name = entryEncoding . GetBytes ( entry . Name ) ;
22582270
22592271 if ( name . Length > 0xFFFF )
22602272 {
@@ -3081,7 +3093,7 @@ private void RunUpdates()
30813093 }
30823094 }
30833095
3084- byte [ ] theComment = ( newComment_ != null ) ? newComment_ . RawComment : ZipStrings . ConvertToArray ( comment_ ) ;
3096+ byte [ ] theComment = newComment_ ? . RawComment ?? _stringCodec . ZipArchiveCommentEncoding . GetBytes ( comment_ ) ;
30853097 using ( ZipHelperStream zhs = new ZipHelperStream ( workFile . baseStream_ ) )
30863098 {
30873099 zhs . WriteEndOfCentralDirectory ( updateCount_ , sizeEntries , centralDirOffset , theComment ) ;
@@ -3481,7 +3493,7 @@ private void ReadEntries()
34813493 byte [ ] comment = new byte [ commentSize ] ;
34823494
34833495 StreamUtils . ReadFully ( baseStream_ , comment ) ;
3484- comment_ = ZipStrings . ConvertToString ( comment ) ;
3496+ comment_ = _stringCodec . ZipArchiveCommentEncoding . GetString ( comment ) ;
34853497 }
34863498 else
34873499 {
@@ -3598,11 +3610,13 @@ private void ReadEntries()
35983610 long offset = ReadLEUint ( ) ;
35993611
36003612 byte [ ] buffer = new byte [ Math . Max ( nameLen , commentLen ) ] ;
3613+ var entryEncoding = _stringCodec . ZipInputEncoding ( bitFlags ) ;
36013614
36023615 StreamUtils . ReadFully ( baseStream_ , buffer , 0 , nameLen ) ;
3603- string name = ZipStrings . ConvertToStringExt ( bitFlags , buffer , nameLen ) ;
3616+ string name = entryEncoding . GetString ( buffer , 0 , nameLen ) ;
3617+ var unicode = entryEncoding . IsZipUnicode ( ) ;
36043618
3605- var entry = new ZipEntry ( name , versionToExtract , versionMadeBy , ( CompressionMethod ) method )
3619+ var entry = new ZipEntry ( name , versionToExtract , versionMadeBy , ( CompressionMethod ) method , unicode )
36063620 {
36073621 Crc = crc & 0xffffffffL ,
36083622 Size = size & 0xffffffffL ,
@@ -3635,7 +3649,7 @@ private void ReadEntries()
36353649 if ( commentLen > 0 )
36363650 {
36373651 StreamUtils . ReadFully ( baseStream_ , buffer , 0 , commentLen ) ;
3638- entry . Comment = ZipStrings . ConvertToStringExt ( bitFlags , buffer , commentLen ) ;
3652+ entry . Comment = entryEncoding . GetString ( buffer , 0 , commentLen ) ;
36393653 }
36403654
36413655 entries_ [ i ] = entry ;
@@ -3787,6 +3801,7 @@ private static void WriteEncryptionHeader(Stream stream, long crcValue)
37873801 private ZipEntry [ ] entries_ ;
37883802 private byte [ ] key ;
37893803 private bool isNewArchive_ ;
3804+ private readonly StringCodec _stringCodec = new StringCodec ( ) ;
37903805
37913806 // Default is dynamic which is not backwards compatible and can cause problems
37923807 // with XP's built in compression which cant read Zip64 archives.
@@ -3825,19 +3840,23 @@ private class ZipString
38253840 /// Initialise a <see cref="ZipString"/> with a string.
38263841 /// </summary>
38273842 /// <param name="comment">The textual string form.</param>
3828- public ZipString ( string comment )
3843+ /// <param name="encoding"></param>
3844+ public ZipString ( string comment , Encoding encoding )
38293845 {
38303846 comment_ = comment ;
38313847 isSourceString_ = true ;
3848+ _encoding = encoding ;
38323849 }
38333850
38343851 /// <summary>
38353852 /// Initialise a <see cref="ZipString"/> using a string in its binary 'raw' form.
38363853 /// </summary>
38373854 /// <param name="rawString"></param>
3838- public ZipString ( byte [ ] rawString )
3855+ /// <param name="encoding"></param>
3856+ public ZipString ( byte [ ] rawString , Encoding encoding )
38393857 {
38403858 rawComment_ = rawString ;
3859+ _encoding = encoding ;
38413860 }
38423861
38433862 #endregion Constructors
@@ -3846,10 +3865,7 @@ public ZipString(byte[] rawString)
38463865 /// Get a value indicating the original source of data for this instance.
38473866 /// True if the source was a string; false if the source was binary data.
38483867 /// </summary>
3849- public bool IsSourceString
3850- {
3851- get { return isSourceString_ ; }
3852- }
3868+ public bool IsSourceString => isSourceString_ ;
38533869
38543870 /// <summary>
38553871 /// Get the length of the comment when represented as raw bytes.
@@ -3894,15 +3910,15 @@ private void MakeTextAvailable()
38943910 {
38953911 if ( comment_ == null )
38963912 {
3897- comment_ = ZipStrings . ConvertToString ( rawComment_ ) ;
3913+ comment_ = _encoding . GetString ( rawComment_ ) ;
38983914 }
38993915 }
39003916
39013917 private void MakeBytesAvailable ( )
39023918 {
39033919 if ( rawComment_ == null )
39043920 {
3905- rawComment_ = ZipStrings . ConvertToArray ( comment_ ) ;
3921+ rawComment_ = _encoding . GetBytes ( comment_ ) ;
39063922 }
39073923 }
39083924
@@ -3911,7 +3927,7 @@ private void MakeBytesAvailable()
39113927 /// </summary>
39123928 /// <param name="zipString">The <see cref="ZipString"/> to convert to a string.</param>
39133929 /// <returns>The textual equivalent for the input value.</returns>
3914- static public implicit operator string ( ZipString zipString )
3930+ public static implicit operator string ( ZipString zipString )
39153931 {
39163932 zipString . MakeTextAvailable ( ) ;
39173933 return zipString . comment_ ;
@@ -3922,6 +3938,7 @@ static public implicit operator string(ZipString zipString)
39223938 private string comment_ ;
39233939 private byte [ ] rawComment_ ;
39243940 private readonly bool isSourceString_ ;
3941+ private readonly Encoding _encoding ;
39253942
39263943 #endregion Instance Fields
39273944 }
0 commit comments