Skip to content

Commit

Permalink
fix(zip): indicate that store/desc. entries cant be extracted
Browse files Browse the repository at this point in the history
  • Loading branch information
piksel committed Feb 13, 2021
1 parent bf6b70e commit 35548ae
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 8 deletions.
32 changes: 32 additions & 0 deletions src/ICSharpCode.SharpZipLib/Zip/ZipEntryExtensions.cs
@@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace ICSharpCode.SharpZipLib.Zip
{
/// <summary>
/// General ZipEntry helper extensions
/// </summary>
public static class ZipEntryExtensions
{
/// <summary>
/// Efficiently check if a <see cref="GeneralBitFlags">flag</see> is set without enum un-/boxing
/// </summary>
/// <param name="entry"></param>
/// <param name="flag"></param>
/// <returns>Returns whether the flag was set</returns>
public static bool HasFlag(this ZipEntry entry, GeneralBitFlags flag)
=> (entry.Flags & (int) flag) != 0;

/// <summary>
/// Efficiently set a <see cref="GeneralBitFlags">flag</see> without enum un-/boxing
/// </summary>
/// <param name="entry"></param>
/// <param name="flag"></param>
/// <param name="enabled">Whether the passed flag should be set (1) or cleared (0)</param>
public static void SetFlag(this ZipEntry entry, GeneralBitFlags flag, bool enabled = true)
=> entry.Flags = enabled
? entry.Flags | (int) flag
: entry.Flags & ~(int) flag;
}
}
17 changes: 9 additions & 8 deletions src/ICSharpCode.SharpZipLib/Zip/ZipInputStream.cs
Expand Up @@ -126,14 +126,15 @@ public string Password
/// <remarks>
/// The entry can only be decompressed if the library supports the zip features required to extract it.
/// See the <see cref="ZipEntry.Version">ZipEntry Version</see> property for more details.
///
/// Since <see cref="ZipInputStream"/> uses the local headers for extraction, entries with no compression combined with the
/// <see cref="GeneralBitFlags.Descriptor"/> flag set, cannot be extracted as the end of the entry data cannot be deduced.
/// </remarks>
public bool CanDecompressEntry
{
get
{
return (entry != null) && IsEntryCompressionMethodSupported(entry) && entry.CanDecompress;
}
}
public bool CanDecompressEntry
=> entry != null
&& IsEntryCompressionMethodSupported(entry)
&& entry.CanDecompress
&& (!entry.HasFlag(GeneralBitFlags.Descriptor) || entry.CompressionMethod != CompressionMethod.Stored);

/// <summary>
/// Is the compression method for the specified entry supported?
Expand All @@ -142,7 +143,7 @@ public bool CanDecompressEntry
/// Uses entry.CompressionMethodForHeader so that entries of type WinZipAES will be rejected.
/// </remarks>
/// <param name="entry">the entry to check.</param>
/// <returns>true if the compression methiod is supported, false if not.</returns>
/// <returns>true if the compression method is supported, false if not.</returns>
private static bool IsEntryCompressionMethodSupported(ZipEntry entry)
{
var entryCompressionMethod = entry.CompressionMethodForHeader;
Expand Down

0 comments on commit 35548ae

Please sign in to comment.