将流作为文件添加到ISO

时间:2015-06-04 22:49:53

标签: c# stream filesystems imapi

我在Windows中使用IMAPI2FS Image Mastering API,我试图弄清楚如何在生成ISO之前将流作为文件添加到文件系统映像。

var fsi = new MsftFileSystemImage();
fsi.ChooseImageDefaultsForMediaType(IMAPI_MEDIA_PHYSICAL_TYPE.IMAPI_MEDIA_TYPE_DISK);
fsi.FileSystemsToCreate = 
    FsiFileSystems.FsiFileSystemISO9660 | FsiFileSystems.FsiFileSystemJoliet;

using (var stream = new MemoryStream())
{
    stream.Write(buffer, 0, bufferSize);

    // Here is where I need to either instantiate an FsiStream and copy
    // stream to it, but I can't figure out how to do this.

    fsi.Root.AddFile(relativePathFromImageRoot, iStreamObject);
}

1 个答案:

答案 0 :(得分:3)

您正在尝试使用IMAPI2FS类型库,并且遇到了该库的常见问题。它的编写相当糟糕,很难直接从.NET程序中使用。大多数以api为目​​标的程序都是用C ++编写的,并使用imap2fs.h SDK头文件。

您遇到的具体问题是类型库导入器将AddFile()的第二个参数转换为FocStream类型的FsiStream。它是您无法创建的类型,它具有[noncreatable]类型库属性。类型库转换器误入歧途,方法实际采用IStream参数。您应该做的是创建自己的IStream实现并将其实例作为第二个参数传递。

这可以在C#version 4 dynamic 关键字的帮助下解决,因此编译器不会抱怨FsiStream。这是一个实现IStream的示例实现类:

using System;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
using System.IO;

[ComVisible(true)]
class FsiStreamImpl : IStream {
    private Stream source;
    private FsiStreamImpl() { }
    public static dynamic Create(Stream from) {
        var stream = new FsiStreamImpl();
        stream.source = from;
        return stream;
    }

    public void Read(byte[] pv, int cb, IntPtr pcbRead) {
        int read = source.Read(pv, 0, cb);
        Marshal.WriteInt32(pcbRead, read);
    }

    public void Seek(long dlibMove, int dwOrigin, IntPtr plibNewPosition) {
        long pos = source.Seek(dlibMove, (SeekOrigin)dwOrigin);
        Marshal.WriteInt64(plibNewPosition, pos);
    }

    public void Stat(out STATSTG pstatstg, int grfStatFlag) {
        var stat = new STATSTG();
        stat.type = 2;
        stat.cbSize = source.Length;
        stat.grfMode = 2;
        pstatstg = stat;
    }

    // Methods that we don't have to implement:
    public void Write(byte[] pv, int cb, IntPtr pcbWritten) {
        throw new NotImplementedException();
    }
    public void Clone(out IStream ppstm) {
        throw new NotImplementedException();
    }
    public void Commit(int grfCommitFlags) {
        throw new NotImplementedException();
    }
    public void SetSize(long libNewSize) {
        throw new NotImplementedException();
    }
    public void CopyTo(IStream pstm, long cb, IntPtr pcbRead, IntPtr pcbWritten) {
        throw new NotImplementedException();
    }
    public void LockRegion(long libOffset, long cb, int dwLockType) {
        throw new NotImplementedException();
    }
    public void Revert() {
        throw new NotImplementedException();
    }
    public void UnlockRegion(long libOffset, long cb, int dwLockType) {
        throw new NotImplementedException();
    }
}

现在您可以编写如下代码:

using (var stream = new MemoryStream(buffer))
{
    stream.Write(buffer, 0, bufferSize);
    stream.Position = 0;
    fsi.Root.AddFile(relativePathFromImageRoot, FsiStreamImpl.Create(stream)));
}

或像这样的代码,我测试过:

using (var file = new FileStream(@"c:\temp\test.txt", FileMode.Open, FileAccess.Read)) {
    fsi.Root.AddFile("test.txt", FsiStreamImpl.Create(file));
}

您可能会遇到更多问题,我只测试了您发布的代码段。我应该指出这个Codeproject.com project,由一个正在与IMAPI2作战的程序员编写。他采用了一种更广泛的方法,一种相当危险的方法,并用手工制作的C#声明重写了整个类型库。他花了很多时间在上面,支持问题只关注学习如何使用IMAPI2,所以你可能还可以依赖它。