如何使用 C++ 压缩文件目录?
我正在开发一个使用 C++、Boost 和 Qt 的项目。 我了解如何使用 Qt 中的 qCompress() 函数等来压缩单个文件和字节流。
如何压缩包含多个文件(包括子目录)的目录? 我正在寻找跨平台(Mac、Win、Linux)的解决方案; 我不想启动一堆新流程。
是否有一种标准方法可以将多个文件中的字节流组合成一个压缩存档,或者 Boost iostream 库中是否有一个方便的函数或方法?
非常感谢您的帮助。
更新:QuaZip 库看起来真的很棒。 下载包(在“tests”目录中)中有一个示例,非常清楚地显示了如何压缩文件目录。
更新 2:在 Linux 构建环境中完成此任务后,我发现 QuaZip 根本无法与 Visual Studio 编译器一起使用。 也许可以解决所有这些编译器错误,但对任何沿着这条道路走下去的人来说,请注意。
I'm working on a project using C++, Boost, and Qt. I understand how to compress single files and bytestreams using, for example, the qCompress() function in Qt.
How do I zip a directory of multiple files, including subdirectories? I am looking for a cross-platform (Mac, Win, Linux) solution; I'd prefer not to fire off a bunch of new processes.
Is there a standard way to combine bytestreams from multiple files into a zipped archive, or maybe there is a convenience function or method that would be available in the Boost iostream library?
Many thanks for the assistance.
Update: The QuaZip library looks really great. There is an example in the download package (in the "tests" dir) that shows very clearly how to zip up a directory of files.
Update 2: After completing this task on my Linux build environment, I discovered that QuaZip doesn't work at all with the Visual Studio compiler. It may be possible to tackle all those compiler errors, but a word of caution to anyone looking down this path.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我找到了以下两个库:
啊,当然,我已经从 这个 关于目录压缩/解压缩的 Qt 邮件列表问题:)
I have found the following two libraries:
Ah, and of course, i have ripped those sites from this Qt Mailinglist question about Zipping/Unzipping of directories :)
仅供记录...
今天,我需要在 Visual C++ 中做一些非常类似的事情。 (虽然希望保持在其他平台上编译该项目的可能性;但不希望仅仅出于此目的而采用 Qt。)
最终使用了 Minizip 库。 它是用纯 C 语言编写的,但围绕它设计一个简单的 C++ 包装器是轻而易举的事,而且最终结果效果很好,至少对于我的目的来说是这样。
Just for the record...
Today, I needed to do something very similar in Visual C++. (Though wanted to maintain the possibility to compile the project on other platforms; however preferred not to adopt Qt just for this purpose.)
Ended up using the Minizip library. It is written in plain C, but devising a simple C++ wrapper around it was a breeze, and the end result works great, at least for my purposes.
我围绕 minizip 构建了一个包装器,添加了一些我需要的功能,并使其使用起来更好。 它确实使用最新的 c++11 并使用 Visual Studio 2013 开发,因此它应该可以开箱即用。
这里有完整的描述: https://github.com/sebastiandev/zipper
你可以压缩整个文件夹,流、向量等。还有一个很好的功能是完全在内存中完成所有操作。
I have built a wrapper around minizip adding some features that I needed and making it nicer to use it. Is does use the latest c++11 and is developed using Visual Studio 2013, so it should work out-of-the-box for you.
There's a full description here: https://github.com/sebastiandev/zipper
you can zip entire folders, streams, vectors, etc. Also a nice feature is doing everything entirely in memory.
Poco::Zip
也是一个选择,它有清晰的文档和一些演示代码。Poco::Zip 文档
Poco::Zip
is also a choice, it has clearly documentation and some code for demo.Poco::Zip Document
我在 Windows 上使用 VisualStudio 2010 尝试了 QuaZIP 0.4.3——仍然存在问题,但可以轻松解决。
使用 VS 构建:
I tried QuaZIP 0.4.3 on Windows with VisualStudio 2010 -- there are still issues but can be resolved easily.
To build with VS: