@@ -367,7 +367,7 @@ public string Password
367
367
}
368
368
else
369
369
{
370
- key = PkzipClassic . GenerateKeys ( ZipStrings . ConvertToArray ( value ) ) ;
370
+ key = PkzipClassic . GenerateKeys ( ZipCryptoEncoding . GetBytes ( value ) ) ;
371
371
}
372
372
373
373
rawPassword_ = value ;
@@ -725,6 +725,14 @@ public ZipEntry this[int index]
725
725
}
726
726
}
727
727
728
+
729
+ /// <inheritdoc cref="StringCodec.ZipCryptoEncoding"/>
730
+ public Encoding ZipCryptoEncoding
731
+ {
732
+ get => _stringCodec . ZipCryptoEncoding ;
733
+ set => _stringCodec . ZipCryptoEncoding = value ;
734
+ }
735
+
728
736
#endregion Properties
729
737
730
738
#region Input Handling
@@ -1191,6 +1199,8 @@ private long TestLocalHeader(ZipEntry entry, HeaderTest tests)
1191
1199
throw new ZipException ( string . Format ( "Version required to extract this entry is invalid ({0})" , extractVersion ) ) ;
1192
1200
}
1193
1201
1202
+ var localEncoding = _stringCodec . ZipInputEncoding ( localFlags ) ;
1203
+
1194
1204
// Local entry flags dont have reserved bit set on.
1195
1205
if ( ( localFlags & ( int ) ( GeneralBitFlags . ReservedPKware4 | GeneralBitFlags . ReservedPkware14 | GeneralBitFlags . ReservedPkware15 ) ) != 0 )
1196
1206
{
@@ -1283,7 +1293,7 @@ private long TestLocalHeader(ZipEntry entry, HeaderTest tests)
1283
1293
}
1284
1294
1285
1295
// Name data has already been read convert it and compare.
1286
- string localName = ZipStrings . ConvertToStringExt ( localFlags , nameData ) ;
1296
+ string localName = localEncoding . GetString ( nameData ) ;
1287
1297
1288
1298
// Central directory and local entry name match
1289
1299
if ( localName != entry . Name )
@@ -1581,7 +1591,7 @@ public void CommitUpdate()
1581
1591
// Create an empty archive if none existed originally.
1582
1592
if ( entries_ . Length == 0 )
1583
1593
{
1584
- byte [ ] theComment = ( newComment_ != null ) ? newComment_ . RawComment : ZipStrings . ConvertToArray ( comment_ ) ;
1594
+ byte [ ] theComment = ( newComment_ != null ) ? newComment_ . RawComment : _stringCodec . ZipArchiveCommentEncoding . GetBytes ( comment_ ) ;
1585
1595
using ( ZipHelperStream zhs = new ZipHelperStream ( baseStream_ ) )
1586
1596
{
1587
1597
zhs . WriteEndOfCentralDirectory ( 0 , 0 , 0 , theComment ) ;
@@ -1619,7 +1629,7 @@ public void SetComment(string comment)
1619
1629
1620
1630
CheckUpdating ( ) ;
1621
1631
1622
- newComment_ = new ZipString ( comment ) ;
1632
+ newComment_ = new ZipString ( comment , _stringCodec . ZipArchiveCommentEncoding ) ;
1623
1633
1624
1634
if ( newComment_ . RawLength > 0xffff )
1625
1635
{
@@ -2147,7 +2157,8 @@ private void WriteLocalEntryHeader(ZipUpdate update)
2147
2157
WriteLEInt ( ( int ) entry . Size ) ;
2148
2158
}
2149
2159
2150
- byte [ ] name = ZipStrings . ConvertToArray ( entry . Flags , entry . Name ) ;
2160
+ var entryEncoding = _stringCodec . ZipInputEncoding ( entry . Flags ) ;
2161
+ byte [ ] name = entryEncoding . GetBytes ( entry . Name ) ;
2151
2162
2152
2163
if ( name . Length > 0xFFFF )
2153
2164
{
@@ -2254,7 +2265,8 @@ private int WriteCentralDirectoryHeader(ZipEntry entry)
2254
2265
WriteLEInt ( ( int ) entry . Size ) ;
2255
2266
}
2256
2267
2257
- byte [ ] name = ZipStrings . ConvertToArray ( entry . Flags , entry . Name ) ;
2268
+ var entryEncoding = _stringCodec . ZipInputEncoding ( entry . Flags ) ;
2269
+ byte [ ] name = entryEncoding . GetBytes ( entry . Name ) ;
2258
2270
2259
2271
if ( name . Length > 0xFFFF )
2260
2272
{
@@ -3081,7 +3093,7 @@ private void RunUpdates()
3081
3093
}
3082
3094
}
3083
3095
3084
- byte [ ] theComment = ( newComment_ != null ) ? newComment_ . RawComment : ZipStrings . ConvertToArray ( comment_ ) ;
3096
+ byte [ ] theComment = newComment_ ? . RawComment ?? _stringCodec . ZipArchiveCommentEncoding . GetBytes ( comment_ ) ;
3085
3097
using ( ZipHelperStream zhs = new ZipHelperStream ( workFile . baseStream_ ) )
3086
3098
{
3087
3099
zhs . WriteEndOfCentralDirectory ( updateCount_ , sizeEntries , centralDirOffset , theComment ) ;
@@ -3481,7 +3493,7 @@ private void ReadEntries()
3481
3493
byte [ ] comment = new byte [ commentSize ] ;
3482
3494
3483
3495
StreamUtils . ReadFully ( baseStream_ , comment ) ;
3484
- comment_ = ZipStrings . ConvertToString ( comment ) ;
3496
+ comment_ = _stringCodec . ZipArchiveCommentEncoding . GetString ( comment ) ;
3485
3497
}
3486
3498
else
3487
3499
{
@@ -3598,11 +3610,13 @@ private void ReadEntries()
3598
3610
long offset = ReadLEUint ( ) ;
3599
3611
3600
3612
byte [ ] buffer = new byte [ Math . Max ( nameLen , commentLen ) ] ;
3613
+ var entryEncoding = _stringCodec . ZipInputEncoding ( bitFlags ) ;
3601
3614
3602
3615
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 ( ) ;
3604
3618
3605
- var entry = new ZipEntry ( name , versionToExtract , versionMadeBy , ( CompressionMethod ) method )
3619
+ var entry = new ZipEntry ( name , versionToExtract , versionMadeBy , ( CompressionMethod ) method , unicode )
3606
3620
{
3607
3621
Crc = crc & 0xffffffffL ,
3608
3622
Size = size & 0xffffffffL ,
@@ -3635,7 +3649,7 @@ private void ReadEntries()
3635
3649
if ( commentLen > 0 )
3636
3650
{
3637
3651
StreamUtils . ReadFully ( baseStream_ , buffer , 0 , commentLen ) ;
3638
- entry . Comment = ZipStrings . ConvertToStringExt ( bitFlags , buffer , commentLen ) ;
3652
+ entry . Comment = entryEncoding . GetString ( buffer , 0 , commentLen ) ;
3639
3653
}
3640
3654
3641
3655
entries_ [ i ] = entry ;
@@ -3787,6 +3801,7 @@ private static void WriteEncryptionHeader(Stream stream, long crcValue)
3787
3801
private ZipEntry [ ] entries_ ;
3788
3802
private byte [ ] key ;
3789
3803
private bool isNewArchive_ ;
3804
+ private readonly StringCodec _stringCodec = new StringCodec ( ) ;
3790
3805
3791
3806
// Default is dynamic which is not backwards compatible and can cause problems
3792
3807
// with XP's built in compression which cant read Zip64 archives.
@@ -3825,19 +3840,23 @@ private class ZipString
3825
3840
/// Initialise a <see cref="ZipString"/> with a string.
3826
3841
/// </summary>
3827
3842
/// <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 )
3829
3845
{
3830
3846
comment_ = comment ;
3831
3847
isSourceString_ = true ;
3848
+ _encoding = encoding ;
3832
3849
}
3833
3850
3834
3851
/// <summary>
3835
3852
/// Initialise a <see cref="ZipString"/> using a string in its binary 'raw' form.
3836
3853
/// </summary>
3837
3854
/// <param name="rawString"></param>
3838
- public ZipString ( byte [ ] rawString )
3855
+ /// <param name="encoding"></param>
3856
+ public ZipString ( byte [ ] rawString , Encoding encoding )
3839
3857
{
3840
3858
rawComment_ = rawString ;
3859
+ _encoding = encoding ;
3841
3860
}
3842
3861
3843
3862
#endregion Constructors
@@ -3846,10 +3865,7 @@ public ZipString(byte[] rawString)
3846
3865
/// Get a value indicating the original source of data for this instance.
3847
3866
/// True if the source was a string; false if the source was binary data.
3848
3867
/// </summary>
3849
- public bool IsSourceString
3850
- {
3851
- get { return isSourceString_ ; }
3852
- }
3868
+ public bool IsSourceString => isSourceString_ ;
3853
3869
3854
3870
/// <summary>
3855
3871
/// Get the length of the comment when represented as raw bytes.
@@ -3894,15 +3910,15 @@ private void MakeTextAvailable()
3894
3910
{
3895
3911
if ( comment_ == null )
3896
3912
{
3897
- comment_ = ZipStrings . ConvertToString ( rawComment_ ) ;
3913
+ comment_ = _encoding . GetString ( rawComment_ ) ;
3898
3914
}
3899
3915
}
3900
3916
3901
3917
private void MakeBytesAvailable ( )
3902
3918
{
3903
3919
if ( rawComment_ == null )
3904
3920
{
3905
- rawComment_ = ZipStrings . ConvertToArray ( comment_ ) ;
3921
+ rawComment_ = _encoding . GetBytes ( comment_ ) ;
3906
3922
}
3907
3923
}
3908
3924
@@ -3911,7 +3927,7 @@ private void MakeBytesAvailable()
3911
3927
/// </summary>
3912
3928
/// <param name="zipString">The <see cref="ZipString"/> to convert to a string.</param>
3913
3929
/// <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 )
3915
3931
{
3916
3932
zipString . MakeTextAvailable ( ) ;
3917
3933
return zipString . comment_ ;
@@ -3922,6 +3938,7 @@ static public implicit operator string(ZipString zipString)
3922
3938
private string comment_ ;
3923
3939
private byte [ ] rawComment_ ;
3924
3940
private readonly bool isSourceString_ ;
3941
+ private readonly Encoding _encoding ;
3925
3942
3926
3943
#endregion Instance Fields
3927
3944
}
0 commit comments