如何使用 C++ 压缩文件目录?

发布于 2024-07-12 07:36:57 字数 477 浏览 9 评论 0原文

我正在开发一个使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

不羁少年 2024-07-19 07:36:57

我找到了以下两个库:

  • ZipIOS++。 似乎是“纯”C++。 他们没有明确将 Windows 列为受支持的平台。 所以我认为你应该自己试试运气。
  • QuaZIP。 基于Qt4。 其实看起来不错。 他们明确列出了 Windows(使用 mingw)。 显然,它是 [this] 库的 C++ 包装器。

啊,当然,我已经从 这个 关于目录压缩/解压缩的 Qt 邮件列表问题:)

I have found the following two libraries:

  • ZipIOS++. Seems to be "pure" C++. They don't list Windows explicitly as a supported platform. So i think you should try your luck yourself.
  • QuaZIP. Based on Qt4. Actually looks nice. They list Windows explicitly (Using mingw). Apparently, it is a C++ wrapper for [this] library.

Ah, and of course, i have ripped those sites from this Qt Mailinglist question about Zipping/Unzipping of directories :)

暗藏城府 2024-07-19 07:36:57

仅供记录...

今天,我需要在 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.

黑白记忆 2024-07-19 07:36:57

我围绕 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.

柒夜笙歌凉 2024-07-19 07:36:57

Poco::Zip 也是一个选择,它有清晰的文档和一些演示代码。

Poco::Zip 文档

Poco::Zip is also a choice, it has clearly documentation and some code for demo.

Poco::Zip Document

晨曦慕雪 2024-07-19 07:36:57

我在 Windows 上使用 VisualStudio 2010 尝试了 QuaZIP 0.4.3——仍然存在问题,但可以轻松解决。

使用 VS 构建:

  1. 使用 CMake 为 QuaZIP 配置和生成 VS 解决方案。
  2. 使用 VS 打开解决方案并构建 - 您首先会注意到它找不到“zlib.h”。
  3. 打开 quazip 项目的首选项,并将 Qt 的 Zlib 副本的路径添加到 C/C++ -> 常规 -> 其他包含目录:$(QTDIR)/src/3rdparty/zlib。
  4. 再次重建,您将收到大量警告和一个错误 C2491:QuaZipFile::staticMetaObject 上的 dllimport 静态问题。
  5. 这是因为 QuaZipFile 被声明为“class QUAZIP_EXPORT QuaZipFile”,并且 QUAZIP_EXPORT 需要根据 QUAZIP_BUILD 是否定义来解析为 dll 的 Q_DECL_EXPORT 和应用程序的 Q_DECL_IMPORT。 构建 QuaZIP 时,应该定义 QUAZIP_BUILD,但实际上没有定义——配置过程错误地定义了完全无用的“quazip_EXPORTS”。
  6. 要修复此问题,只需从所有构建配置中删除“quazip_EXPORTS”并添加 QUAZIP_BUILD 即可 - QuaZIP 现在可以正常构建。

I tried QuaZIP 0.4.3 on Windows with VisualStudio 2010 -- there are still issues but can be resolved easily.

To build with VS:

  1. Use CMake to configure and generate VS solution for QuaZIP.
  2. Open soltion with VS and build -- you'll first notice that it can't find 'zlib.h'.
  3. Open preferences for quazip project and add path to Qt's copy of Zlib to C/C++->General->Additional Include Directories: $(QTDIR)/src/3rdparty/zlib.
  4. Rebuild again and you'll get lots of warnings and one error C2491: dllimport static issue on QuaZipFile::staticMetaObject.
  5. This is because QuaZipFile is declared like "class QUAZIP_EXPORT QuaZipFile" and QUAZIP_EXPORT needs to resolve to Q_DECL_EXPORT for dll and to Q_DECL_IMPORT for application, based on whether QUAZIP_BUILD is defined or not. When building QuaZIP QUAZIP_BUILD should be defined but isn't -- configuration process defines in error completely useless "quazip_EXPORTS" instead.
  6. To fix, just remove "quazip_EXPORTS" from all build configurations and add QUAZIP_BUILD instead -- QuaZIP will now build fine.
一个人的夜不怕黑 2024-07-19 07:36:57
system("zip myarchive.zip *");
system("zip myarchive.zip *");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文