I am currently using the SharpZip api to handle my zip file entries. It works splendid for zipping and unzipping. Though, I am having trouble identifying if a file is a zip
Thanks to dkackman and Kiquenet for answers above. For completeness, the below code uses the signature to identify compressed (zip) files. You then have the added complexity that the newer MS Office file formats will also return match this signature lookup (your .docx and .xlsx files etc). As remarked upon elsewhere, these are indeed compressed archives, you can rename the files with a .zip extension and have a look at the XML inside.
Below code, first does a check for ZIP (compressed) using the signatures used above, and we then have a subsequent check for the MS Office packages. Note that to use the System.IO.Packaging.Package you need a project reference to "WindowsBase" (that is a .NET assembly reference).
private const string SignatureZip = "50-4B-03-04";
private const string SignatureGzip = "1F-8B-08";
public static bool IsZip(this Stream stream)
{
if (stream.Position > 0)
{
stream.Seek(0, SeekOrigin.Begin);
}
bool isZip = CheckSignature(stream, 4, SignatureZip);
bool isGzip = CheckSignature(stream, 3, SignatureGzip);
bool isSomeKindOfZip = isZip || isGzip;
if (isSomeKindOfZip && stream.IsPackage()) //Signature matches ZIP, but it's package format (docx etc).
{
return false;
}
return isSomeKindOfZip;
}
///
/// MS .docx, .xslx and other extensions are (correctly) identified as zip files using signature lookup.
/// This tests if System.IO.Packaging is able to open, and if package has parts, this is not a zip file.
///
///
///
private static bool IsPackage(this Stream stream)
{
Package package = Package.Open(stream, FileMode.Open, FileAccess.Read);
return package.GetParts().Any();
}