SOLVED: How to check if a ZIP file is encrypted in C#.NET using Xceed ZIP.NET

If you would like to chekc whether a ZIP file is encrypted in C#.NET using Xceed ZIP.NET you can use the following code.

As a ZIP file supports the ability to encrypt each individual file (rather than the ZIP archive itself) you need to check the contents of the ZIP file and see if the files inside it are encrypted.


/// <summary>
/// Determines whether any of the contents of the specified ZIP file is encrypted
/// </summary>
/// <param name="filename">The absolute path to the ZIP file to evaluate.</param>
/// <returns>A System.Boolean value that indicates whether any of the contents of the specified ZIP file is encrypted.</returns>
public static bool IsEncrypted(String filename)
{
    try
    {
        if (!File.Exists(filename)) { throw new FileNotFoundException(Resources.CompressionSupport.FileNotFoundException); }
        AbstractFile zipFile = new DiskFile(filename);
        ZipArchive zip = new ZipArchive(zipFile);
        AbstractFile[] files = zip.GetFiles(true);
        foreach (AbstractFile file in files)
        {
            ZippedFile zippedFile = (ZippedFile)file;
            if (zippedFile == null) { continue; }
            if (zippedFile.Encrypted) { return true; }
        }
        return false;
    }
    catch (Exception ex) { throw new CompressionSupportException(String.Format(Resources.CompressionSupport.IsEncryptedException, filename, ex.Message), ex);
    }
}


 While you're here -
Why not check out our Group Policy Audit and Documentation Tool?


Comments

Popular posts from this blog

Windows Server 2016, 2019, 2022, Windows 10 and Windows 11: Date and time "Some settings are managed by your organization".

TFTPD32 or TFTPD64 reports Bind error 10013 An attempt was made to access a socket in a way forbidden by its access permissions.

Windows Server 2019 desktop icons such as My Computer, Windows cannot access the specified device, path, or file.