Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Temtaime committed Nov 2, 2021
1 parent 612969e commit bc49979
Showing 1 changed file with 28 additions and 3 deletions.
31 changes: 28 additions & 3 deletions src/ICSharpCode.SharpZipLib/Zip/ZipOutputStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,8 @@ internal void PutNextEntry(Stream stream, ZipEntry entry, long streamOffset = 0)
throw new InvalidOperationException("The Password property must be set before AES encrypted entries can be added");
}

entryIsPrecompressed = string.IsNullOrEmpty(Password) && method == CompressionMethod.Deflated && entry.Size >= 0 && entry.HasCrc && entry.CompressedSize >= 0;

int compressionLevel = defaultCompressionLevel;

// Clear flags that the library manages internally
Expand All @@ -322,7 +324,7 @@ internal void PutNextEntry(Stream stream, ZipEntry entry, long streamOffset = 0)
bool headerInfoAvailable;

// No need to compress - definitely no data.
if (entry.Size == 0)
if (entry.Size == 0 && !entryIsPrecompressed)
{
entry.CompressedSize = entry.Size;
entry.Crc = 0;
Expand Down Expand Up @@ -406,14 +408,17 @@ internal void PutNextEntry(Stream stream, ZipEntry entry, long streamOffset = 0)

// Activate the entry.
curEntry = entry;
size = 0;

if(entryIsPrecompressed)
return;

crc.Reset();
if (method == CompressionMethod.Deflated)
{
deflater_.Reset();
deflater_.SetLevel(compressionLevel);
}
size = 0;

}

/// <summary>
Expand Down Expand Up @@ -506,6 +511,17 @@ internal void WriteEntryFooter(Stream stream)
throw new InvalidOperationException("No open entry");
}

if(entryIsPrecompressed)
{
if(curEntry.CompressedSize != size)
{
throw new ZipException($"compressed size was {size}, but {curEntry.CompressedSize} expected");
}

offset += size;
return;
}

long csize = size;

// First finish the deflater, if appropriate
Expand Down Expand Up @@ -695,6 +711,13 @@ public override void Write(byte[] buffer, int offset, int count)
throw new ArgumentException("Invalid offset/count combination");
}

if(entryIsPrecompressed)
{
size += count;
baseOutputStream_.Write(buffer, offset, count);
return;
}

if (curEntry.AESKeySize == 0)
{
// Only update CRC if AES is not enabled
Expand Down Expand Up @@ -844,6 +867,8 @@ public override void Flush()
/// </summary>
private ZipEntry curEntry;

private bool entryIsPrecompressed;

private int defaultCompressionLevel = Deflater.DEFAULT_COMPRESSION;

private CompressionMethod curMethod = CompressionMethod.Deflated;
Expand Down

0 comments on commit bc49979

Please sign in to comment.