Work done is the product of force acting and the displacement. In this case, we have to find the work done by the gravity on the compressor during displacement.
Let r₁ and r₂ are the initial and the final position of the body. So, displacement will be, r₂ - r₁.
Work done, W = F × d
Since, F = mg
W = mg × (r₂ - r₁)
Hence, the work done by the gravity on the compressor is " mg × (r₂ - r₁) Joules".