From 194db482f5bb70962f829b8b335975f3576b647f Mon Sep 17 00:00:00 2001 From: Richard Webb Date: Sun, 9 Feb 2020 21:00:09 +0000 Subject: [PATCH] Change all the variants of ZipFile.Add that take a compression method to validate the method themselves, and throw consistent exceptions for unsupported methods --- src/ICSharpCode.SharpZipLib/Zip/ZipFile.cs | 34 ++++++++++++++-------- 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/src/ICSharpCode.SharpZipLib/Zip/ZipFile.cs b/src/ICSharpCode.SharpZipLib/Zip/ZipFile.cs index c28b603e3..5942b2c5c 100644 --- a/src/ICSharpCode.SharpZipLib/Zip/ZipFile.cs +++ b/src/ICSharpCode.SharpZipLib/Zip/ZipFile.cs @@ -1654,7 +1654,7 @@ private void AddUpdate(ZipUpdate update) /// Ensure Unicode text is used for name and comment for this entry. /// Argument supplied is null. /// ZipFile has been closed. - /// Compression method is not supported. + /// Compression method is not supported for creating entries. public void Add(string fileName, CompressionMethod compressionMethod, bool useUnicodeText) { if (fileName == null) @@ -1667,11 +1667,7 @@ public void Add(string fileName, CompressionMethod compressionMethod, bool useUn throw new ObjectDisposedException("ZipFile"); } - if (!ZipEntry.IsCompressionMethodSupported(compressionMethod)) - { - throw new ArgumentOutOfRangeException(nameof(compressionMethod)); - } - + CheckSupportedCompressionMethod(compressionMethod); CheckUpdating(); contentsEdited_ = true; @@ -1688,7 +1684,7 @@ public void Add(string fileName, CompressionMethod compressionMethod, bool useUn /// The name of the file to add. /// The compression method to use. /// ZipFile has been closed. - /// The compression method is not supported. + /// Compression method is not supported for creating entries. public void Add(string fileName, CompressionMethod compressionMethod) { if (fileName == null) @@ -1696,11 +1692,7 @@ public void Add(string fileName, CompressionMethod compressionMethod) throw new ArgumentNullException(nameof(fileName)); } - if (!ZipEntry.IsCompressionMethodSupported(compressionMethod)) - { - throw new ArgumentOutOfRangeException(nameof(compressionMethod)); - } - + CheckSupportedCompressionMethod(compressionMethod); CheckUpdating(); contentsEdited_ = true; @@ -1774,6 +1766,7 @@ public void Add(IStaticDataSource dataSource, string entryName) /// The source of the data for this entry. /// The name to give to the entry. /// The compression method to use. + /// Compression method is not supported for creating entries. public void Add(IStaticDataSource dataSource, string entryName, CompressionMethod compressionMethod) { if (dataSource == null) @@ -1786,6 +1779,7 @@ public void Add(IStaticDataSource dataSource, string entryName, CompressionMetho throw new ArgumentNullException(nameof(entryName)); } + CheckSupportedCompressionMethod(compressionMethod); CheckUpdating(); ZipEntry entry = EntryFactory.MakeFileEntry(entryName, false); @@ -1801,6 +1795,7 @@ public void Add(IStaticDataSource dataSource, string entryName, CompressionMetho /// The name to give to the entry. /// The compression method to use. /// Ensure Unicode text is used for name and comments for this entry. + /// Compression method is not supported for creating entries. public void Add(IStaticDataSource dataSource, string entryName, CompressionMethod compressionMethod, bool useUnicodeText) { if (dataSource == null) @@ -1813,6 +1808,7 @@ public void Add(IStaticDataSource dataSource, string entryName, CompressionMetho throw new ArgumentNullException(nameof(entryName)); } + CheckSupportedCompressionMethod(compressionMethod); CheckUpdating(); ZipEntry entry = EntryFactory.MakeFileEntry(entryName, false); @@ -1853,6 +1849,7 @@ public void Add(ZipEntry entry) /// /// The encryption method specified in is unsupported. /// + /// Compression method is not supported for creating entries. public void Add(IStaticDataSource dataSource, ZipEntry entry) { if (entry == null) @@ -1872,6 +1869,7 @@ public void Add(IStaticDataSource dataSource, ZipEntry entry) throw new NotSupportedException("Creation of AES encrypted entries is not supported"); } + CheckSupportedCompressionMethod(entry.CompressionMethod); CheckUpdating(); AddUpdate(new ZipUpdate(dataSource, entry)); @@ -1894,6 +1892,18 @@ public void AddDirectory(string directoryName) AddUpdate(new ZipUpdate(UpdateCommand.Add, dirEntry)); } + /// + /// Check if the specified compression method is supported for adding a new entry. + /// + /// The compression method for the new entry. + private void CheckSupportedCompressionMethod(CompressionMethod compressionMethod) + { + if (compressionMethod != CompressionMethod.Deflated && compressionMethod != CompressionMethod.Stored) + { + throw new NotImplementedException("Compression method not supported"); + } + } + #endregion Adding Entries #region Modifying Entries