zlib c++ 和提取文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4696907/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 16:10:05  来源:igfitidea点击:

zlib c++ and extracting files

c++czlib

提问by reza

I have started to use zlib 1.2.5 and I do not see any routine to extract from a zip file? I read about a minizip application, part of the distribution.

我已经开始使用 zlib 1.2.5 并且我没有看到任何从 zip 文件中提取的例程?我读到了一个 minizip 应用程序,它是发行版的一部分。

Is that how it is supposed to be done?

这是应该怎么做的吗?

回答by Stephane Rolland

Yes, it does it well. (But if ever you don't like C code, you should look at 7-zip SDK that have code in C++ and C#.)

是的,它做得很好。(但如果您不喜欢 C 代码,则应该查看包含 C++ 和 C# 代码的 7-zip SDK。)

  • All the functions to browse and uncompress the files from a zip archive are in unzip.h
  • All the functions to compress and add files to a zip archive are in zip.h
  • 从 zip 存档中浏览和解压缩文件的所有功能都在 unzip.h
  • 压缩和添加文件到 zip 存档的所有功能都在 zip.h

(look at contrib\minizip\unzip.hand contrib\minizip\zip.h)

(查看contrib\minizip\unzip.hcontrib\minizip\zip.h

For example, decompressing: the unzOpen()functions of your zip file returns a unzFile

例如,解压缩:unzOpen()您的 zip 文件的函数返回一个unzFile

then use unzGoToFirstFile()and unzGoToNextFile()on this unzFileto browse through all files in the archive.

然后使用unzGoToFirstFile()unzGoToNextFile()在此unzFile通过的所有文件浏览存档。

then you get the file info for each file with unzGetCurrentFileInfo(), namely for its size,

然后你得到每个文件的文件信息unzGetCurrentFileInfo(),即它的大小,

surely you should call unzOpenCurrentFile()at some moment.

你当然应该unzOpenCurrentFile()在某个时候打电话。

and call unzReadCurrentFile()using the size from file info, retrieving the binary content of the archived file.

unzReadCurrentFile()使用文件信息中的大小调用,检索存档文件的二进制内容。

optionally, there is an opaque structure you can provide so as to use your own i/o function, but obviously, there is a default win32 implementation for file access, so you could even not worry about that.

可选地,您可以提供一个不透明的结构以便使用您自己的 i/o 功能,但显然,有一个默认的 win32 实现用于文件访问,因此您甚至不必担心。

PS:and dont forget to call unzCloseCurrentFile().

PS:不要忘记打电话unzCloseCurrentFile()

回答by Kirt Undercoffer

From: http://www.zlib.net/zlib_faq.html#faq11: 11. Can zlib handle .zip archives?

来自:http://www.zlib.net/zlib_faq.html#faq11 : 11. zlib 可以处理 .zip 档案吗?

Not by itself, no. See the directory contrib/minizip in the zlib distribution.

不是它自己,不是。请参阅 zlib 发行版中的目录 contrib/minizip。

There's not a tutorial there but the minizip zip.c source is exactly for IO (so presumably compression and decompression) on zip files using zlib.

那里没有教程,但 minizip zip.c 源代码完全适用于使用 zlib 对 zip 文件进行 IO(因此大概是压缩和解压缩)。

And still no tutorial BUT http://www.winimage.com/zLibDll/minizip.htmlgives more details.

仍然没有教程,但http://www.winimage.com/zLibDll/minizip.html提供了更多详细信息。

回答by Sebastian

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 (should be portable, but I haven't tested it on unix)

我已经围绕 minizip 构建了一个包装器,添加了一些我需要的功能并使其更好用。Is 确实使用最新的 c++11 并且是使用 Visual Studio 2013 开发的(应该是可移植的,但我还没有在 unix 上测试过)

There's a full description here: https://github.com/sebastiandev/zipper

这里有完整的描述:https: //github.com/sebastiandev/zipper

you can zip entire folders, streams, vectors, etc. Also a nice feature is doing everything entirely in memory.

您可以压缩整个文件夹、流、向量等。另外一个很好的功能是完全在内存中完成所有操作。