Many times you like to group files and folders into a single file(archive), The motivation for that is sometimes because you like efficiently (smaller space, fast upload) to send the group of files and folder over the internet (mail , ftp..etc)as a single file, in other cases you just want to upload the group of files and folder to a web-server or a cloud storage in a single operation, you can find many other methods and technics how to do so, but one of the most simple and the efficient ways is by using the traditional ZIP command
Two main reasons to use the ZIP command , the first reason its because the ZIP command is a build-in archive tool that can be found in every MacOS machines , the second reason as you already probably knows, its because that the ZIP command its also a tool to compress the information that the files carries, means the single archive file will consume less storage space then the total storage space that the all files will consume all together as individuals files , of course their are many other data compression method that are relatively more efficient and will compress better for various type of files(such as audio\video..etc) , but the ZIP algorithm over the years proof its efficiency and simplicity to archives and compress files and data. Another important reason why we may benefits from using the ZIP tool, its because it is a Cross-Platform tool, means its been supported by most of the popular Operating System, you can extract the same archive Zip file in Windows, Mac’s or Linux machines without the need to install extra software – its a build-in tool
#-------------------------------------------------------------- # ZIP: #-------------------------------------------------------------- # To demonstrate how to use the ZIP command to archive and compress # one or more files, we will use two types of files for that, one is a # simple text file ['txt' type] and the second file is an image ['png' type] # The text file 'report.txt' size is 6232 Bytes (6.232KB) # The image file 'mypic.png' size is 126340 Bytes (126KB) bash> ls -ltr -rw-------@ 1 sfcompy staff 126340 Mar 23 13:43 pic1.png -rw-r--r--@ 1 sfcompy staff 6232 Jul 3 14:25 report.txt #------------------------------------------------------------ # To archive the text file we can use the following command format: # zip [-options] [-b path] [-t mmddyyyy] [-n suffixes] [zipfile list] [-xi list] # The simplified version for this example is: zip archivename.zip myfile # archivename.zip: is the name of the archive file # myfile: is the file you like to archive #------------------------------------------------------------ # To archive and compress 'report.txt' file: bash> zip myreport.zip report.txt adding: report.txt (deflated 58%) bash> ls -ltr -rw-r--r-- 1 sfcompy staff 2763 Jul 13 15:20 myreport.zip # The result archive file is a compressed file with a compression ratio # around 58%, the archive file size now is 2763 Bytes vs 6232 Bytes # before the compressions # To calculate the compression ratio in precent: 100 x (1 - 2763/6232))= 55.66% and as expected its around around 58% #----------------------------------------------------------- # now lets compress the image file bash> zip myimage.zip pic1.png adding: pic1.png (deflated 3%) bash> ls -ltr -rw-r--r-- 1 sfcompy staff 122977 Jul 13 15:50 myimage.zip # as expected the compression ration is not great as with the text # file (only 3%) from the reason that the 'png' file type is already # a version of a compressed image file #------------------------------------------------------------------ # To archive and compress more then one file we can use the following format # zip myarchive.zip file1 file1 ..fileN # now lets archive and compress both of the files bash> zip mytwofiles.zip report.txt pic1.png adding: report.txt (deflated 58%) adding: pic1.png (deflated 3%) # and the result is an archive file mytwofiles.zip that includes both of the files with size of 125718 Bytes as expected around 126KB: # (122977 + 2763 = 125740) bash> ls -ltr -rw-r--r-- 1 sfcompy staff 125718 Jul 13 15:53 mytwofiles.zip #---------------------------------------------------------------- # if we like to archive and compress folders and files , we need to # use the recursive switch '-r' at the Zip command # we can use the following format # zip -r myarchive.zip myfolder # to see all the Zip command switches and options used the switch '-h' # zip -h # To simplified this process its highly recommended to copy all the # files and folders that you like to archive to a new folder bash> mkdir myfolder bash> cp pic1.png report.txt myfolder bash>zip -r myarchive.zip myfolder/ bash> ls -ltr total 512 -rw-------@ 1 sfcompy staff 126340 Mar 23 13:43 pic1.png -rw-r--r--@ 1 sfcompy staff 6232 Jul 13 15:13 report.txt drwxr-xr-x 4 sfcompy staff 128 Jul 13 16:46 myfolder -rw-r--r-- 1 sfcompy staff 125858 Jul 13 16:48 myarchive.zip # as expected myarchive.zip files size is around 126KB #-------------------------------------------------------------- # UNZIP: #-------------------------------------------------------------- # To unzip a compressed and archive zip file the unzip command format # as the following: # unzip [-Z] [-opts[modifiers]] file[.zip] [list] [-x xlist] [-d exdir] # To simplified this example we will create an new folder and then copy # the archive zip file into the new folder, then we will extract the archive inside # First ,we can inspect the archive zip file by using the '-l' switch # then we will get a full report about the archive file and content bash> uzip -l myarchive.zip Archive: myarchive.zip Length Date Time Name --------- ---------- ----- ---- 0 07-13-2021 16:46 myfolder/ 126340 07-13-2021 16:46 myfolder/pic1.png 6232 07-13-2021 16:46 myfolder/report.txt --------- ------- 132572 3 files # we can see that it contains a folder 'myfolder' which contains the two # files that we archive at the example above #----------------------------------------------- # ALERT: keep in mind if you extract the archive file to a folder that # contains the same files and folders names as in the archive, # !!! it may OVER WRITE the file and the folders WITHOUT prompting!!!! #------------------------------------- # Thats why in this example we will extract the archive file # in a new temporary folder bash> mkdir tmp bash> cp myarchive.zip tmp bash> cd tmp bash> unzip myarchive.zip Archive: myarchive.zip creating: myfolder/ inflating: myfolder/pic1.png inflating: myfolder/report.txt bash> ls -ltr myfolder/ total 264 -rw-r--r-- 1 sfcompy staff 6232 Jul 13 16:46 report.txt -rw------- 1 sfcompy staff 126340 Jul 13 16:46 pic1.png # as expected the the unzip command created the folder myfolder which # includes both of the files # To extract the myarchive.zip to a specific folder (tmp2) we can use the # switch '-d' bash> unzip -d tmp2 myarchive.zip bash> ls -ltr total 248 drwxr-xr-x 4 sfcompy staff 128 Jul 13 16:58 myfolder -rw-r--r-- 1 sfcompy staff 125900 Jul 13 17:22 myarchive.zip drwxr-xr-x 3 sfcompy staff 96 Jul 13 17:37 tmp2 bash> ls -ltr tmp2 total 0 drwxr-xr-x 4 sfcompy staff 128 Jul 13 16:58 myfolder # as we can see the tmp2 folder contains the extracted folder bash> ls -ltr tmp2/myfolder/ total 264 -rw-r--r-- 1 sfcompy staff 6232 Jul 13 16:46 report.txt -rw------- 1 sfcompy staff 126340 Jul 13 16:46 pic1.png # -------------- DONE!