In this paper lossless compression with polar codes is considered. A polar encoding algorithm is developed and a method to design the code and compute the average compression rate for finite lengths is given. It is shown that the scheme achieves the optimal compression rate asymptotically. Furthermore, the proposed scheme has a very good performance at finite lengths. Both the encoding and decoding operations can be accomplished with complexity O(N logN) where N denotes the length of the code.