Creating 7zip files on Linux with LZMA

So today I was trying to create an archive that someone had specifically requested be in 7zip format from my Linux laptop and I ran in to a few annoying “features” of the very basic p7Zip application availible in most Debian/Ubuntu based systems.

p7Zip essentially only has two command line options, compress or decompress and nothing you can tell it will stop it from deleting the original source file once it’s finished compressing. This is worrying.

Another annoying feature is it’s choice to have no option to choose an output filename. Infact even the utility I’m going to introduce in a second doesn’t have this option either but it’s at least trivial to work around.

If you didn’t already know, 7zip is actually just an implementation of LZMA and LZMA2 compression with the file extention ‘.7z.’

Given this we can use the command lzma (sudo apt-get install lzma.)

Here is the command issued to create a 7zip a file with lzma and then a test decompress with p7zip to prove it works.

(~ $ lzma -kvzc file.tst > file.7z)

Now an explanation of the command one option at a time.

-k, this forces lzma to keep the original file and not delete it once the archive is complete.

-v, verbose output so we can see what’s going on.

-z, this is the option that tells lzma that we want to create an archive.

And finally the most interesting bit;

-c, this option forces lzma to output the file not to a file but to the stdout, this can then be redirected to a file of our choosing using the “> file.7z”. Replacing the filename with whatever you want.