The man page says 7z tool supports 7z, LZMA2, XZ, ZIP, Zip64, CAB, RAR, ARJ, GZIP, BZIP2, TAR, CPIO, RPM, ISO, most filesystem images and DEB formats.

And it says we could pass the type as -t{Type}. The following command is success.

7z a -tzip archive.zip dir1 dir2 file1 file2

But I can't archive the files into .xz using the following command.

7z a -txz archive.zip dir1 dir2 file1 file2
$ 7z a -txz archive.xz dir1 dir2 file1 file2

7-Zip [64] 16.02 : Copyright (c) 1999-2016 Igor Pavlov : 2016-05-21
p7zip Version 16.02 (locale=en_GB.UTF-8,Utf16=on,HugeFiles=on,64 bits,4 CPUs Intel(R) Core(TM) i5-7200U CPU @ 2.50GHz (806E9),ASM,AES-NI)

Scanning the drive:
2 folders, 7 files, 196192711 bytes (188 MiB)

Creating archive: archive.xz

Items to compress: 9

System ERROR:

Anything I am doing wrong here? How to create .xz file using 7z command?

EDIT: The following command successful.

7z a -txz archive archive.tar

Does it mean we can use -txz or -tbzip2 on only archive files as we can't directly use compression on files without archiving the files first?

  • Welcome to the site, and congratulations on finding the solution youself. However, please don't edit your question to contain the answer - write your own answer instead.
    – AdminBee
    Commented Sep 24, 2021 at 15:09

1 Answer 1


Like gzip (.gz) and bzip2 (.bz2), but unlike zip and rar, xz is a compression format, not an archive format. You can compress one file:

7z a -txz foo.xz foo

This is equivalent to xz foo.

Typically, to make a compressed archive, you make a tar archive and compress it. You can make a compressed archive with 7z, but as far as I can tell, you can't do it in a single step: you have to create the tar first, then compress it. (7z does know how to read from a compressed archive directly.) You can do that in a pipeline.

set -o pipefail
7z a -ttar -so archive.tar.xz dir1 dir2 file1 file2 | 7z a -txz -si archive.tar.xz

As far as I know, archive.tar.xz in the left-hand command is ignored (-so tells 7z to write to standard output), but it's still necessary.

With a pipeline, beware that in sh, if the tar creation fails, for example because a file can't be read, then the pipeline command won't fail. Ksh, bash and zsh have an option pipefail to make the whole command fail.

It's a lot simpler to call tar directly. Most modern tar implementations can create gz, bz2 and xz compressed archives.

tar -cJf archive.tar.xz dir1 dir2 file1 file2
  • I see you are an Embedded Systems developer. Me too. Not an experienced one. Is there any stackexchange for us?
    – NayabSD
    Commented Sep 22, 2021 at 17:17
  • Hi Gilles, Could you please give the command to create .rar file using 7z too please?
    – NayabSD
    Commented Sep 22, 2021 at 18:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .