A 10 MeV/c $mu^+$ beam was stopped in helium gas of a few mbar in a magnetic field of 5 T. The muon swarm has been efficiently compressed from a length of 16 cm down to a few mm along the magnetic field axis (longitudinal compression) using electrostatic fields. The simulation reproduces the low energy interactions of slow muons in helium gas. Phase space compression occurs on the order of microseconds, compatible with the muon lifetime of 2 $mu$s. This paves the way for preparation of a high quality muon beam.