嘿,我想知道是否有人知道如何使用 gzip 压缩 tarball 文件。我已经检查了这个并成功压缩了一个tarball ,尽管我想用 gzip 而不是 libbz2 来压缩该 tarball。
我还尝试从 gzappend.c 示例实现 gztack
函数.gz" rel="nofollow noreferrer">zlib 源代码。 但最终出现了一堆错误和弃用警告,所以我认为我不会从弃用的示例中得到太多信息。
有谁知道我如何实现这一目标,最好使用 zlib 库?
Hey, I was wondering if anyone knew how I could compress a tarball file with gzip. I checked out this already and compressed a tarball successfully, although I want to then compress that tarball with gzip instead of libbz2.
I have also tried implementing the gztack
function from the gzappend.c
example from the zlib source code. but ended up getting a bunch of errors and deprecating warnings, so I figured I won't get much out of a deprecated example.
Does anyone know how I can achieve this, preferably with the zlib library?
发布评论
评论(4)
使用 zlib 例程
gzopen
打开压缩流,gcwrite
将数据写入其中并对其进行压缩,gzclose
将其关闭。这是一个将一个文件压缩到另一个文件的完整程序:像这样使用它:
Use the zlib routines
gzopen
to open a compressed stream,gcwrite
to write data to it and compress it, andgzclose
to close it. Here's a complete program that compresses one file to another:Use it like this:
您是否尝试过简单地使用 zlib?这里有一个教程:
http://www.zlib.net/zlib_how.html
这是制作 gzip 的好方法文件,当然。根据您所说的目标,我认为使用
popen()
或system()
来运行程序并不是那么好(需要机器安装其他东西,而不是提到如果你要经常这样做的话效率会降低)。Have you tried simply using zlib? There's a tutorial here:
http://www.zlib.net/zlib_how.html
That's a good way to make a gzip file, certainly. By your stated goals I would assume that using
popen()
orsystem()
to run a program is not so great (requires the machine to have something else installed, not to mention it's less efficient if you're going to do this a lot).在 shell 中有一个明确的方法可以执行此操作:
您可以让 C++ 程序使用
popen
系统调用来运行命令。您还可以为 zlib 编写一个包装器来模拟 c++ iostream(例如 http://www .cs.unc.edu/Research/compgeom/gzstream/)
There's a clear way to do this in the shell:
You can have the C++ program uses the
popen
system call to run the command.You can also write a wrapper to zlib that emulates c++ iostream (for example http://www.cs.unc.edu/Research/compgeom/gzstream/)
为
tar
命令构造 const char* 参数字符串并将其传递到 system() 函数可能是最简单的方法。或者使用 popen(),正如 Foo Bah 的回答提到的那样。我很难相信您的目标平台包含没有tar
或gzip
的平台,因为即使像 BusyBox 这样的恢复 shell 也可以访问tar
。一旦 system()/popen() 返回(显然检查返回代码是否成功),创建一个 ifstream 到压缩文件并对其进行任何您喜欢的操作。编辑:当你标记某个东西时,Linux 人们往往会认为这意味着特定且仅意味着 Linux。当然,
tar
不适用于 Windows 操作系统的标准安装,因此在这种情况下,是的,提供捆绑的 zlib dll 并按照 John 提到的那样使用 zlib。Constructing a const char* argument string for the
tar
command and passing it into the system() function incstdlib.h
is probably the simplest way to do that. Or using popen(), as Foo Bah's answer mentioned. I find it hard to believe that your target platforms include ones withouttar
orgzip
, as even recovery shells like BusyBox have access totar
. Once system()/popen() returns (check the return code for success obviously), create an ifstream to the compressed file and do whatever you like with it.EDIT: When you tag something Linux people tend to assume that means specifically and only Linux. Of course
tar
will not work for standard installs of Windows operating systems, so in that case, yes, provide a bundled zlib dll and use zlib as John mentions.