File tree 3 files changed +60
-11
lines changed
src/ICSharpCode.SharpZipLib/Zip
test/ICSharpCode.SharpZipLib.Tests/Zip
3 files changed +60
-11
lines changed Original file line number Diff line number Diff line change @@ -655,7 +655,7 @@ public bool LocalHeaderRequiresZip64
655
655
656
656
if ( ( versionToExtract == 0 ) && IsCrypted )
657
657
{
658
- trueCompressedSize += ZipConstants . CryptoHeaderSize ;
658
+ trueCompressedSize += ( ulong ) this . EncryptionOverheadSize ;
659
659
}
660
660
661
661
// TODO: A better estimation of the true limit based on compression overhead should be used
@@ -1013,6 +1013,26 @@ internal int AESOverheadSize
1013
1013
}
1014
1014
}
1015
1015
1016
+ /// <summary>
1017
+ /// Number of extra bytes required to hold the encryption header fields.
1018
+ /// </summary>
1019
+ internal int EncryptionOverheadSize
1020
+ {
1021
+ get
1022
+ {
1023
+ // Entry is not encrypted - no overhead
1024
+ if ( ! this . IsCrypted )
1025
+ return 0 ;
1026
+
1027
+ // Entry is encrypted using ZipCrypto
1028
+ if ( _aesEncryptionStrength == 0 )
1029
+ return ZipConstants . CryptoHeaderSize ;
1030
+
1031
+ // Entry is encrypted using AES
1032
+ return this . AESOverheadSize ;
1033
+ }
1034
+ }
1035
+
1016
1036
/// <summary>
1017
1037
/// Process extra data fields updating the entry based on the contents.
1018
1038
/// </summary>
Original file line number Diff line number Diff line change @@ -337,7 +337,7 @@ public void PutNextEntry(ZipEntry entry)
337
337
}
338
338
else
339
339
{
340
- WriteLeInt ( entry . IsCrypted ? ( int ) entry . CompressedSize + ZipConstants . CryptoHeaderSize : ( int ) entry . CompressedSize ) ;
340
+ WriteLeInt ( ( int ) entry . CompressedSize + entry . EncryptionOverheadSize ) ;
341
341
WriteLeInt ( ( int ) entry . Size ) ;
342
342
}
343
343
}
@@ -382,7 +382,7 @@ public void PutNextEntry(ZipEntry entry)
382
382
if ( headerInfoAvailable )
383
383
{
384
384
ed . AddLeLong ( entry . Size ) ;
385
- ed . AddLeLong ( entry . CompressedSize ) ;
385
+ ed . AddLeLong ( entry . CompressedSize + entry . EncryptionOverheadSize ) ;
386
386
}
387
387
else
388
388
{
@@ -540,14 +540,7 @@ public void CloseEntry()
540
540
541
541
if ( curEntry . IsCrypted )
542
542
{
543
- if ( curEntry . AESKeySize > 0 )
544
- {
545
- curEntry . CompressedSize += curEntry . AESOverheadSize ;
546
- }
547
- else
548
- {
549
- curEntry . CompressedSize += ZipConstants . CryptoHeaderSize ;
550
- }
543
+ curEntry . CompressedSize += curEntry . EncryptionOverheadSize ;
551
544
}
552
545
553
546
// Patch the header if possible
Original file line number Diff line number Diff line change @@ -42,6 +42,42 @@ public void ZipCryptoEncryption(CompressionMethod compressionMethod)
42
42
CreateZipWithEncryptedEntries ( "foo" , 0 , compressionMethod ) ;
43
43
}
44
44
45
+ /// <summary>
46
+ /// Test Known zero length encrypted entries with ZipOutputStream.
47
+ /// These are entries where the entry size is set to 0 ahead of time, so that PutNextEntry will fill in the header and there will be no patching.
48
+ /// Test with Zip64 on and off, as the logic is different for the two.
49
+ /// </summary>
50
+ [ Test ]
51
+ public void ZipOutputStreamEncryptEmptyEntries (
52
+ [ Values ] UseZip64 useZip64 ,
53
+ [ Values ( 0 , 128 , 256 ) ] int keySize ,
54
+ [ Values ( CompressionMethod . Stored , CompressionMethod . Deflated ) ] CompressionMethod compressionMethod )
55
+ {
56
+ using ( var ms = new MemoryStream ( ) )
57
+ {
58
+ using ( var zipOutputStream = new ZipOutputStream ( ms ) )
59
+ {
60
+ zipOutputStream . IsStreamOwner = false ;
61
+ zipOutputStream . Password = "password" ;
62
+ zipOutputStream . UseZip64 = useZip64 ;
63
+
64
+ ZipEntry zipEntry = new ZipEntry ( "emptyEntry" )
65
+ {
66
+ AESKeySize = keySize ,
67
+ CompressionMethod = compressionMethod ,
68
+ CompressedSize = 0 ,
69
+ Crc = 0 ,
70
+ Size = 0 ,
71
+ } ;
72
+
73
+ zipOutputStream . PutNextEntry ( zipEntry ) ;
74
+ zipOutputStream . CloseEntry ( ) ;
75
+ }
76
+
77
+ VerifyZipWith7Zip ( ms , "password" ) ;
78
+ }
79
+ }
80
+
45
81
[ Test ]
46
82
[ Category ( "Encryption" ) ]
47
83
[ Category ( "Zip" ) ]
You can’t perform that action at this time.
0 commit comments