Skip to content

Commit fed3bd2

Browse files
authored
Merge PR #421: Have ZipFile.Add validate compression compability internally
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 dafdda9 commit fed3bd2

File tree

1 file changed

+22
-12
lines changed

1 file changed

+22
-12
lines changed

src/ICSharpCode.SharpZipLib/Zip/ZipFile.cs

+22-12
Original file line numberDiff line numberDiff line change
@@ -1654,7 +1654,7 @@ private void AddUpdate(ZipUpdate update)
16541654
/// <param name="useUnicodeText">Ensure Unicode text is used for name and comment for this entry.</param>
16551655
/// <exception cref="ArgumentNullException">Argument supplied is null.</exception>
16561656
/// <exception cref="ObjectDisposedException">ZipFile has been closed.</exception>
1657-
/// <exception cref="ArgumentOutOfRangeException">Compression method is not supported.</exception>
1657+
/// <exception cref="NotImplementedException">Compression method is not supported for creating entries.</exception>
16581658
public void Add(string fileName, CompressionMethod compressionMethod, bool useUnicodeText)
16591659
{
16601660
if (fileName == null)
@@ -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

@@ -1688,19 +1684,15 @@ public void Add(string fileName, CompressionMethod compressionMethod, bool useUn
16881684
/// <param name="fileName">The name of the file to add.</param>
16891685
/// <param name="compressionMethod">The compression method to use.</param>
16901686
/// <exception cref="ArgumentNullException">ZipFile has been closed.</exception>
1691-
/// <exception cref="ArgumentOutOfRangeException">The compression method is not supported.</exception>
1687+
/// <exception cref="NotImplementedException">Compression method is not supported for creating entries.</exception>
16921688
public void Add(string fileName, CompressionMethod compressionMethod)
16931689
{
16941690
if (fileName == null)
16951691
{
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="NotImplementedException">Compression method is not supported for creating entries.</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="NotImplementedException">Compression method is not supported for creating entries.</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);
@@ -1853,6 +1849,7 @@ public void Add(ZipEntry entry)
18531849
/// <exception cref="NotSupportedException">
18541850
/// The encryption method specified in <paramref name="entry"/> is unsupported.
18551851
/// </exception>
1852+
/// <exception cref="NotImplementedException">Compression method is not supported for creating entries.</exception>
18561853
public void Add(IStaticDataSource dataSource, ZipEntry entry)
18571854
{
18581855
if (entry == null)
@@ -1872,6 +1869,7 @@ public void Add(IStaticDataSource dataSource, ZipEntry entry)
18721869
throw new NotSupportedException("Creation of AES encrypted entries is not supported");
18731870
}
18741871

1872+
CheckSupportedCompressionMethod(entry.CompressionMethod);
18751873
CheckUpdating();
18761874

18771875
AddUpdate(new ZipUpdate(dataSource, entry));
@@ -1894,6 +1892,18 @@ public void AddDirectory(string directoryName)
18941892
AddUpdate(new ZipUpdate(UpdateCommand.Add, dirEntry));
18951893
}
18961894

1895+
/// <summary>
1896+
/// Check if the specified compression method is supported for adding a new entry.
1897+
/// </summary>
1898+
/// <param name="compressionMethod">The compression method for the new entry.</param>
1899+
private void CheckSupportedCompressionMethod(CompressionMethod compressionMethod)
1900+
{
1901+
if (compressionMethod != CompressionMethod.Deflated && compressionMethod != CompressionMethod.Stored)
1902+
{
1903+
throw new NotImplementedException("Compression method not supported");
1904+
}
1905+
}
1906+
18971907
#endregion Adding Entries
18981908

18991909
#region Modifying Entries

0 commit comments

Comments
 (0)