 						// unknown error
 						if (err != JZlib.Z_OK)
 						{
-							if (stream.msg == null) {
-								trace("Unknown error. Error code : " + err);
+							if (stream.msg == null)
+							{
+								trace("Zlib.uncompress. Unknown error. Error code : " + err);
 							}
-							else {
-								trace("Unknown error. Error code : " + err + " and message : " + stream.msg);
+							else
+							{
+								trace("Zlib.uncompress. Unknown error. Error code : " + err + " and message : " + stream.msg);
 							}
 						}
 				}
+				stream.avail_in = 1;
 				
-				outcount++;
-				//trace("Zlib.uncompress. outcount: " + outcount + ", stream.total_out: " + stream.total_out);
-
+				if (stream.total_in >= data.length)
+				{
+					emptyByteArrayIntoByteArray(chunk, uncompressed);
+					break;
+				}
+				
 				if (!checkError(stream, err, "inflate"))
 				{
 					break;

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

			}
			return true;
		}
		
		private function emptyByteArrayIntoByteArray(chunk:ByteArray, target:ByteArray):void
		{
			for (var j:uint = 0; j < chunk.length; j++)
			{
				target.writeByte(chunk.readByte());
			}
			chunk.clear();
		}
	}
}
