一時ファイルを作成せずにCで読み取るためにgzip圧縮ファイルを開く
質問
fopenとfscanfを介してCで読み取りたいgzip圧縮ファイルがいくつかあります。ファイルを一時ファイルに圧縮することなくこれを行う方法はありますか?
ありがとう。
解決
libzlibを使用して、gzip圧縮されたファイルを直接開くことができます。
また、「gzopen」も提供しています。 fopenと同様に動作するが、gzip圧縮されたファイルで動作する関数。ただし、fscanfは通常のFILEポインターを想定しているため、このようなハンドルではおそらく動作しません。
他のヒント
popen
が公正なゲームである場合、 fopen
および fscanf
を使用して実行できます。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
int main(int argc, char *argv[])
{
const char prefix[] = "zcat ";
const char *arg;
char *cmd;
FILE *in;
char buf[4096];
if (argc != 2) {
fprintf(stderr, "Usage: %s file\n", argv[0]);
return 1;
}
arg = argv[1];
cmd = malloc(sizeof(prefix) + strlen(arg) + 1);
if (!cmd) {
fprintf(stderr, "%s: malloc: %s\n", argv[0], strerror(errno));
return 1;
}
sprintf(cmd, "%s%s", prefix, arg);
in = popen(cmd, "r");
if (!in) {
fprintf(stderr, "%s: popen: %s\n", argv[0], strerror(errno));
return 1;
}
while (fscanf(in, "%s", buf) == 1)
printf("%s: got [%s]\n", argv[0], buf);
if (ferror(in)) {
fprintf(stderr, "%s: fread: %s\n", argv[0], strerror(errno));
return 1;
}
else if (!feof(in)) {
fprintf(stderr, "%s: %s: unconsumed input\n", argv[0], argv[1]);
return 1;
}
return 0;
}
例:
$ zcat file.gz
Every good boy does fine.
$ ./gzread file.gz
./gzread: got [Every]
./gzread: got [good]
./gzread: got [boy]
./gzread: got [does]
./gzread: got [fine.]
使用しないでください
sprintf(cmd, "zcat %s", argv[1]);
popen(cmd,"r");
.gzファイルを開きます。代わりにargv [1]を適切にエスケープしてください。そうしないと、特に一部が
などの引数argv [1]を注入する場合に脆弱性が発生する可能性があります123;rm -rf /
上記の指示を
に変更すると、すでに役立ちますsprintf(cmd, "zcat \'%s\'",argv[1]);
「\ 0」、「\」、「\;」などの文字をエスケープすることもできますなど。
gzscanf()での新規試行:
#include <stdio.h>
#include <stdarg.h>
#include <zlib.h>
#define MAXLEN 256
int gzscanf(gzFile *stream, const char *fmt, ...) {
/* read one line from stream (up to newline) and parse with sscanf */
va_list args;
va_start(args, fmt);
int n;
static char buf[MAXLEN];
if (NULL == gzgets(stream, buf, MAXLEN)) {
printf("gzscanf: Failed to read line from gz file.\n");
exit(EXIT_FAILURE);
}
n = vsscanf(buf, fmt, args);
va_end(args);
return n;
}
zlib を使用できますが、I / O呼び出しを置き換えてzlibにする必要があります。固有。
これを行うにはパイプを開く必要があります。擬似コードの基本的なフローは次のとおりです。
create pipe // man pipe
fork // man fork
if (parent) {
close the writing end of the pipe // man 2 close
read from the pipe // man 2 read
} else if (child) {
close the reading end of the pipe // man 2 close
overwrite the file descriptor for stdout with the writing end of the pipe // man dup2
call exec() with gzip and the relevant parameters // man 3 exec
}
これを行う方法の詳細については、コメントの man
ページを使用できます。
zlibを使用して、通常のファイルポインターにラップすることができます。これにより、fscanf、freadなどを使用できます。透過的に。
FILE *myfopen(const char *path, const char *mode)
{
#ifdef WITH_ZLIB
gzFile *zfp;
/* try gzopen */
zfp = gzopen(path,mode);
if (zfp == NULL)
return fopen(path,mode);
/* open file pointer */
return funopen(zfp,
(int(*)(void*,char*,int))gzread,
(int(*)(void*,const char*,int))gzwrite,
(fpos_t(*)(void*,fpos_t,int))gzseek,
(int(*)(void*))gzclose);
#else
return fopen(path,mode);
#endif
}
zlib
を使用して .gz
ファイルを開くのは非常に簡単です。 zlib.net に合理的なマニュアルがあります。
開始するための簡単な例を次に示します。
#include <stdio.h>
#include <zlib.h>
int main( int argc, char **argv )
{
// we're reading 2 text lines, and a binary blob from the given file
char line1[1024];
char line2[1024];
int blob[64];
if (argc > 1)
{
const char *filename = argv[1];
gzFile gz_in = gzopen( filename, "rb" ); // same as fopen()
if (gz_in != NULL)
{
if ( gzgets( gz_in, line1, sizeof(line1) ) != NULL ) // same as fgets()
{
if ( gzgets( gz_in, line2, sizeof(line2) ) != NULL )
{
if ( gzfread( blob, sizeof(int), 64, gz_in ) == 64 ) // same as fread()
{
printf("Line1: %s", line1);
printf("Line2: %s", line2);
// ...etc
}
}
}
gzclose(gz_in); // same as fclose()
}
else
{
printf( "Failed to GZ-open [%s]\n", filename );
}
}
return 0;
}
UNIXの gcc ... -lz
zlib
とリンクすることを忘れないでください 所属していません StackOverflow