C#.net确定zip文件 [英] C#.net identify zip file

查看:158
本文介绍了C#.net确定zip文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前使用的SharpZip API来处理我的zip文件条目。它的工作原理为灿烂的压缩和解压缩。虽然,我无法确定,如果一个文件是一个zip或没有。我需要知道,如果有一种方法来检测,如果一个文件流可以DECOM pressed。原来我用

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 or not. I need to know if there is a way to detect if a file stream can be decompressed. Originally I used

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成为空每次和if语句失败。我/试过不带缓冲。任何人都可以给任何见解,为什么?我知道我可以检查文件扩展名。我需要的东西比这更明确。我也知道,拉链有一个神奇的#(PK的东西),但它不能保证它永远在那里,因为它不是格式的要求。

The LZipStreamTester becomes null every time and the if statement fails. I tried it with/without a buffer. Can anybody give any insight as to why? I am aware that i can check for file extension. I need something that is more definitive than that. I am also aware that zip has a magic #(PK something), but it isn't a guarantee that it will always be there because it isn't a requirement of the format.

此外,我了解.NET 4.5原生有ZIP支持,所以我的项目可能迁移到不是sharpzip但我仍然需要没有看到类似CanDecom pressEntry这里的方法/参数:<一href=\"http://msdn.microsoft.com/en-us/library/3z72378a%28v=vs.110%29\">http://msdn.microsoft.com/en-us/library/3z72378a%28v=vs.110%29

Also i read about .net 4.5 having native zip support so my project may migrate to that instead of sharpzip but I still need didn't see a method/param similar to CanDecompressEntry here: http://msdn.microsoft.com/en-us/library/3z72378a%28v=vs.110%29

我不得已将使用尝试捕捉和文件试图一个解压缩。

My last resort will be to use a try catch and attempt an unzip on the file.

推荐答案

这是一个组件一个基类,需要处理的数据要么是uncom pressed,PKZIP COM pressed(sharpziplib)或GZip压缩COM pressed(建于.NET)。或许有点比你更需要,但应该让你去。这是使用@ PhonicUK的建议来解析数据流的报头的一个例子。你在小工厂马托看到的派生类处理的PKZip的细节和GZIP DECOM pression。

This is a base class for a component that needs to handle data that is either uncompressed, PKZIP compressed (sharpziplib) or GZip compressed (built in .net). Perhaps a bit more than you need but should get you going. This is an example of using @PhonicUK's suggestion to parse the header of the data stream. The derived classes you see in the little factory mathod handled the specifics of PKZip and GZip decompression.

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

这篇关于C#.net确定zip文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