1

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:
E_INVALIDARG

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?

1
  • 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
    Sep 24, 2021 at 15:09

1 Answer 1

2

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
2
  • I see you are an Embedded Systems developer. Me too. Not an experienced one. Is there any stackexchange for us? Sep 22, 2021 at 17:17
  • Hi Gilles, Could you please give the command to create .rar file using 7z too please? Sep 22, 2021 at 18:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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