@@ -1616,10 +1616,10 @@ public void AddFileWithAlternateName()
1616
1616
/// Test for https://github.com/icsharpcode/SharpZipLib/issues/147, when deleting items in a zip
1617
1617
/// </summary>
1618
1618
/// <param name="useZip64">Whether Zip64 should be used in the test archive</param>
1619
- [ TestCase ( UseZip64 . On ) ]
1620
- [ TestCase ( UseZip64 . Off ) ]
1619
+ /// <param name="updateMode">Whether safe or direct updates should be used</param>
1620
+ [ Test ]
1621
1621
[ Category ( "Zip" ) ]
1622
- public void TestDescriptorUpdateOnDelete ( UseZip64 useZip64 )
1622
+ public void TestDescriptorUpdateOnDelete ( [ Values ] UseZip64 useZip64 , [ Values ] FileUpdateMode updateMode )
1623
1623
{
1624
1624
MemoryStream msw = new MemoryStreamWithoutSeek ( ) ;
1625
1625
using ( ZipOutputStream outStream = new ZipOutputStream ( msw ) )
@@ -1640,15 +1640,10 @@ public void TestDescriptorUpdateOnDelete(UseZip64 useZip64)
1640
1640
{
1641
1641
using ( var zipFile = new ZipFile ( memoryStream , leaveOpen : true ) )
1642
1642
{
1643
- zipFile . BeginUpdate ( ) ;
1643
+ zipFile . BeginUpdate ( new MemoryArchiveStorage ( updateMode ) ) ;
1644
1644
zipFile . Delete ( "StripedMarlin" ) ;
1645
1645
zipFile . CommitUpdate ( ) ;
1646
- }
1647
-
1648
- memoryStream . Position = 0 ;
1649
1646
1650
- using ( var zipFile = new ZipFile ( memoryStream , leaveOpen : true ) )
1651
- {
1652
1647
Assert . That ( zipFile . TestArchive ( true ) , Is . True ) ;
1653
1648
}
1654
1649
}
@@ -1658,10 +1653,10 @@ public void TestDescriptorUpdateOnDelete(UseZip64 useZip64)
1658
1653
/// Test for https://github.com/icsharpcode/SharpZipLib/issues/147, when adding items to a zip
1659
1654
/// </summary>
1660
1655
/// <param name="useZip64">Whether Zip64 should be used in the test archive</param>
1661
- [ TestCase ( UseZip64 . On ) ]
1662
- [ TestCase ( UseZip64 . Off ) ]
1656
+ /// <param name="updateMode">Whether safe or direct updates should be used</param>
1657
+ [ Test ]
1663
1658
[ Category ( "Zip" ) ]
1664
- public void TestDescriptorUpdateOnAdd ( UseZip64 useZip64 )
1659
+ public void TestDescriptorUpdateOnAdd ( [ Values ] UseZip64 useZip64 , [ Values ] FileUpdateMode updateMode )
1665
1660
{
1666
1661
MemoryStream msw = new MemoryStreamWithoutSeek ( ) ;
1667
1662
using ( ZipOutputStream outStream = new ZipOutputStream ( msw ) )
@@ -1681,16 +1676,69 @@ public void TestDescriptorUpdateOnAdd(UseZip64 useZip64)
1681
1676
1682
1677
using ( var zipFile = new ZipFile ( memoryStream , leaveOpen : true ) )
1683
1678
{
1684
- zipFile . BeginUpdate ( ) ;
1679
+ zipFile . BeginUpdate ( new MemoryArchiveStorage ( updateMode ) ) ;
1685
1680
zipFile . Add ( new StringMemoryDataSource ( "stripey" ) , "Zebra" ) ;
1686
1681
zipFile . CommitUpdate ( ) ;
1682
+
1683
+ Assert . That ( zipFile . TestArchive ( true ) , Is . True ) ;
1687
1684
}
1685
+ }
1686
+ }
1687
+
1688
+ /// <summary>
1689
+ /// Test for https://github.com/icsharpcode/SharpZipLib/issues/147, when replacing items in a zip
1690
+ /// </summary>
1691
+ /// <param name="useZip64">Whether Zip64 should be used in the test archive</param>
1692
+ /// <param name="updateMode">Whether safe or direct updates should be used</param>
1693
+ [ Test ]
1694
+ [ Category ( "Zip" ) ]
1695
+ public void TestDescriptorUpdateOnReplacee ( [ Values ] UseZip64 useZip64 , [ Values ] FileUpdateMode updateMode )
1696
+ {
1697
+ MemoryStream msw = new MemoryStreamWithoutSeek ( ) ;
1698
+ using ( ZipOutputStream outStream = new ZipOutputStream ( msw ) )
1699
+ {
1700
+ outStream . UseZip64 = useZip64 ;
1701
+ outStream . IsStreamOwner = false ;
1702
+ outStream . PutNextEntry ( new ZipEntry ( "StripedMarlin" ) ) ;
1703
+ outStream . WriteByte ( 89 ) ;
1704
+ outStream . PutNextEntry ( new ZipEntry ( "WhiteMarlin" ) ) ;
1705
+ outStream . WriteByte ( 91 ) ;
1706
+ outStream . PutNextEntry ( new ZipEntry ( "Sailfish" ) ) ;
1707
+ outStream . WriteByte ( 3 ) ;
1708
+ }
1688
1709
1689
- memoryStream . Position = 0 ;
1710
+ var zipData = msw . ToArray ( ) ;
1711
+ Assert . IsTrue ( ZipTesting . TestArchive ( zipData ) ) ;
1690
1712
1713
+ using ( var memoryStream = new MemoryStream ( zipData ) )
1714
+ {
1691
1715
using ( var zipFile = new ZipFile ( memoryStream , leaveOpen : true ) )
1692
1716
{
1717
+ zipFile . BeginUpdate ( new MemoryArchiveStorage ( updateMode ) ) ;
1718
+ zipFile . Delete ( "WhiteMarlin" ) ;
1719
+ zipFile . Add ( new StringMemoryDataSource ( "Kajikia albida" ) , "WhiteMarlin" ) ;
1720
+ zipFile . CommitUpdate ( ) ;
1721
+
1693
1722
Assert . That ( zipFile . TestArchive ( true ) , Is . True ) ;
1723
+ Assert . That ( zipFile . Count , Is . EqualTo ( 3 ) ) ;
1724
+
1725
+ // Test for expected descriptor states:
1726
+ // 'StripedMarlin' should still have a descriptor in Direct update mode as the entry data will be kept, but won't have one
1727
+ // in Safe update mode as that recreates the whole archive.
1728
+ // 'WhiteMarlin' should no longer have one because the entry is new and didn't need one
1729
+ // 'Sailfish' should have its descriptor removed.
1730
+ var entry = zipFile . GetEntry ( "StripedMarlin" ) ;
1731
+
1732
+ if ( updateMode == FileUpdateMode . Direct )
1733
+ Assert . True ( ( ( GeneralBitFlags ) entry . Flags ) . HasFlag ( GeneralBitFlags . Descriptor ) ) ;
1734
+ else
1735
+ Assert . False ( ( ( GeneralBitFlags ) entry . Flags ) . HasFlag ( GeneralBitFlags . Descriptor ) ) ;
1736
+
1737
+ entry = zipFile . GetEntry ( "WhiteMarlin" ) ;
1738
+ Assert . False ( ( ( GeneralBitFlags ) entry . Flags ) . HasFlag ( GeneralBitFlags . Descriptor ) ) ;
1739
+
1740
+ entry = zipFile . GetEntry ( "Sailfish" ) ;
1741
+ Assert . False ( ( ( GeneralBitFlags ) entry . Flags ) . HasFlag ( GeneralBitFlags . Descriptor ) ) ;
1694
1742
}
1695
1743
}
1696
1744
}
0 commit comments