OnWorks Linux and Windows Online WorkStations

Logo

Free Hosting Online for WorkStations

< Previous | Contents | Next >

zip

The zip program is both a compression tool and an archiver. The file format used by the program is familiar to Windows users, as it reads and writes .zip files. In Linux, how- ever, gzip is the predominant compression program with bzip2 being a close second.

In its most basic usage, zip is invoked like this:

zip options zipfile file...

For example, to make a zip archive of our playground, we would do this:



[me@linuxbox ~]$ zip -r playground.zip playground

[me@linuxbox ~]$ zip -r playground.zip playground


Unless we include the -r option for recursion, only the playground directory (but none of its contents) is stored. Although the addition of the extension .zip is automatic, we will include the file extension for clarity.

During the creation of the zip archive, zip will normally display a series of messages like this:



adding: playground/dir-020/file-Z (stored 0%) adding: playground/dir-020/file-Y (stored 0%) adding: playground/dir-020/file-X (stored 0%) adding: playground/dir-087/ (stored 0%) adding: playground/dir-087/file-S (stored 0%)

adding: playground/dir-020/file-Z (stored 0%) adding: playground/dir-020/file-Y (stored 0%) adding: playground/dir-020/file-X (stored 0%) adding: playground/dir-087/ (stored 0%) adding: playground/dir-087/file-S (stored 0%)


These messages show the status of each file added to the archive. zip will add files to the archive using one of two storage methods: Either it will “store” a file without com- pression, as shown here, or it will “deflate” the file which performs compression. The nu- meric value displayed after the storage method indicates the amount of compression achieved. Since our playground only contains empty files, no compression is performed on its contents.

Extracting the contents of a zip file is straightforward when using the unzip program:


[me@linuxbox ~]$ cd foo

[me@linuxbox foo]$ unzip ../playground.zip

[me@linuxbox ~]$ cd foo

[me@linuxbox foo]$ unzip ../playground.zip


One thing to note about zip (as opposed to tar) is that if an existing archive is speci- fied, it is updated rather than replaced. This means that the existing archive is preserved, but new files are added and matching files are replaced.

Files may be listed and extracted selectively from a zip archive by specifying them to

../playground.zip

../playground.zip

Date

----

Date

----

Time

----

Time

----

10-05-16 09:25

10-05-16 09:25

unzip:



[me@linuxbox ~]$ unzip -l playground.zip playground/dir-087/file-Z

[me@linuxbox ~]$ unzip -l playground.zip playground/dir-087/file-Z


Archive:

Length

--------

0

--------

0

Name

----

playground/dir-087/file-Z

-------

1 file

Archive:

Length

--------

0

--------

0


[me@linuxbox ~]$ cd foo

[me@linuxbox foo]$ unzip ../playground.zip playground/dir-087/file-Z

Archive: ../playground.zip

replace playground/dir-087/file-Z? [y]es, [n]o, [A]ll, [N]one, [r]ename: y

extracting: playground/dir-087/file-Z

[me@linuxbox ~]$ cd foo

[me@linuxbox foo]$ unzip ../playground.zip playground/dir-087/file-Z

Archive: ../playground.zip

replace playground/dir-087/file-Z? [y]es, [n]o, [A]ll, [N]one, [r]ename: y

extracting: playground/dir-087/file-Z


image

Using the -l option causes unzip to merely list the contents of the archive without ex- tracting the file. If no file(s) are specified, unzip will list all files in the archive. The -v option can be added to increase the verbosity of the listing. Note that when the archive extraction conflicts with an existing file, the user is prompted before the file is replaced.

Like tar, zip can make use of standard input and output, though its implementation is somewhat less useful. It is possible to pipe a list of filenames to zip via the -@ option:


[me@linuxbox foo]$ cd

[me@linuxbox ~]$ find playground -name "file-A" | zip -@ file-A.zip

[me@linuxbox foo]$ cd

[me@linuxbox ~]$ find playground -name "file-A" | zip -@ file-A.zip


Here we use find to generate a list of files matching the test -name "file-A", and then pipe the list into zip, which creates the archive file-A.zip containing the se- lected files.

zip also supports writing its output to standard output, but its use is limited because very few programs can make use of the output. Unfortunately, the unzip program does not accept standard input. This prevents zip and unzip from being used together to per-


form network file copying like tar.

zip can, however, accept standard input, so it can be used to compress the output of other programs:



[me@linuxbox ~]$ ls -l /etc/ | zip ls-etc.zip -

adding: - (deflated 80%)

[me@linuxbox ~]$ ls -l /etc/ | zip ls-etc.zip -

adding: - (deflated 80%)


In this example we pipe the output of ls into zip. Like tar, zip interprets the trailing dash as “use standard input for the input file.”

The unzip program allows its output to be sent to standard output when the -p (for pipe) option is specified:



[me@linuxbox ~]$ unzip -p ls-etc.zip | less

[me@linuxbox ~]$ unzip -p ls-etc.zip | less


We touched on some of the basic things that zip/unzip can do. They both have a lot of options that add to their flexibility, though some are platform specific to other systems. The man pages for both zip and unzip are pretty good and contain useful examples. However, the main use of these programs is for exchanging files with Windows systems, rather than performing compression and archiving on Linux, where tar and gzip are greatly preferred.


Top OS Cloud Computing at OnWorks: