Decompressing bzip2

Thomas Brix Larsen via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Apr 4 01:26:07 PDT 2016


On Sunday, 3 April 2016 at 02:03:29 UTC, stunaep wrote:
> I am trying to use the bzip2 bindings that are available on 
> code.dlang.org/packages, but I am having a really hard time 
> using it due to the pointers. It needs to be an array once it's 
> decompressed.
>
> Here is what I have:
>>>import std.stdio;
>>>import bzlib;
>>>
>>>void main(string[] args)
>>>{
>>>
>>>    File f = File("./test.bz2");
>>>    ubyte[] data = new ubyte[f.size];
>>>   f.rawRead(data);
>>>    writeln(data);
>>>
>>>    ubyte* output;
>>>    uint avail_out;
>>>    bz_stream* stream = new bz_stream();
>>>    stream.avail_out = avail_out;
>>>    stream.next_out = output;
>>>
>>>    int init_error = BZ2_bzDecompressInit(stream, 0, 0);
>>>    int bzipresult = BZ2_bzDecompress(stream);
>>>
>>>    stream.avail_in = cast(uint) data.length;
>>>    stream.next_in = cast(ubyte*) data;
>>>
>>>    bzipresult = BZ2_bzDecompress(stream);
>>>    int read = stream.total_out_lo32;
>>>    BZ2_bzDecompressEnd(stream);
>>>    delete stream;
>>>    writeln(output);
>>>}
>
> It's not working at all so any help would be very much 
> appreciated.

You need to allocate the output array:

import std.stdio;
import bzlib;

void main(string[] args)
{
     File f = File("./test.bz2");
     auto data = new ubyte[](f.size);
     f.rawRead(data);
     writeln(data);

     auto output = new ubyte[](4096);
     scope stream = new bz_stream();
     stream.avail_out = cast(uint)output.length;
     stream.next_out = output.ptr;

     int init_error = BZ2_bzDecompressInit(stream, 0, 0);
     int bzipresult = BZ2_bzDecompress(stream);

     stream.avail_in = cast(uint)data.length;
     stream.next_in = data.ptr;

     bzipresult = BZ2_bzDecompress(stream);
     int read = stream.total_out_lo32;
     BZ2_bzDecompressEnd(stream);
     writeln(output);
}


More information about the Digitalmars-d-learn mailing list