我想知道是否有人对此有任何见解。
我有一个本地 C++ 对象(在 DLL 中),它使用 boost::archive 写入它的数据。
该项目由 C# 应用程序(通过 C++/CLI 类)使用,我想将其中的几个存档存储在一个压缩文件中。我在 C# (DotNetZipFile) 中使用 Ionic.Zipfile。
目前,我正在写入一个临时文件,然后使用 ZipFile.UpdateFile 添加到存档中,但我想避免中间人。
所以,这就是我想要的:
C#:
ZipEntry ae = archive.UpdateEntry("Project", (f, stream) =>
{
pManagedObjectWrapper.Save(stream);
});
C++/命令行界面:
void ManagedObjectWrapper::Save(System::IO::Stream ^ stream)
{
std::ofstream *myofstream = MAGICALFunction(stream);
pUnmanagedObject->Save(myofstream);
}
C++:
void UnmanagedObject::Save(std::ofstream *myofstream)
{
boost::archive::binary_oarchive oa(*myofstream);
oa << *this;
}
对于什么是 MAGICALFunction
有任何帮助吗?
最佳答案
确实,使用适当的下溢/溢出实现子类化 streambuf 是可行的方法。
我不记得这很难,但我现在也没有合适的 Windows 机器来做这件事,所以你可以四处寻找现成的实现。
资源:
根据我的经验,在 SO 上找到好的工作、小样本并不难:
- How to boost::serialize into a sqlite::blob? ;我记得我使用了 Dietmar Kühl 之一在这里的答案(关于加密或压缩流的东西?)
埃德先生有 an excellent blog 文章¹ 向您展示了开始使用标准库实际需要的一切。它使用 3 个不同的例子来说明这一点。
不要忘记 Boost IOStreams。正如埃德先生所说:
If you were new to stream buffers before you read this post, I hope you feel a little more comfortable with them now. All the implementations were pretty basic, but a lot more is possible. However, I've found that once I start attempting more extravagant buffers, things can get fiddly pretty quickly. This is when I'll reach for the Boost IOStreams library, which provides a framework for implementing more involved buffers and streams.
It also allows you to treat the sources, sinks, filters and other concepts independently of one another. In our final example, we hard coded the sink as another std::ostream. What if we wanted the data to go somewhere that doesn't have a stream interface? The Boost IOStreams library allows more flexibility in this area by isolating concepts that I've had to mash together in my example code.
cppreference 有 the best ever visual explanation of stream buffers :
关于c# - 将 boost::archive 写入 C# 流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29513911/