Electron-bunch compression using a dynamical nonlinearity correction for a compact x-ray free-electron laser