So in the first example you want to compress your files into zip or rar archives after a user has uploaded?
And in the 2nd example you want to compress the files before? or after? the user has downloaded?
You can use the linux/unix zip function to do what you're after, and the unzip for the inverse.
PHP has NO native compression utility. You may find some luck with Pear but i doubt it. From what I have seen about what is available the PEAR bits only allow you to read compressed files, not actually create them. You WILL have to use the unix binaries to do what you're after.
Also uncompressing a file "after" download is not automagically possible, especially where PHP is concerned. Self-extracting files are possible, but to be honest you're better off relying on the client having winzip or an alternative program.