Archive - Originally posted on "The Horse's Mouth" - 2012-07-26 22:02:36 - Graham Ellis
Scenario - a contact emails you a [tjw]ar file containing - let's say - 20 files within it. You unpack it in a directory that already has 15 files in it ... and you end up with 30 file. Hmm - not what you might have expected; you were expecting 35 files in total.
What went wrong? Five of the files that you unpacked had names that were named the same as files that already existing in the directory in which you unpacked and by unpacking there you have overwritten the original files. Which could be a major issue if the files had the same name in the existing directory and archive, but the content differed.
It's to avoid problems like this that you need a best practise for creating and unpacking tar / jar / war files
Best Practise:
1. If you receive a jar / war / tar file, you should create an empty directory and unpack it within that. Yes - that might (should) result in a directory that contains only another directory ... but that's far better than the potential corruption if you unpack into a populated area.
2. If you are creating a jar / tar file, you should move to the directory level ABOVE the one in which the files are contained, and pack the file from there: tar czf htdocs_8.2.2.tgz htdocs_8.2.2
and you should ideally name the file and the directory with a version number or date.
Regrettably, if you're creating a war file (web archive), you'll need to flout good practise #2 and pack it from within the directory: jar cf /tmp/marmite.war *
as .war files need to be created in this was to be able to be unpacked and (auto)deployed without changing paths or renaming.