using GLib; using ZLib; class ZLibTest : Object { // 16KB buffer size const int CHUNK = 16*1024; public int deflate (InputStream source, OutputStream dest) { uchar[] buf_in; uchar[] buf_out; buf_in = new uchar[CHUNK]; buf_out = new uchar[CHUNK]; uint have; int flush = Flush.NONE; int ret = Status.OK; var strm = DeflateStream.full(Compression.BEST_COMPRESSION, Algorithm.DEFLATED, (15 + 16), // + 16 for gzip output format 8, Strategy.DEFAULT ); // compress until deflate stream ends or end of file do { strm.avail_in = (int) source.read (buf_in, CHUNK, null); if(strm.avail_in == 0) { flush = Flush.FINISH; } else { flush = Flush.NONE; } strm.next_in = buf_in; // run deflate() on input until output buffer not full do { strm.avail_out = buf_out.length; strm.next_out = buf_out; ret = strm.deflate (flush); assert (ret != Status.STREAM_ERROR); // state not clobbered have = CHUNK - strm.avail_out; //print("%u\n", have); if (dest.write (buf_out, have, null) != have) { print("write error\n"); return Status.ERRNO; } } while (strm.avail_out == 0); assert (strm.avail_in == 0); } while (flush != Flush.FINISH); if(ret == Status.STREAM_END) print("Deflate: Status.OK\n"); else print("Deflate: Status.DATA_ERROR\n"); return ret == Status.STREAM_END ? Status.OK : Status.DATA_ERROR; } public static int main (string[] argv) { var zlib_test = new ZLibTest (); var infile = File.new_for_path ("dummyfile.txt"); var outfile = File.new_for_path ("dummyfile.txt.gz"); InputStream instream; OutputStream outstream; try { instream = infile.read (null); if(outfile.query_exists(null)) outfile.delete(null); outstream = outfile.create (FileCreateFlags.NONE, null); zlib_test.deflate (instream, outstream); } catch (Error e) { stderr.printf ("Error: %s\n", e.message); return -1; } return 0; } }