Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

How can I create and extract zip archives from the command line?

share|improve this question

migrated from stackoverflow.com Jan 28 '11 at 16:37

This question came from our site for professional and enthusiast programmers.

3  
@joachim, you should ask the question in a question and provide an answer in an answer. There's no problem with answering your own questions but not as part of the question itself, since that breaks down the whole community-votes-on-the-best-answer advantage of SO. In addition, this is probably better suited for superuser since it's not programming related. –  paxdiablo Jan 28 '11 at 16:02
2  
Where will you document how to get online and check SO if you forget your login? –  Doug Chamberlain Jan 28 '11 at 16:05
    
@rlb.usa: I work in a lot of different places on a lot of different servers. Using SA for a note like this helps other people too, as I see it. –  joachim Jan 28 '11 at 16:51

7 Answers 7

If you don't have zip and unzip packages installed and you have java, you can use jar to unzip:

jar -xf file.zip
share|improve this answer

Well, when it comes to distributing files for a variety of operating systems, I'd recommend 7-zip.

Usually in the package p7zip, you'll get the 7z and 7za command, with which you can create your own 7z archives.

7za can also decompress standard (pkzip) zip archives (and create them as well with the -tzip switch).

Compressing:

7za a archive.7z file1 file2 directory/

Decompressing:

7za x archive.7z

It can also create self-extracting archives with the -sfx switch:

7za a -sfx archive.exe files1 file2 dir

I recommend this method in case Windows users can't open 7z archives (in case you want to advice a tool for that: PeaZip).

If you want to use the same compression algorithm with your tarballs, use the -J switch with tar:

tar cJf archive.tar.xz file1 file2 dir

xz is a UNIX tool, that uses LZMA2 for compression, but works the way gz, bz2, etc works. It even works as a filter.

7z doesn't create archives with full filesystem information on UNIX, so you'd need to use tar before using 7z (but since 7z stores other information about the tar file, I'd recommend using xz, as it is designed for it):

tar cf - file1 file2 dir | 7za z -si archive.tar.7z
share|improve this answer
    
+1 for 7z and 7za –  Stephane Rolland Mar 12 '13 at 21:51
1  
I should perhaps append this answer with atool, it's a nice little tool that makes using many different compressors more comfortable. Bind that to a and creating an archive becomes a arc.7z folder/ or extracting a simple a arc.7z –  polemon May 2 at 23:17
unzip <filename>

and

zip -r archive.zip <filename> [filename]

You'll need to make sure these commands are installed via your package manager. It's no harder than using anything else on the command line. It's certainly simpler than creating archives with tar.

share|improve this answer

The most standard answer is pax, which is recommended over cpio and tar.

Unlike cpio and Posix tar, but like GNU tar, pax is able to both archive files and compress the archive. This behaviour is different from zip, which compresses each file before putting it in the archive.

share|improve this answer

Typically one uses tar to create an uncompressed archive and either gzip or bzip2 to compress that archive. The corresponding gunzip and bunzip2 commands can be used to uncompress said archive, or you can just use flags on the tar command to perform the uncompression.

If you are referring specifically to the zip file format, you can simply use the zip and unzip commands.

To compress:

zip squash.zip file1 file2 file3

To uncompress:

unzip squash.zip

share|improve this answer
    
I personally only ever use the zip -r9 archive.zip files... syntax (-r=recursive -9=max compression). –  amphetamachine Jan 31 '11 at 12:29
    
Just as a side note: zip can create many different flavors of .ZIP archives, as well as use many different compressors. On the other hand, there are a number of other compression tools named similarly: 7-Zip, gzip, bzip2, rzip, etc. which are not interchangeable. –  polemon May 4 at 14:34

here (for anyone wondering) are the meaning of the flags in his commands

c Create a new archive.
t List the contents of an archive.
x Extract the contents of an archive.
f The archive file name is given on the command line (required whenever the tar output is going to a file)
M The archive can span multiple floppies.
v Print verbose output (list file names as they are processed).
u Add files to the archive if they are newer than the copy in the tar file.
z Compress or decompress files automatically.

source: Tar - Linux Commands

share|improve this answer
    
Sorry, I only have finite brain space and I'm absolutely not going to waste it on cryptic stuff like that. Hence why I wanted to post this so I can find it again -- and other people too I hope. –  joachim Jan 28 '11 at 16:51
3  
What's wrong with using the man page? –  pdo Jan 28 '11 at 18:55
    
What's wrong with the man page? You want to go read it and then come back and tell me? It's completely obscure and impenetrable, that's why. –  joachim Feb 1 '11 at 11:30

You can zip files up with:

tar -zcvf myfile.tgz .

which will do the current directory. Replace . with other file names if you want something else.

To unzip that file, use:

tar -zxvf myfile.tgz

That's assuming of course that you have a tar capable of doing the compression as well as combining of files into one.

If not, you can just use tar cvf followed by gzip (again, if avaialable) for compression and gunzip followed by tar xvf.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.