Ubuntu – Convert from one archive type to another using CLI

14.04archivecommand linefile-roller

If a particular archive type has to be converted into another format (eg – tar.gz to zip), then one can open the archive using file-roller and go to –

Archive -> Save As -> (select the extension) -> Save

Also, in this method, Other Options can be used to set a password for the zip file, which is not possible in case of tar.gz files using file-roller.

How can the above steps be performed using the command line?

Best Answer

Basic Shell commands:

$ cd $HOME
$ mkdir tempdir
$ cd tempdir
$ tar -zxvf ../archive.tar.gz 

At this point you have a copy of the contents of archive.tar.gz in $HOME/tempdir/

$ zip -rmp password ../archive.zip *

... will create a zip archive from the contents of tempdir/ and then remove the files added. I presume it still does; use standard (weak) PKZip 2.0 encryption as stated for the -e option - which doesn't take the password, but prompts for it instead.

Make SURE / VERIFY you're still in tempdir/

$ pwd
.../tempdir

If there is ANYTHING else than "/tempdir" at the end above,
then DO NOT continue with what comes next, here:

$ rm -rf *  
$ cd ..
$ rmdir tempdir

All the above might be possible with a pipe too, as in:

$ tar -zxvf ./archive.tar.gz - | zip -p password - ./archive.zip 

... I see no reason to try it out though - due to the weak encryption and possible problems with how zip handles special files, links and whatnot.

If you want real encryption, look into gnupg and related utilities instead.

man tar, man zip, zip --help, zip -h2 | less, tar --help | less may hold information vital to the above, especially the piped conversion which I have not tried.

Related Question