Skip to content

Commit 96603b3

Browse files
committed
In ZipOutputStream.PutNextEntry, account for AES overhead when calculating compressed entry size
1 parent 251f189 commit 96603b3

File tree

2 files changed

+39
-11
lines changed

2 files changed

+39
-11
lines changed

src/ICSharpCode.SharpZipLib/Zip/ZipEntry.cs

+21-1
Original file line numberDiff line numberDiff line change
@@ -655,7 +655,7 @@ public bool LocalHeaderRequiresZip64
655655

656656
if ((versionToExtract == 0) && IsCrypted)
657657
{
658-
trueCompressedSize += ZipConstants.CryptoHeaderSize;
658+
trueCompressedSize += (ulong)this.EncryptionOverheadSize;
659659
}
660660

661661
// TODO: A better estimation of the true limit based on compression overhead should be used
@@ -1013,6 +1013,26 @@ internal int AESOverheadSize
10131013
}
10141014
}
10151015

1016+
/// <summary>
1017+
/// Number of extra bytes required to hold the encryptio 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+
10161036
/// <summary>
10171037
/// Process extra data fields updating the entry based on the contents.
10181038
/// </summary>

src/ICSharpCode.SharpZipLib/Zip/ZipOutputStream.cs

+18-10
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,14 @@ public void PutNextEntry(ZipEntry entry)
327327
}
328328
else
329329
{
330-
WriteLeInt(entry.IsCrypted ? (int)entry.CompressedSize + ZipConstants.CryptoHeaderSize : (int)entry.CompressedSize);
330+
int entryCompressedSize = (int)entry.CompressedSize;
331+
332+
if (entry.IsCrypted)
333+
{
334+
entryCompressedSize += entry.EncryptionOverheadSize;
335+
}
336+
337+
WriteLeInt(entryCompressedSize);
331338
WriteLeInt((int)entry.Size);
332339
}
333340
}
@@ -372,7 +379,15 @@ public void PutNextEntry(ZipEntry entry)
372379
if (headerInfoAvailable)
373380
{
374381
ed.AddLeLong(entry.Size);
375-
ed.AddLeLong(entry.CompressedSize);
382+
383+
long entryCompressedSize = entry.CompressedSize;
384+
385+
if (entry.IsCrypted)
386+
{
387+
entryCompressedSize += entry.EncryptionOverheadSize;
388+
}
389+
390+
ed.AddLeLong(entryCompressedSize);
376391
}
377392
else
378393
{
@@ -530,14 +545,7 @@ public void CloseEntry()
530545

531546
if (curEntry.IsCrypted)
532547
{
533-
if (curEntry.AESKeySize > 0)
534-
{
535-
curEntry.CompressedSize += curEntry.AESOverheadSize;
536-
}
537-
else
538-
{
539-
curEntry.CompressedSize += ZipConstants.CryptoHeaderSize;
540-
}
548+
curEntry.CompressedSize += curEntry.EncryptionOverheadSize;
541549
}
542550

543551
// Patch the header if possible

0 commit comments

Comments
 (0)