I've to process many huge zip files.
I'd code an application that receive the data from a pipe, so I can simple unzip the data and send it (via pipe) to my app.
Something like that:
gzip -dc <file> | app
The problem is: How can I check if there is any problem in the gzip (exit status) from my app?
Depends on your shell. BASH has an array PIPESTATUS containing the return statuses of the last pipe chain you ran. I'm not sure how ksh does it. If you're in a basic bourne shell, that'd be tricky to do at all.
If your app doesn't have any idea where the info is supposed to end, that's a problem. if gzip dies instantly that's one thing, no data read, but if it dies halfway through? It doesn't get gzip's exit status and doesn't know gzip wasn't supposed to end there.
Your code could run its own gunzip instead, and would be able to get its exit status that way.
---------- Post updated at 02:04 PM ---------- Previous update was at 02:00 PM ----------
Something like:
$ cat popen.c
#include <stdio.h>
#include <sys/wait.h> // for wexitstatus
#include <sys/types.h>
int main(void)
{
int status;
char buf[512];
FILE *fp=popen("gunzip", "r"); // Decompress, reading from stdin
while(fgets(buf, 512, fp)) // Read until EOF
printf("%s", buf);
if(WEXITSTATUS(status=pclose(fp)))
printf("gunzip failed with status %d, do rollback\n", WEXITSTATUS(status));
}
$ gcc popen.c
$ echo asdf | gzip | ./a.out
asdf
$ echo asdf | ./a.out
gzip: stdin: not in gzip format
gunzip failed with status 1, do rollback
$