How do I use std.zlib to write a struct of data to a binary file with compression?

TJB broughtj at gmail.com
Mon Sep 24 18:25:37 PDT 2012


> Since you're already set up with the Stream interface, try 
> creating a
> MemoryStream instead of a BufferedFile.  Write to the stream 
> just as you
> are now, then use the .data() property (on MemoryStream's 
> superclass,
> TArrayStream) to get an array of raw bytes.  You can feed this 
> array into
> the compress function in std.zlib to produce a new (hopefully 
> shorter)
> array of bytes that contains the compressed data.  Simply write 
> this
> compressed data to a file with `fout.writeExact(compressed.ptr,
> compressed.length)` and you're done.
>
> Since uncompress works better if you know the exact size of the
> uncompressed data, you might also consider writing the 
> uncompressed size
> of the data to your output file before the compressed data.
>
> Justin

Ok, I think I get it.  Can you help me set up the first part, 
writing to the MemoryStream?

TJB


More information about the Digitalmars-d-learn mailing list