Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate compression method in all ZipFile.Add overloads #421

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 22 additions & 12 deletions src/ICSharpCode.SharpZipLib/Zip/ZipFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1654,7 +1654,7 @@ private void AddUpdate(ZipUpdate update)
/// <param name="useUnicodeText">Ensure Unicode text is used for name and comment for this entry.</param>
/// <exception cref="ArgumentNullException">Argument supplied is null.</exception>
/// <exception cref="ObjectDisposedException">ZipFile has been closed.</exception>
/// <exception cref="ArgumentOutOfRangeException">Compression method is not supported.</exception>
/// <exception cref="NotImplementedException">Compression method is not supported for creating entries.</exception>
public void Add(string fileName, CompressionMethod compressionMethod, bool useUnicodeText)
{
if (fileName == null)
Expand All @@ -1667,11 +1667,7 @@ public void Add(string fileName, CompressionMethod compressionMethod, bool useUn
throw new ObjectDisposedException("ZipFile");
}

if (!ZipEntry.IsCompressionMethodSupported(compressionMethod))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This just reads out as being wrong. Why would ZipFile ask an entry if it supports compressing it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it seems inside out.

{
throw new ArgumentOutOfRangeException(nameof(compressionMethod));
}

CheckSupportedCompressionMethod(compressionMethod);
CheckUpdating();
contentsEdited_ = true;

Expand All @@ -1688,19 +1684,15 @@ public void Add(string fileName, CompressionMethod compressionMethod, bool useUn
/// <param name="fileName">The name of the file to add.</param>
/// <param name="compressionMethod">The compression method to use.</param>
/// <exception cref="ArgumentNullException">ZipFile has been closed.</exception>
/// <exception cref="ArgumentOutOfRangeException">The compression method is not supported.</exception>
/// <exception cref="NotImplementedException">Compression method is not supported for creating entries.</exception>
public void Add(string fileName, CompressionMethod compressionMethod)
{
if (fileName == null)
{
throw new ArgumentNullException(nameof(fileName));
}

if (!ZipEntry.IsCompressionMethodSupported(compressionMethod))
{
throw new ArgumentOutOfRangeException(nameof(compressionMethod));
}

CheckSupportedCompressionMethod(compressionMethod);
CheckUpdating();
contentsEdited_ = true;

Expand Down Expand Up @@ -1774,6 +1766,7 @@ public void Add(IStaticDataSource dataSource, string entryName)
/// <param name="dataSource">The source of the data for this entry.</param>
/// <param name="entryName">The name to give to the entry.</param>
/// <param name="compressionMethod">The compression method to use.</param>
/// <exception cref="NotImplementedException">Compression method is not supported for creating entries.</exception>
public void Add(IStaticDataSource dataSource, string entryName, CompressionMethod compressionMethod)
{
if (dataSource == null)
Expand All @@ -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);
Expand All @@ -1801,6 +1795,7 @@ public void Add(IStaticDataSource dataSource, string entryName, CompressionMetho
/// <param name="entryName">The name to give to the entry.</param>
/// <param name="compressionMethod">The compression method to use.</param>
/// <param name="useUnicodeText">Ensure Unicode text is used for name and comments for this entry.</param>
/// <exception cref="NotImplementedException">Compression method is not supported for creating entries.</exception>
public void Add(IStaticDataSource dataSource, string entryName, CompressionMethod compressionMethod, bool useUnicodeText)
{
if (dataSource == null)
Expand All @@ -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);
Expand Down Expand Up @@ -1853,6 +1849,7 @@ public void Add(ZipEntry entry)
/// <exception cref="NotSupportedException">
/// The encryption method specified in <paramref name="entry"/> is unsupported.
/// </exception>
/// <exception cref="NotImplementedException">Compression method is not supported for creating entries.</exception>
public void Add(IStaticDataSource dataSource, ZipEntry entry)
{
if (entry == null)
Expand All @@ -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));
Expand All @@ -1894,6 +1892,18 @@ public void AddDirectory(string directoryName)
AddUpdate(new ZipUpdate(UpdateCommand.Add, dirEntry));
}

/// <summary>
/// Check if the specified compression method is supported for adding a new entry.
/// </summary>
/// <param name="compressionMethod">The compression method for the new entry.</param>
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
Expand Down