Skip to content

Commit 5d02051

Browse files
authored
Merge PR #350: Add tests and overload for ZipEntry.HostSystem
1 parent 88cfaa6 commit 5d02051

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed

src/ICSharpCode.SharpZipLib/Zip/ZipFile.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1839,6 +1839,29 @@ public void Add(ZipEntry entry)
18391839
AddUpdate(new ZipUpdate(UpdateCommand.Add, entry));
18401840
}
18411841

1842+
/// <summary>
1843+
/// Add a <see cref="ZipEntry"/> with data.
1844+
/// </summary>
1845+
/// <param name="dataSource">The source of the data for this entry.</param>
1846+
/// <param name="entry">The entry to add.</param>
1847+
/// <remarks>This can be used to add file entries with a custom data source.</remarks>
1848+
public void Add(IStaticDataSource dataSource, ZipEntry entry)
1849+
{
1850+
if (entry == null)
1851+
{
1852+
throw new ArgumentNullException(nameof(entry));
1853+
}
1854+
1855+
if (dataSource == null)
1856+
{
1857+
throw new ArgumentNullException(nameof(dataSource));
1858+
}
1859+
1860+
CheckUpdating();
1861+
1862+
AddUpdate(new ZipUpdate(dataSource, entry));
1863+
}
1864+
18421865
/// <summary>
18431866
/// Add a directory entry to the archive.
18441867
/// </summary>

test/ICSharpCode.SharpZipLib.Tests/Zip/ZipFileHandling.cs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1371,6 +1371,7 @@ public void FileStreamNotClosedWhenNotOwner()
13711371
/// Check that input stream is closed when construction fails and leaveOpen is false
13721372
/// </summary>
13731373
[Test]
1374+
[Category("Zip")]
13741375
public void StreamClosedOnError()
13751376
{
13761377
var ms = new TrackedMemoryStream(new byte[32]);
@@ -1397,6 +1398,7 @@ public void StreamClosedOnError()
13971398
/// Check that input stream is not closed when construction fails and leaveOpen is true
13981399
/// </summary>
13991400
[Test]
1401+
[Category("Zip")]
14001402
public void StreamNotClosedOnError()
14011403
{
14021404
var ms = new TrackedMemoryStream(new byte[32]);
@@ -1418,5 +1420,66 @@ public void StreamNotClosedOnError()
14181420
Assert.IsTrue(blewUp, "Should have failed to load the file");
14191421
Assert.IsFalse(ms.IsClosed, "Underlying stream should NOT be closed");
14201422
}
1423+
1424+
[Test]
1425+
[Category("Zip")]
1426+
public void HostSystemPersistedFromOutputStream()
1427+
{
1428+
using (var ms = new MemoryStream())
1429+
{
1430+
var fileName = "testfile";
1431+
1432+
using (var zos = new ZipOutputStream(ms) { IsStreamOwner = false })
1433+
{
1434+
var source = new StringMemoryDataSource("foo");
1435+
zos.PutNextEntry(new ZipEntry(fileName) { HostSystem = (int)HostSystemID.Unix });
1436+
source.GetSource().CopyTo(zos);
1437+
zos.CloseEntry();
1438+
zos.Finish();
1439+
}
1440+
1441+
ms.Seek(0, SeekOrigin.Begin);
1442+
1443+
using (var zis = new ZipFile(ms))
1444+
{
1445+
var ze = zis.GetEntry(fileName);
1446+
Assert.NotNull(ze);
1447+
1448+
Assert.AreEqual((int)HostSystemID.Unix, ze.HostSystem);
1449+
Assert.AreEqual(ZipConstants.VersionMadeBy, ze.VersionMadeBy);
1450+
}
1451+
}
1452+
}
1453+
1454+
[Test]
1455+
[Category("Zip")]
1456+
public void HostSystemPersistedFromZipFile()
1457+
{
1458+
using (var ms = new MemoryStream())
1459+
{
1460+
var fileName = "testfile";
1461+
1462+
using (var zof = new ZipFile(ms, true))
1463+
{
1464+
var ze = zof.EntryFactory.MakeFileEntry(fileName, false);
1465+
ze.HostSystem = (int)HostSystemID.Unix;
1466+
1467+
zof.BeginUpdate();
1468+
zof.Add(new StringMemoryDataSource("foo"), ze);
1469+
zof.CommitUpdate();
1470+
}
1471+
1472+
ms.Seek(0, SeekOrigin.Begin);
1473+
1474+
using (var zis = new ZipFile(ms))
1475+
{
1476+
var ze = zis.GetEntry(fileName);
1477+
Assert.NotNull(ze);
1478+
1479+
Assert.AreEqual((int)HostSystemID.Unix, ze.HostSystem);
1480+
Assert.AreEqual(ZipConstants.VersionMadeBy, ze.VersionMadeBy);
1481+
}
1482+
}
1483+
}
14211484
}
14221485
}

0 commit comments

Comments
 (0)