3

I'm looking for a combination of techniques to create a compressed file. Later I want to decompress that file on the fly and pass through pipe to the curl command.

The problem is the code I'm using produces a file that holds compressed data stream (as opposed to a file):

my $fh = IO::Zlib->new("file.gz", "wb9");
if (defined $fh) {
    print $fh "some big data here!\n";
    $fh->close;
}

Because of that I can't simply uncompress it using zcat:

zcat file.gz
zcat: can't stat: file.gz (file.gz.Z): No such file or directory

and the whole reason to do this is I was hoping to redirect zcat output as STDIN for curl command later:

zcat file.gz | curl -X PUT -d @- http://localhost:5822/

The above works if I create text file and gzip it. I was looking for an elegant way to do it from Perl, where I don't need to create temp file, zip it, delete temp file first.

This could be probably achieved in two ways:

1) find a way to create compressed file containing file data (as opposed to data stream)

2) find a Linux/Unix/OSX command that deals with files with stream compressed data (like zcat apparently can't)

Would be grateful for help re. both ways

msciwoj
  • 772
  • 7
  • 23
  • to avoid decompressing, you could generate the data compatible with `Content-Encoding: gzip` http header. – jfs Feb 01 '14 at 03:49
  • that's interesting. Could you leave some pointers on how to do this? – msciwoj Feb 01 '14 at 12:48
  • e.g., http://stackoverflow.com/questions/6866341/how-do-i-create-a-gzip-compressed-httpresponse – jfs Feb 01 '14 at 14:02
  • 2
    Well, it works for me. Are you sure you actually executed the perl script? Are you sure you're lookng in the right place for the generated file? (`file.gz`) – aidan Feb 05 '14 at 06:25
  • I'm not familiar with zcat (I would use "gzip -d file.gz"). But it appears the problem isn't in creating the file, but in convincing zcat to open the right file (it appears to want to append a ".Z" to the filename). If you're on a Unix-like system, use "file file.gz" to see if your file actually fits the conventions for a Gzipped file, and then look at the documentation for zcat to determine how to get it to open a Gzipped file instead of a file created with the old compress utility (which is what the ".Z" signifies). – Max Lybbert Apr 20 '14 at 03:39

1 Answers1

2

Your Perl code works from my testing, with the addition of a necessary use line:

use IO::Zlib;

You say that your code produces a file that holds compressed data stream (as opposed to a file). That doesn't make any sense. A file is a file. It's an object in a file system holding a sequence of bytes. Your code generates an ordinary file, the contents of which is the compressed output of Zlib.

I believe your problem is either that the code is failing to generate the file for some reason (lack of permissions?) or you're simply looking in the wrong place for the file.

Add some debugging to see if the file is being successfully opened. Replace your "new" line with

my $fh = new IO::Zlib;
$fh->open("/full/path/to/filename", "wb9") or die("couldn't open file for writing: $!");

And use a full path with the file name so you can be sure of where the file is being generated. (Normally it would be created in the current directory, but it's not clear where that is without context)

balazer
  • 282
  • 3
  • 7