从.CAB档案或InfoPath XSN文件中提取的最小C#代码,保存在内存中。

14
最近我一直在尝试实现某些从InfoPath XSN文件(即.CAB归档)中提取文件的功能。经过在互联网上广泛搜索,似乎没有本机.NET API来实现这一功能。所有当前解决方案都集中在大型库上,例如用C++编写的托管库将Cabinet.dll封装起来。
可悲的是,这一切都违反了我们公司的“不使用第三方库”政策。
自2.0版本以来,.NET增加了一个名为UnmanagedFunctionPointer的属性,允许使用__cdecl进行源级回调声明。在此之前,除非你不介意篡改IL,否则__stdcall是唯一的选择,而这种做法在这里也是被禁止的。我立刻知道这将允许实现一个相当小的C#包装器,用于Cabinet.dll,但我无法找到任何示例。
是否有人知道比以下方法更简洁的使用本机代码的方法?
我的当前解决方案(执行未托管代码,但完全工作,在32/64位上测试通过):
[StructLayout(LayoutKind.Sequential)]
public class CabinetInfo //Cabinet API: "FDCABINETINFO"
{
    public int cbCabinet;
    public short cFolders;
    public short cFiles;
    public short setID;
    public short iCabinet;
    public int fReserve;
    public int hasprev;
    public int hasnext;
}

public class CabExtract : IDisposable
{
    //If any of these classes end up with a different size to its C equivilent, we end up with crash and burn.
    [StructLayout(LayoutKind.Sequential)]
    private class CabError //Cabinet API: "ERF"
    {
        public int erfOper;
        public int erfType;
        public int fError;
    }

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
    private class FdiNotification //Cabinet API: "FDINOTIFICATION"
    {
        public int cb;
        public string psz1;
        public string psz2;
        public string psz3;
        public IntPtr userData;
        public IntPtr hf;
        public short date;
        public short time;
        public short attribs;
        public short setID;
        public short iCabinet;
        public short iFolder;
        public int fdie;
    }

    private enum FdiNotificationType
    {
        CabinetInfo,
        PartialFile,
        CopyFile,
        CloseFileInfo,
        NextCabinet,
        Enumerate
    }

    private class DecompressFile
    {
        public IntPtr Handle { get; set; }
        public string Name { get; set; }
        public bool Found { get; set; }
        public int Length { get; set; }
        public byte[] Data { get; set; }
    }

