Skip to content

Commit b1a3d2f

Browse files
committed
Change all the variants of ZipFile.Add that take a compression method to validate the method themselves, and throw consistent exceptions for unsupported methods
1 parent 803b4a2 commit b1a3d2f

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

src/ICSharpCode.SharpZipLib/Zip/ZipFile.cs

+20-10
Original file line numberDiff line numberDiff line change
@@ -1667,11 +1667,7 @@ public void Add(string fileName, CompressionMethod compressionMethod, bool useUn
16671667
throw new ObjectDisposedException("ZipFile");
16681668
}
16691669

1670-
if (!ZipEntry.IsCompressionMethodSupported(compressionMethod))
1671-
{
1672-
throw new ArgumentOutOfRangeException(nameof(compressionMethod));
1673-
}
1674-
1670+
CheckSupportedCompressionMethod(compressionMethod);
16751671
CheckUpdating();
16761672
contentsEdited_ = true;
16771673

@@ -1696,11 +1692,7 @@ public void Add(string fileName, CompressionMethod compressionMethod)
16961692
throw new ArgumentNullException(nameof(fileName));
16971693
}
16981694

1699-
if (!ZipEntry.IsCompressionMethodSupported(compressionMethod))
1700-
{
1701-
throw new ArgumentOutOfRangeException(nameof(compressionMethod));
1702-
}
1703-
1695+
CheckSupportedCompressionMethod(compressionMethod);
17041696
CheckUpdating();
17051697
contentsEdited_ = true;
17061698

@@ -1774,6 +1766,7 @@ public void Add(IStaticDataSource dataSource, string entryName)
17741766
/// <param name="dataSource">The source of the data for this entry.</param>
17751767
/// <param name="entryName">The name to give to the entry.</param>
17761768
/// <param name="compressionMethod">The compression method to use.</param>
1769+
/// <exception cref="ArgumentOutOfRangeException">The compression method is not supported.</exception>
17771770
public void Add(IStaticDataSource dataSource, string entryName, CompressionMethod compressionMethod)
17781771
{
17791772
if (dataSource == null)
@@ -1786,6 +1779,7 @@ public void Add(IStaticDataSource dataSource, string entryName, CompressionMetho
17861779
throw new ArgumentNullException(nameof(entryName));
17871780
}
17881781

1782+
CheckSupportedCompressionMethod(compressionMethod);
17891783
CheckUpdating();
17901784

17911785
ZipEntry entry = EntryFactory.MakeFileEntry(entryName, false);
@@ -1801,6 +1795,7 @@ public void Add(IStaticDataSource dataSource, string entryName, CompressionMetho
18011795
/// <param name="entryName">The name to give to the entry.</param>
18021796
/// <param name="compressionMethod">The compression method to use.</param>
18031797
/// <param name="useUnicodeText">Ensure Unicode text is used for name and comments for this entry.</param>
1798+
/// <exception cref="ArgumentOutOfRangeException">The compression method is not supported.</exception>
18041799
public void Add(IStaticDataSource dataSource, string entryName, CompressionMethod compressionMethod, bool useUnicodeText)
18051800
{
18061801
if (dataSource == null)
@@ -1813,6 +1808,7 @@ public void Add(IStaticDataSource dataSource, string entryName, CompressionMetho
18131808
throw new ArgumentNullException(nameof(entryName));
18141809
}
18151810

1811+
CheckSupportedCompressionMethod(compressionMethod);
18161812
CheckUpdating();
18171813

18181814
ZipEntry entry = EntryFactory.MakeFileEntry(entryName, false);
@@ -1850,6 +1846,7 @@ public void Add(ZipEntry entry)
18501846
/// <param name="dataSource">The source of the data for this entry.</param>
18511847
/// <param name="entry">The entry to add.</param>
18521848
/// <remarks>This can be used to add file entries with a custom data source.</remarks>
1849+
/// <exception cref="ArgumentOutOfRangeException">The compression method is not supported.</exception>
18531850
public void Add(IStaticDataSource dataSource, ZipEntry entry)
18541851
{
18551852
if (entry == null)
@@ -1862,6 +1859,7 @@ public void Add(IStaticDataSource dataSource, ZipEntry entry)
18621859
throw new ArgumentNullException(nameof(dataSource));
18631860
}
18641861

1862+
CheckSupportedCompressionMethod(entry.CompressionMethod);
18651863
CheckUpdating();
18661864

18671865
AddUpdate(new ZipUpdate(dataSource, entry));
@@ -1884,6 +1882,18 @@ public void AddDirectory(string directoryName)
18841882
AddUpdate(new ZipUpdate(UpdateCommand.Add, dirEntry));
18851883
}
18861884

1885+
/// <summary>
1886+
/// Check if the specified compression method is supported for adding a new entry.
1887+
/// </summary>
1888+
/// <param name="compressionMethod">The compression method for the new entry.</param>
1889+
private void CheckSupportedCompressionMethod(CompressionMethod compressionMethod)
1890+
{
1891+
if (compressionMethod != CompressionMethod.Deflated && compressionMethod != CompressionMethod.Stored)
1892+
{
1893+
throw new ArgumentOutOfRangeException(nameof(compressionMethod), "Compression method not supported");
1894+
}
1895+
}
1896+
18871897
#endregion Adding Entries
18881898

18891899
#region Modifying Entries

0 commit comments

Comments
 (0)