A two-qubit controlled-z gate is presented based on the non-instantaneous unitary kicks. Instead of putting two atoms through the cavity simultaneously, we make the atoms cross the cavity sequentially. The interaction between the second atom and the cavity plays the role for kicking the evolution of the system consisting of the first atom and cavity. By repeating the whole process N times, we obtain the controlled-z gate with a high fidelity. The effects of decoherence such as spontaneous emission and the loss of cavity on the average gate fidelity are investigated in virtue of master equation. Furthermore the method for achieving the multi-qubit controlled-z gate is also proposed.