    [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
    private delegate IntPtr FdiMemAllocDelegate(int numBytes);

    [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
    private delegate void FdiMemFreeDelegate(IntPtr mem);

    [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
    private delegate IntPtr FdiFileOpenDelegate(string fileName, int oflag, int pmode);

    [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
    private delegate Int32 FdiFileReadDelegate(IntPtr hf,
                                              [In, Out] [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 2,
                                                  ArraySubType = UnmanagedType.U1)] byte[] buffer, int cb);

    [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
    private delegate Int32 FdiFileWriteDelegate(IntPtr hf,
                                               [In] [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 2,
                                                   ArraySubType = UnmanagedType.U1)] byte[] buffer, int cb);

    [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
    private delegate Int32 FdiFileCloseDelegate(IntPtr hf);

    [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
    private delegate Int32 FdiFileSeekDelegate(IntPtr hf, int dist, int seektype);

    [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
    private delegate IntPtr FdiNotifyDelegate(
        FdiNotificationType fdint, [In] [MarshalAs(UnmanagedType.LPStruct)] FdiNotification fdin);

    [DllImport("cabinet.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "FDICreate", CharSet = CharSet.Ansi)]
    private static extern IntPtr FdiCreate(
        FdiMemAllocDelegate fnMemAlloc,
        FdiMemFreeDelegate fnMemFree,
        FdiFileOpenDelegate fnFileOpen,
        FdiFileReadDelegate fnFileRead,
        FdiFileWriteDelegate fnFileWrite,
        FdiFileCloseDelegate fnFileClose,
        FdiFileSeekDelegate fnFileSeek,
        int cpuType,
        [MarshalAs(UnmanagedType.LPStruct)] CabError erf);

    [DllImport("cabinet.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "FDIIsCabinet", CharSet = CharSet.Ansi)]
    private static extern bool FdiIsCabinet(
        IntPtr hfdi,
        IntPtr hf,
        [MarshalAs(UnmanagedType.LPStruct)] CabinetInfo cabInfo);

    [DllImport("cabinet.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "FDIDestroy", CharSet = CharSet.Ansi)]
    private static extern bool FdiDestroy(IntPtr hfdi);

    [DllImport("cabinet.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "FDICopy", CharSet = CharSet.Ansi)]
    private static extern bool FdiCopy(
        IntPtr hfdi,
        string cabinetName,
        string cabinetPath,
        int flags,
        FdiNotifyDelegate fnNotify,
        IntPtr fnDecrypt,
        IntPtr userData);

    private readonly FdiFileCloseDelegate _fileCloseDelegate;
    private readonly FdiFileOpenDelegate _fileOpenDelegate;
    private readonly FdiFileReadDelegate _fileReadDelegate;
    private readonly FdiFileSeekDelegate _fileSeekDelegate;
    private readonly FdiFileWriteDelegate _fileWriteDelegate;
    private readonly FdiMemAllocDelegate _femAllocDelegate;
    private readonly FdiMemFreeDelegate _memFreeDelegate;

    private readonly CabError _erf;
    private readonly List<DecompressFile> _decompressFiles;
    private readonly byte[] _inputData;
    private IntPtr _hfdi;
    private bool _disposed;
    private const int CpuTypeUnknown = -1;

    public CabExtract(byte[] inputData)
    {
        _fileReadDelegate = FileRead;
        _fileOpenDelegate = InputFileOpen;
        _femAllocDelegate = MemAlloc;
        _fileSeekDelegate = FileSeek;
        _memFreeDelegate = MemFree;
        _fileWriteDelegate = FileWrite;
        _fileCloseDelegate = InputFileClose;
        _inputData = inputData;
        _decompressFiles = new List<DecompressFile>();
        _erf = new CabError();
        _hfdi = IntPtr.Zero;
    }

    private static IntPtr FdiCreate(
        FdiMemAllocDelegate fnMemAlloc,
        FdiMemFreeDelegate fnMemFree,
        FdiFileOpenDelegate fnFileOpen,
        FdiFileReadDelegate fnFileRead,
        FdiFileWriteDelegate fnFileWrite,
        FdiFileCloseDelegate fnFileClose,
        FdiFileSeekDelegate fnFileSeek,
        CabError erf)
    {
        return FdiCreate(fnMemAlloc, fnMemFree, fnFileOpen, fnFileRead, fnFileWrite,
                         fnFileClose, fnFileSeek, CpuTypeUnknown, erf);
    }

    private static bool FdiCopy(
        IntPtr hfdi,
        FdiNotifyDelegate fnNotify)
    {
        return FdiCopy(hfdi, "<notused>", "<notused>", 0, fnNotify, IntPtr.Zero, IntPtr.Zero);
    }

    private IntPtr FdiContext
    {
        get
        {
            if (_hfdi == IntPtr.Zero)
            {
                _hfdi = FdiCreate(_femAllocDelegate, _memFreeDelegate, _fileOpenDelegate, _fileReadDelegate, _fileWriteDelegate, _fileCloseDelegate, _fileSeekDelegate, _erf);
                if (_hfdi == IntPtr.Zero)
                    throw new ApplicationException("Failed to create FDI context.");
            }
            return _hfdi;
        }
    }

    public void Dispose()
    {
        Dispose(true);
    }

    private void Dispose(bool disposing)
    {
        if (!_disposed)
        {
            if (_hfdi != IntPtr.Zero)
            {
                FdiDestroy(_hfdi);
                _hfdi = IntPtr.Zero;
            }
            _disposed = true;
        }
    }

    private IntPtr NotifyCallback(FdiNotificationType fdint, FdiNotification fdin)
    {
        switch (fdint)
        {
            case FdiNotificationType.CopyFile:
                return OutputFileOpen(fdin);
            case FdiNotificationType.CloseFileInfo:
                return OutputFileClose(fdin);
            default:
                return IntPtr.Zero;
        }
    }

    private IntPtr InputFileOpen(string fileName, int oflag, int pmode)
    {
        var stream = new MemoryStream(_inputData);
        GCHandle gch = GCHandle.Alloc(stream);
        return (IntPtr)gch;
    }

    private int InputFileClose(IntPtr hf)
    {
        var stream = StreamFromHandle(hf);
        stream.Close();
        ((GCHandle)(hf)).Free();
        return 0;
    }

    private IntPtr OutputFileOpen(FdiNotification fdin)
    {
        var extractFile = _decompressFiles.Where(ef => ef.Name == fdin.psz1).SingleOrDefault();

        if (extractFile != null)
        {
            var stream = new MemoryStream();
            GCHandle gch = GCHandle.Alloc(stream);
            extractFile.Handle = (IntPtr)gch;
            return extractFile.Handle;
        }

        //Don't extract
        return IntPtr.Zero;
    }

    private IntPtr OutputFileClose(FdiNotification fdin)
    {
        var extractFile = _decompressFiles.Where(ef => ef.Handle == fdin.hf).Single();
        var stream = StreamFromHandle(fdin.hf);

        extractFile.Found = true;
        extractFile.Length = (int)stream.Length;

        if (stream.Length > 0)
        {
            extractFile.Data = new byte[stream.Length];
            stream.Position = 0;
            stream.Read(extractFile.Data, 0, (int)stream.Length);
        }

        stream.Close();
        return IntPtr.Zero;
    }

    private int FileRead(IntPtr hf, byte[] buffer, int cb)
    {
        var stream = StreamFromHandle(hf);
        return stream.Read(buffer, 0, cb);
    }

    private int FileWrite(IntPtr hf, byte[] buffer, int cb)
    {
        var stream = StreamFromHandle(hf);
        stream.Write(buffer, 0, cb);
        return cb;
    }

    private static Stream StreamFromHandle(IntPtr hf)
    {
        return (Stream)((GCHandle)hf).Target;
    }

    private IntPtr MemAlloc(int cb)
    {
        return Marshal.AllocHGlobal(cb);
    }

    private void MemFree(IntPtr mem)
    {
        Marshal.FreeHGlobal(mem);
    }

    private int FileSeek(IntPtr hf, int dist, int seektype)
    {
        var stream = StreamFromHandle(hf);
        return (int)stream.Seek(dist, (SeekOrigin)seektype);
    }

    public bool ExtractFile(string fileName, out byte[] outputData, out int outputLength)
    {
        if (_disposed)
            throw new ObjectDisposedException("CabExtract");

        var fileToDecompress = new DecompressFile();
        fileToDecompress.Found = false;
        fileToDecompress.Name = fileName;

        _decompressFiles.Add(fileToDecompress);

        FdiCopy(FdiContext, NotifyCallback);

        if (fileToDecompress.Found)
        {
            outputData = fileToDecompress.Data;
            outputLength = fileToDecompress.Length;
            _decompressFiles.Remove(fileToDecompress);
            return true;
        }

        outputData = null;
        outputLength = 0;
        return false;
    }

    public bool IsCabinetFile(out CabinetInfo cabinfo)
    {
        if (_disposed)
            throw new ObjectDisposedException("CabExtract");

        var stream = new MemoryStream(_inputData);
        GCHandle gch = GCHandle.Alloc(stream);

        try
        {
            var info = new CabinetInfo();
            var ret = FdiIsCabinet(FdiContext, (IntPtr)gch, info);
            cabinfo = info;
            return ret;
        }
        finally
        {
            stream.Close();
            gch.Free();
        }
    }

    public static bool IsCabinetFile(byte[] inputData, out CabinetInfo cabinfo)
    {
        using (var decomp = new CabExtract(inputData))
        {
            return decomp.IsCabinetFile(out cabinfo);
        }
    }

    //In an ideal world, this would take a stream, but Cabinet.dll seems to want to open the input several times.
    public static bool ExtractFile(byte[] inputData, string fileName, out byte[] outputData, out int length)
    {
        using (var decomp = new CabExtract(inputData))
        {
            return decomp.ExtractFile(fileName, out outputData, out length);
        }
    }

    //TODO: Add methods for enumerating/extracting multiple files
}

有趣。如果这是工作代码而不是问题(你表明它是),考虑将其作为一个(较小的)“项目”放在CodePlexgithub上,最好带一些测试。 - Christian.K
我曾经考虑过,但我一开始真正想要的只是从stackoverflow上进行一个小的“复制/粘贴”工作,而不是一个项目,所以这就是我回馈的内容。 - Matthew Millman
4
“我公司的‘无第三方库政策’” - 我希望你经常对此进行挑战。公司的开发人员能否最擅长编写每种类型的组件?他们是否能够理解与公司实际业务毫不相关的各种问题领域的细微差别呢? - Damien_The_Unbeliever
1
我希望你能定期挑战自己,大约每周三次。每天重新发明轮子也有一些积极的方面,比如我可以学到一些真正的实用知识,并且还能得到报酬。我在这里确实做到了。我必须说,写这篇文章比处理InfoPath表单要有趣得多。 - Matthew Millman
你能展示一下你如何使用上面的代码吗? - user844541
显示剩余6条评论
5个回答

2

你能否使用微软创建的其他库?虽然它不随框架一起提供,但是有一个用于处理Cab文件的微软库:

Microsoft.Deployment.Compression.Cab

可以按照以下方式使用:

CabInfo cab = new CabInfo(@"C:\data.cab");
cab.Unpack(@"C:\ExtractDir");

0

这个方法怎么样: 如何从.CAB文件中提取文件? 我的意思是JohnWein(一个MCC)的建议。 然后你可以对这些文件做什么 - 无论是将它们复制到目标目录中,就像引用示例中的foreach语句一样,还是只在内存中使用 - 都由你决定。 (我猜这也是Alexandru-Dan的意思 - 如果你不是在看问题而是在看答案。)

如果这种方法适用于您的情况,那么它是一个更简单的解决方案 - 没有任何第三方dll。

可以吗?


0
我们还有“无第三方库”政策(虽然很痛苦)。但是出于各种原因,我们需要“展开”IP表单。我们的解决方案是使用CABARC(这是一个来自Microsoft的exe文件,曾经包含在Windows安装中,尽管现在可能不再包含)。它可以在任何语言的shell中进行脚本编写,我们使用vbs或powershell。它的工作方式就像任何带有命令行开关的zip / unzip程序一样。

对于那些从上面找到好信息的人来说,这只是另一个选择。


有趣,但它会提取到文件系统,而不是内存中。 - vcsjones

0

扩展CAB文件与扩展ZIP文件相同。

您可以在这里找到一些代码。


1
你发布的代码片段正在使用java.util.zip.ZipFile。即使它可以打开.CAB文件,但这并不是在没有安装第三方库的系统上运行的解决方案,这违背了最初的目标。 - Matthew Millman

-1

ICSharpCode.SharpZipLib的文档中根本没有提到CAB文件。所以,除非您能提供一个代码示例,否则这个答案并不是很有用。 - Deantwo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接