19

我目前正在使用 SharpZip api 来处理我的 zip 文件条目。它非常适合拉链和解压缩。不过,我无法确定文件是否为 zip。我需要知道是否有办法检测文件流是否可以解压缩。原来我用

FileStream lFileStreamIn = File.OpenRead(mSourceFile);
lZipFile = new ZipFile(lFileStreamIn);
ZipInputStream lZipStreamTester = new ZipInputStream(lFileStreamIn, mBufferSize);// not working
lZipStreamTester.Read(lBuffer, 0, 0);
if (lZipStreamTester.CanDecompressEntry)
{

LZipStreamTester 每次都变为 null 并且 if 语句失败。我试过有/没有缓冲区。任何人都可以就原因提供任何见解吗?我知道我可以检查文件扩展名。我需要比这更确定的东西。我也知道 zip 有一个神奇的#(PK something),但它不能保证它会一直存在,因为它不是格式的要求。

我还阅读了有关 .net 4.5 具有本机 zip 支持的信息,因此我的项目可能会迁移到该版本而不是Sharpzip,但我仍然不需要在这里看到类似于 CanDecompressEntry 的方法/参数:http: //msdn.microsoft.com/en-我们/图书馆/3z72378a%28v=vs.110%29

我最后的手段是使用 try catch 并尝试对文件进行解压缩。

4

6 回答 6

20

这是一个组件的基类,该组件需要处理未压缩、PKZIP 压缩 (sharpziplib) 或 GZip 压缩(内置于 .net)的数据。也许比你需要的多一点,但应该让你继续前进。这是使用@PhonicUK 的建议来解析数据流标头的示例。您在小工厂方法中看到的派生类处理了 PKZip 和 GZip 解压缩的细节。

abstract class Expander
{
    private const int ZIP_LEAD_BYTES = 0x04034b50;
    private const ushort GZIP_LEAD_BYTES = 0x8b1f;

    public abstract MemoryStream Expand(Stream stream); 
    
    internal static bool IsPkZipCompressedData(byte[] data)
    {
        Debug.Assert(data != null && data.Length >= 4);
        // if the first 4 bytes of the array are the ZIP signature then it is compressed data
        return (BitConverter.ToInt32(data, 0) == ZIP_LEAD_BYTES);
    }

    internal static bool IsGZipCompressedData(byte[] data)
    {
        Debug.Assert(data != null && data.Length >= 2);
        // if the first 2 bytes of the array are theG ZIP signature then it is compressed data;
        return (BitConverter.ToUInt16(data, 0) == GZIP_LEAD_BYTES);
    }

    public static bool IsCompressedData(byte[] data)
    {
        return IsPkZipCompressedData(data) || IsGZipCompressedData(data);
    }

    public static Expander GetExpander(Stream stream)
    {
        Debug.Assert(stream != null);
        Debug.Assert(stream.CanSeek);
        stream.Seek(0, 0);

        try
        {
            byte[] bytes = new byte[4];

            stream.Read(bytes, 0, 4);

            if (IsGZipCompressedData(bytes))
                return new GZipExpander();

            if (IsPkZipCompressedData(bytes))
                return new ZipExpander();

            return new NullExpander();
        }
        finally
        {
            stream.Seek(0, 0);  // set the stream back to the begining
        }
    }
}
于 2012-08-16T22:39:29.490 回答
13

查看https://stackoverflow.com/a/16587134/206730参考

检查以下链接:

icsharpcode-sharpziplib-validate-zip-文件

如何检查文件是否在 C 中压缩#

ZIP 文件始终以 0x04034b50(4 个字节)开头
查看更多:http ://en.wikipedia.org/wiki/Zip_(file_format)#File_headers

示例用法:

        bool isPKZip = IOHelper.CheckSignature(pkg, 4, IOHelper.SignatureZip);
        Assert.IsTrue(isPKZip, "Not ZIP the package : " + pkg);

// http://blog.somecreativity.com/2008/04/08/how-to-check-if-a-file-is-compressed-in-c/
    public static partial class IOHelper
    {
        public const string SignatureGzip = "1F-8B-08";
        public const string SignatureZip = "50-4B-03-04";

        public static bool CheckSignature(string filepath, int signatureSize, string expectedSignature)
        {
            if (String.IsNullOrEmpty(filepath)) throw new ArgumentException("Must specify a filepath");
            if (String.IsNullOrEmpty(expectedSignature)) throw new ArgumentException("Must specify a value for the expected file signature");
            using (FileStream fs = new FileStream(filepath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
            {
                if (fs.Length < signatureSize)
                    return false;
                byte[] signature = new byte[signatureSize];
                int bytesRequired = signatureSize;
                int index = 0;
                while (bytesRequired > 0)
                {
                    int bytesRead = fs.Read(signature, index, bytesRequired);
                    bytesRequired -= bytesRead;
                    index += bytesRead;
                }
                string actualSignature = BitConverter.ToString(signature);
                if (actualSignature == expectedSignature) return true;
                return false;
            }
        }

    }
于 2014-05-19T12:38:33.853 回答
8

您可以:

  • 使用 try-catch 结构并尝试读取潜在 zip 文件的结构
  • 解析文件头看是否为zip文件

ZIP 文件始终以 0x04034b50 作为其前 4 个字节 ( http://en.wikipedia.org/wiki/Zip_(file_format)#File_headers )

于 2012-08-16T22:26:20.173 回答
3

我使用了https://en.wikipedia.org/wiki/List_of_file_signatures,只是为我的 zip 文件添加了一个额外的字节,以区分我的 zip 文件和 Word 文档(它们共享前四个字节)。

这是我的代码:

public class ZipFileUtilities
{
    private static readonly byte[] ZipBytes1 = { 0x50, 0x4b, 0x03, 0x04, 0x0a };
    private static readonly byte[] GzipBytes = { 0x1f, 0x8b };
    private static readonly byte[] TarBytes = { 0x1f, 0x9d };
    private static readonly byte[] LzhBytes = { 0x1f, 0xa0 };
    private static readonly byte[] Bzip2Bytes = { 0x42, 0x5a, 0x68 };
    private static readonly byte[] LzipBytes = { 0x4c, 0x5a, 0x49, 0x50 };
    private static readonly byte[] ZipBytes2 = { 0x50, 0x4b, 0x05, 0x06 };
    private static readonly byte[] ZipBytes3 = { 0x50, 0x4b, 0x07, 0x08 };

    public static byte[] GetFirstBytes(string filepath, int length)
    {
        using (var sr = new StreamReader(filepath))
        {
            sr.BaseStream.Seek(0, 0);
            var bytes = new byte[length];
            sr.BaseStream.Read(bytes, 0, length);

            return bytes;
        }
    }

    public static bool IsZipFile(string filepath)
    {
        return IsCompressedData(GetFirstBytes(filepath, 5));
    }

    public static bool IsCompressedData(byte[] data)
    {
        foreach (var headerBytes in new[] { ZipBytes1, ZipBytes2, ZipBytes3, GzipBytes, TarBytes, LzhBytes, Bzip2Bytes, LzipBytes })
        {
            if (HeaderBytesMatch(headerBytes, data))
                return true;
        }

        return false;
    }

    private static bool HeaderBytesMatch(byte[] headerBytes, byte[] dataBytes)
    {
        if (dataBytes.Length < headerBytes.Length)
            throw new ArgumentOutOfRangeException(nameof(dataBytes), 
                $"Passed databytes length ({dataBytes.Length}) is shorter than the headerbytes ({headerBytes.Length})");

        for (var i = 0; i < headerBytes.Length; i++)
        {
            if (headerBytes[i] == dataBytes[i]) continue;

            return false;
        }

        return true;
    }

 }

可能有更好的编码方法,特别是字节比较,但由于它是一个可变长度的字节比较(取决于被检查的签名),我觉得至少这段代码是可读的——至少对我来说是这样。

于 2019-03-11T13:58:03.420 回答
2

如果您正在为 Web 编程,您可以检查文件内容类型:application/zip

于 2012-08-16T22:36:12.267 回答
1

感谢 dkackman 和 Kiquenet 提供上述答案。为了完整起见,以下代码使用签名来识别压缩 (zip) 文件。然后,您会增加复杂性,较新的 MS Office 文件格式也将返回匹配此签名查找(您的 .docx 和 .xlsx 文件等)。正如其他地方所说,这些确实是压缩档案,您可以使用 .zip 扩展名重命名文件并查看其中的 XML。

下面的代码,首先使用上面使用的签名检查 ZIP(压缩),然后我们对 MS Office 包进行后续检查。请注意,要使用 System.IO.Packaging.Package,您需要对“WindowsBase”的项目引用(即 .NET 程序集引用)。

    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;
    }

    /// <summary>
    /// 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.
    /// </summary>
    /// <param name="stream"></param>
    /// <returns></returns>
    private static bool IsPackage(this Stream stream)
    {
        Package package = Package.Open(stream, FileMode.Open, FileAccess.Read);
        return package.GetParts().Any();
    }
于 2018-06-16T10:52:59.140 回答