是否有类似 Filestorage 类的东西来存储文件?
是否有类似类的东西可以用来存储文件和目录,就像使用 Zip 文件的方式一样?
由于我还没有找到任何“真正的”类来编写 Zip 文件(真正的类如 real 类中的那样), 如果能够将文件和目录存储在类似容器的文件中,那就太好了。
一个完美的 API 可能看起来像这样:
int main()
{
ContainerFile cntf("myContainer.cnt", ContainerFile::CREATE);
cntf.addFile("data/some-interesting-stuff.txt");
cntf.addDirectory("data/foo/");
cntf.addDirectory("data/bar/", ContainerFile::RECURSIVE);
cntf.close();
}
...我希望你能明白这个想法。 重要的要求是:
- 库必须是跨平台的
- *在这种情况下 GPL 是不可接受的(MIT 和 BSD 许可证是)
我已经考虑过创建一个基于 SQLite 的实现(及其存储二进制 blob 的能力)。不幸的是,似乎不可能在 SQLite 数据库中存储目录结构,这使得它在这种情况下几乎毫无用处。
寄希望于这样的类库难道就没用了吗?
Is there something like a class that might be used to store Files and directories in, just like the way Zip files might be used?
Since I haven't found any "real" class to write Zip files (real class as in real class),
It would be nice to be able to store Files and Directories in a container-like file.
A perfect API would probably look like this:
int main()
{
ContainerFile cntf("myContainer.cnt", ContainerFile::CREATE);
cntf.addFile("data/some-interesting-stuff.txt");
cntf.addDirectory("data/foo/");
cntf.addDirectory("data/bar/", ContainerFile::RECURSIVE);
cntf.close();
}
... I hope you get the Idea.
Important Requirements are:
- The Library must be crossplatform
- anything *GPL is not acceptable in this case (MIT and BSD License are)
I already played with the thought of creating an Implentation based on SQLite (and its ability to store binary blobs). Unfortunately, it seems impossible to store Directory structures in a SQLite Database, which makes it pretty much useless in this case.
Is it useless to hope for such a class library?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在 SQLite 数据库中,您可以存储类似目录的结构...您只需要有一个“目录”表,每个目录有一个条目,至少有一个索引和一个“父”字段(它保存另一个目录的索引)目录,如果没有父目录则为 0)。然后你就可以有一个“Files”表,其中包含文件属性、父目录的索引和文件内容。
就是这样,现在您在关系数据库中拥有了目录树。
In an SQLite db you can store directory-like structures... you just have to have a "Directories" table, with one entry for each directory, having at least an index and a "parent" field (which holds the index of another directory, or 0 if it has no parent). Then you can have a "Files" table, which contains file attributes, the index of the parent directory and the file content.
That's it, now you have your directory tree in a relational DB.
有人向我指出 PhysicsFS,它的 API 与您描述的类似,但它是一个纯 C API你需要的一切。可以轻松编写一个简单的面向对象的包装器。
Someone pointed me to PhysicsFS, which has an API similar to what you describe, but it's a pure C API that does everything you need. A trivial object-oriented wrapper could be easily written.
您可能想查看 http://www.cs.unc.edu/Research /compgeom/gzstream/
如果您自己制作,那么 redis 可能是比 SQLite 更好的选择,因为我相信它可以更好地处理二进制数据。
You might like to check out http://www.cs.unc.edu/Research/compgeom/gzstream/
If you are making your own then redis may be a better choice than SQLite as I believe it handles binary data better.
我花时间围绕 libarchive 编写了一个小型但有效的包装器。我并不完全熟悉 Libarchive 的所有功能,但结果符合我的需要:
archive_wrapper.cpp @ gist.github.com
它使用 libmars 作为字符串等。但我想用
替换
。mars::mstring
出现不会太难std::string当然,这个包装器可以在 MIT/X11 许可证下使用(就像 libmars 一样),这意味着您可以用它做任何您想做的事情。 ;-)
I took the time to write a tiny, yet working wrapper around libarchive. I'm not exactly familiar with all features of Libarchive, but the result fits what I needed:
archive_wrapper.cpp @ gist.github.com
It uses libmars for strings, etc. But I guess it wouldn't be too hard to replace the
mars::mstring
occurances withstd::string
.And of course this wrapper is available under the MIT/X11 License (just as libmars), which means you can do whatever you want with it. ;-)