 			stream.next_out = uncompressed;
 			stream.next_out_index = 0;
 			
+			stream.avail_out = outBytesLength;
+			stream.total_in = 0;
+			
+			
 			err = stream.inflateInit();
 			checkError(stream, err, "inflateInit");
 
 			var outcount:int = 0;
-			while (stream.total_out < data.length && stream.total_in < data.length)
-			{
-				stream.avail_in = stream.avail_out = 1; /* force small buffers */
+			while (true)
+			{
+				// force small buffers
+				stream.avail_in = 1;
+				stream.avail_out = 1;
+				
 				err = stream.inflate(JZlib.Z_NO_FLUSH);
-				if (err == JZlib.Z_STREAM_END)
-				{
-					trace("Zlib.uncompress. inflation stream ended");
-					break;
+				switch (err)
+				{
+					case JZlib.Z_STREAM_END:
+						// completed decompression, lets copy data and get out
+						trace("Zlib.uncompress. inflation stream ended");
+						break;
+					case JZlib.Z_BUF_ERROR:
+						stream.next_out_index = 0;
+						stream.avail_out = outBytesLength;
+						break;
+					default:
+						// unknown error
+						if (err != JZlib.Z_OK)
+						{
+							if (stream.msg == null) {
+								trace("Unknown error. Error code : " + err);
+							}
+							else {
+								trace("Unknown error. Error code : " + err + " and message : " + stream.msg);
+							}
+						}
 				}
 				
 				outcount++;
-				trace("Zlib.uncompress. outcount: " + outcount + ", stream.total_out: " + stream.total_out);
+				//trace("Zlib.uncompress. outcount: " + outcount + ", stream.total_out: " + stream.total_out);
 
 				if (!checkError(stream, err, "inflate"))
 				{

			
			trace("Zlib.compress. compressed.length: " + compressed.length);
			trace("Zlib.compress. compressed: " + compressed.toString());
			
			return compressed;
		}
		

		{
			data.position = 0;
			var uncompressed:ByteArray = new ByteArray();
			var outBytesLength:int = data.length * 2;
			
			var err:int;
			var i:int = 0;
			var j:int = 0;
