如何使用 gzip 压缩 tarball?

发布于 2024-10-17 06:18:29 字数 435 浏览 8 评论 0 原文

嘿,我想知道是否有人知道如何使用 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

花桑 2024-10-24 06:18:29

使用 zlib 例程 gzopen 打开压缩流, gcwrite 将数据写入其中并对其进行压缩,gzclose 将其关闭。这是一个将一个文件压缩到另一个文件的完整程序:

#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <zlib.h>

int main(int argc, char **argv)
{
  if(argc < 3)
  {
    fprintf(stderr, "Usage: %s input output\n", argv[0]);
    return 1;
  }

  // Open input & output files
  FILE *input = fopen(argv[1], "rb");
  if(input == NULL)
  {
    fprintf(stderr, "fopen: %s: %s\n", argv[1], strerror(errno));
    return 1;
  }

  gzFile output = gzopen(argv[2], "wb");
  if(output == NULL)
  {
    fprintf(stderr, "gzopen: %s: %s\n", argv[2], strerror(errno));
    fclose(input);
    return 1;
  }

  // Read in data from the input file, and compress & write it to the
  // output file
  char buffer[8192];
  int N;
  while((N = fread(buffer, 1, sizeof(buffer), input)) > 0)
  {
    gzwrite(output, buffer, N);
  }

  fclose(input);
  gzclose(output);

  return 0;
}

像这样使用它:

$ ./mygzip input output.gz
$ diff input <(gunzip < output.gz)  # Verify that it worked

Use the zlib routines gzopen to open a compressed stream, gcwrite to write data to it and compress it, and gzclose to close it. Here's a complete program that compresses one file to another:

#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <zlib.h>

int main(int argc, char **argv)
{
  if(argc < 3)
  {
    fprintf(stderr, "Usage: %s input output\n", argv[0]);
    return 1;
  }

  // Open input & output files
  FILE *input = fopen(argv[1], "rb");
  if(input == NULL)
  {
    fprintf(stderr, "fopen: %s: %s\n", argv[1], strerror(errno));
    return 1;
  }

  gzFile output = gzopen(argv[2], "wb");
  if(output == NULL)
  {
    fprintf(stderr, "gzopen: %s: %s\n", argv[2], strerror(errno));
    fclose(input);
    return 1;
  }

  // Read in data from the input file, and compress & write it to the
  // output file
  char buffer[8192];
  int N;
  while((N = fread(buffer, 1, sizeof(buffer), input)) > 0)
  {
    gzwrite(output, buffer, N);
  }

  fclose(input);
  gzclose(output);

  return 0;
}

Use it like this:

$ ./mygzip input output.gz
$ diff input <(gunzip < output.gz)  # Verify that it worked
天冷不及心凉 2024-10-24 06:18:29

您是否尝试过简单地使用 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() or system() 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).

从﹋此江山别 2024-10-24 06:18:29

在 shell 中有一个明确的方法可以执行此操作:

gzip ball.tar

您可以让 C++ 程序使用 popen 系统调用来运行命令。

您还可以为 zlib 编写一个包装器来模拟 c++ iostream(例如 http://www .cs.unc.edu/Research/compgeom/gzstream/

There's a clear way to do this in the shell:

gzip ball.tar

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/)

离笑几人歌 2024-10-24 06:18:29

tar 命令构造 const char* 参数字符串并将其传递到 system() 函数可能是最简单的方法。或者使用 popen(),正如 Foo Bah 的回答提到的那样。我很难相信您的目标平台包含没有 targzip 的平台,因为即使像 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 in cstdlib.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 without tar or gzip, as even recovery shells like BusyBox have access to tar. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文