Using unzip

From Haiku Max Wiki

Zip files are a very common format of file compression found on the internet. Double clicking the .zip file in the tracker will open the 'Expander' and the zip file contents will be extracted effortlessly. But sometimes we want to do more specific things than just extracting the whole contents of the zip file in the current directory.

Table of contents

Unzip Syntax

# unzip -h
UnZip 5.40 of 28 November 1998, by Info-ZIP.  Maintained by C. Spieler.  Send
bug reports to the authors at Zip-Bugs@lists.wku.edu; see README for details.

Usage: unzip [-Z] [-opts[modifiers]] file[.zip] [list] [-x xlist] [-d exdir]
  Default action is to extract files in list, except those in xlist, to exdir;
  file[.zip] may be a wildcard.  -Z => ZipInfo mode ("unzip -Z" for usage).

  -p  extract files to pipe, no messages     -l  list files (short format)
  -f  freshen existing files, create none    -t  test compressed archive data
  -u  update files, create if necessary      -z  display archive comment
  -x  exclude files that follow (in xlist)   -d  extract files into exdir

modifiers:                                   -q  quiet mode (-qq => quieter)
  -n  never overwrite existing files         -a  auto-convert any text files
  -o  overwrite files WITHOUT prompting      -aa treat ALL files as text
  -j  junk paths (do not make directories)   -v  be verbose/print version info
  -C  match filenames case-insensitively     -L  make (some) names lowercase
  -X  restore UID/GID info                   -V  retain VMS version numbers
                                             -M  pipe through "more" pager
Examples (see unzip.doc for more info):
  unzip data1 -x joe   => extract all files except joe from zipfile data1.zip
  unzip -p foo | more  => send contents of foo.zip via pipe into program more
  unzip -fo foo ReadMe => quietly replace existing ReadMe if archive file newer

Do not get confused by the brackets, all that means is to seperate the different options and they are not actually typed in when you go to do something. Follow along with all the examples and the unzip utility can be learned very easily!

Examples

For all examples used is this file: http://chiptune.de/complete/cta-modules.zip . To download open a terminal from any folder that you want to save this file too. Paste this

wget -c http://chiptune.de/complete/cta-modules.zip

and the zip file will be downloaded and is 73MB .

Listing the contents of the zip file

Within the same terminal as the above, to list the contents of the zip file do this:

unzip -l cta-modules.zip 

The preceding will show you the Length, Date, Time and Name of all files contaned in the archive.
However, sometimes that is not good enough and we want more details.

unzip -lv cta-modules.zip 

The above will show you the Length (in bytes uncompressed), Method (of packing into the zip file), Size (in bytes compressed), Ratio (of compression), Date, Time, CRC-32 (http://en.wikipedia.org/wiki/Cyclic_redundancy_check#Variations), and Name. And at the bottom of the list the output shows us the total uncompressed size in bytes, total compressed size in bytes, average compression ratio, and finally the total number of files contained in the zip file.

Note: if the Name is in a format of folder/file.extension then when the zipfile is extracted a folder is made and it will usually contain all the contents of the zipfile. This is what we want or else we will get spammed with a bunch of unwanted files in a directory with other things in it that we usually navigate through. Unfortunately for us the example zipfile contains multiple other zipfiles but with no master folder to store all of them in. The Name instead is in a format of file.extension only, and a solution to this problem is included below.

Extracting contents of the zip file

Extracting in the current directory

Simply extracting the full contents, in the current directory

unzip cta-modules.zip

The above will spam our current directory with a bunch of txt files and zip files. This is not what we want. So if you aready did that, delete all the files extracted. Create a new directory either from the terminal

mkdir cta-modules

or by Right-Clicxing in the tracker and choosing New>New folder.

Extracting in an external directory

Now that a directory is made, we can extract the zip file contents where we want.

unzip cta-modules.zip -d cta-modules

The -d option listed after the zip file tells the utility to extract the files to a directory we want. For example if we wanted to extract to /boot/apps it would be:

unzip cta-modules.zip -d /boot/apps

Excluding files to be extracted

The example zip file contains a useless .txt file for each archive extracted. Since we don't care about the txt files, we can do this to not extract them:

unzip cta-modules.zip -d cta-modules -x *.txt

The above will also save time it takes to completely extract the contents. The -x means to exclude certain files. The * is called globbing (http://en.wikipedia.org/wiki/Glob%28%29). Unzip will exclude all file names that end in .txt . We can also include more files to extract by just putting a space after each one. To get the name of the file to extract you would have to list the contents of the zip file first and then copy/paste them.

Extracting multiple zip files at once

How tedius it is to select all and hit enter on the zipfiles - many expander windows come up at once and all those files are being unzipped simultaneously creating massive in-efficiency instead of being unzipped window-spam free from within the terminal. All the archives being extracted at once would then create a seperate unzip process for each Expander window open. Much memory would get used up and all the cpu would got used up as well.
We can use globbing to extract all zipfiles at once but with unzip a special exception arises: a back slash has to be used before the star in order for it to work.
For example now that we have extracted all the .zip files contained in the cta-modules.zip archive now in the cta-modules folder, this is how we would do it:

unzip cta-modules/\*.zip

For example to unzip all the files in the current directory it would be

unzip \*.zip

Clean up!

Now we're stuck with a number of unwanted .zip files in our cta-modules folder. From within the tracker it could be not so difficult to delete all of them by clicking on the 'Kind' sorting button above all the files, selecting them and deleting them. However it can be completed faster and easier sice we are all ready using a terminal window. We can use the globbing technique combined with the utility rm which means 'remove' .

rm -v cta-modules/*.zip

The -v option means verbose and will show us each file removed as it gets removed.

Now to delete the main cta-modules.zip

rm cta-modules.zip

And there you have it, a tutorial on unzip. Now you can go listen to all the modules you could ever want!