Read and write gzip files easily.

Kamil Slowikowski kslowikowski at gmail.com
Wed Feb 19 19:58:00 PST 2014


On Wednesday, 19 February 2014 at 16:36:29 UTC, Adam D. Ruppe 
wrote:
> And if you are writing, use new Compress(HeaderFormat.gzip) 
> then call the compress method and write what it returns to teh 
> file.

I successfully read and printed the contents of a gzipped file, 
but the documentation is too sparse for me to figure out why I 
can't write a gzipped file.

http://dlang.org/phobos/std_zlib.html#.Compress

I'd appreciate any tips.


Here's the output:

- - -
$ echo -e "hi there\nhere's some text in a file\n-K" | gzip > 
test.gz

$ zcat test.gz
hi there
here's some text in a file
-K

$ ./zfile.d test.gz out.gz
hi there
here's some text in a file
-K

$ zcat out.gz

gzip: out.gz: unexpected end of file
- - -


And the code:

- - -
#!/usr/bin/env rdmd
// zfile.d
import std.stdio,
        std.stream,
        std.zlib,
        std.c.process,
        std.process,
        std.file;

void main(string[] args)
{
     if (args.length != 3) {
         writefln("Usage: ./%s <file> <output>", args[0]);
         exit(0);
     }

     // Read command line arguments.
     string filename = args[1];
     string outfile = args[2];
     auto len = filename.length;

     std.file.File input;
     // Automatically decompress the file if it ends with "gz".
     if (filename[len - 2 .. len] == "gz") {
         auto pipe = pipeShell("gunzip -c " ~ filename);
         input = pipe.stdout;
     } else {
         input = std.stdio.File(filename);
     }

     // Write data to a stream in memory
     auto mem = new MemoryStream();
     string line;
     while ((line = input.readln()) !is null) {
         mem.write(line);
         // Also write the line to stdout.
         write(line);
     }

     // Put the uncompressed data into a new gz file.
     auto comp = new Compress(HeaderFormat.gzip);
     auto compressed = comp.compress(mem.data);
     //comp.flush(); // Does not fix the problem.

     // See the raw compressed bytes.
     //writeln(cast(ubyte[])compressed);

     // Write compressed output to a file.
     with (new std.stream.File(outfile, FileMode.OutNew)) {
         writeExact(compressed.ptr, compressed.length);
         //write(cast(ubyte[])compressed); // Also does not work.
     }
}
- - -


More information about the Digitalmars-d mailing list