www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - std.zlib uncompress issue

Hello.

Recently I was working with std.zlib and experienced error. After 
few hours I realized it was uncompress issue. I've made simple 
example that shows the issue:

import std.stdio,
        std.zlib,
        std.array,
        std.file,
        std.conv;

void main()
{
     createFile();
     loadFile();
}


void createFile()
{
     auto file = new File("test.txt", "wb");
     scope(exit) file.close;
     auto cpr = new Compress(9);
     string str = "abcdef";

     for(int i = 0; i < 500; i++)
     {
         file.write(cast(string)cpr.compress(str~to!string(i)));
     }
     file.write(cast(string)cpr.flush);
}

void loadFile()
{
     auto file = new File("test.txt", "rb");
     scope(exit) file.close;

     auto target = new File("test2.txt", "wb");
     scope(exit) target.close;

     auto ucpr = new UnCompress();

     // Making chunk size bigger than file size will (dirty)fix it.
     foreach(chunk; file.byChunk(64))
     {
         target.write(cast(string)ucpr.uncompress(chunk));
     }
     target.write(cast(string)ucpr.flush);
}

The code creates file with compressed data, then tries to 
uncompress it. That's where the issue is. 
UnCompress.uncompress(src) works only if the src is complete. If 
I pass half of it, I get exception (data error). I don't know how 
to fix it except increasing buffer size to incredible high 
values(some files may be pretty big).
Mar 04 2012