Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/ICSharpCode.SharpZipLib/Zip/ZipFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2171,17 +2171,21 @@ private int WriteCentralDirectoryHeader(ZipEntry entry)
WriteLEInt((int)entry.Crc);
}

bool useExtraCompressedSize = false; //Do we want to store the compressed size in the extra data?
if ((entry.IsZip64Forced()) || (entry.CompressedSize >= 0xffffffff))
{
useExtraCompressedSize = true;
WriteLEInt(-1);
}
else
{
WriteLEInt((int)(entry.CompressedSize & 0xffffffff));
}

bool useExtraUncompressedSize = false; //Do we want to store the uncompressed size in the extra data?
if ((entry.IsZip64Forced()) || (entry.Size >= 0xffffffff))
{
useExtraUncompressedSize = true;
WriteLEInt(-1);
}
else
Expand All @@ -2205,12 +2209,12 @@ private int WriteCentralDirectoryHeader(ZipEntry entry)
{
ed.StartNewEntry();

if ((entry.Size >= 0xffffffff) || (useZip64_ == UseZip64.On))
if (useExtraUncompressedSize)
{
ed.AddLeLong(entry.Size);
}

if ((entry.CompressedSize >= 0xffffffff) || (useZip64_ == UseZip64.On))
if (useExtraCompressedSize)
{
ed.AddLeLong(entry.CompressedSize);
}
Expand Down
51 changes: 51 additions & 0 deletions test/ICSharpCode.SharpZipLib.Tests/Zip/ZipFileHandling.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,57 @@ public void Zip64Useage()
}
}

/// <summary>
/// Test that entries can be removed from a Zip64 file
/// </summary>
[Test]
[Category("Zip")]
public void Zip64Update()
{
using (var memStream = new MemoryStream())
{
using (ZipFile f = new ZipFile(memStream, leaveOpen: true))
{
f.UseZip64 = UseZip64.On;

var m = new StringMemoryDataSource("0000000");
f.BeginUpdate(new MemoryArchiveStorage());
f.Add(m, "a.dat");
f.Add(m, "b.dat");
f.CommitUpdate();
Assert.That(f.TestArchive(true), Is.True, "initial archive should be valid");
}

memStream.Seek(0, SeekOrigin.Begin);

using (ZipFile f = new ZipFile(memStream, leaveOpen: true))
{
Assert.That(f.Count, Is.EqualTo(2), "Archive should have 2 entries");

f.BeginUpdate(new MemoryArchiveStorage());
f.Delete("b.dat");
f.CommitUpdate();
Assert.That(f.TestArchive(true), Is.True, "modified archive should be valid");
}

memStream.Seek(0, SeekOrigin.Begin);

using (ZipFile f = new ZipFile(memStream, leaveOpen: true))
{
Assert.That(f.Count, Is.EqualTo(1), "Archive should have 1 entry");

for (int index = 0; index < f.Count; ++index)
{
Stream entryStream = f.GetInputStream(index);
var data = new MemoryStream();
StreamUtils.Copy(entryStream, data, new byte[128]);
string contents = Encoding.ASCII.GetString(data.ToArray());
Assert.That(contents, Is.EqualTo("0000000"), "archive member data should be correct");
}
}
}
}

[Test]
[Category("Zip")]
[Explicit]
Expand Down