Home 压缩解压缩-lz4
Post
Cancel

压缩解压缩-lz4

有遇到.tar.lz4 的压缩文件,记录一下 关于lz4的压缩格式。

lz4介绍

LZ4是一种无损压缩算法,主要特点就是压缩和解压的速度非常快,能有效利用CPU多核资源。速度快是其主要特点,所以非常适合需要频繁压缩或解压的场合。其不足主要 是压缩率不高,不如gnuzip。另外它只能对单个文件压缩,无法对目录压缩,所以对目录压缩时往往需要配合tar工具,由tar工具打包后,进行lz4压缩。解压则反之。

工具安装

一般的主流发行版都默认安装,在debian系上手动安装lz4工具,包含压缩和解压。

1
2
3
4
5
6
sudo apt install lz4


# 查看帮助
lz4 --help
man lz4

基本用法:

1
2
3
4
5
6
Usage : 
      lz4 [arg] [input] [output] 

input   : a filename 
          with no FILE, or when FILE is - or stdin, read standard input

一些常见参数选项,它支持递归,但是是目录下逐个个文件的压缩,不是对整个目录:

ArgumentsDescription
-1Fast compression (default)
-9High compression
-ddecompression (default for .lz4 extension)
-zforce compression
-foverwrite output without prompting(出现同名文件时会提示是否覆盖,-f就强制覆盖不提示)
-kpreserve source files(s) (default)
–rmremove source file(s) after successful de/compression
-h/-Hdisplay help/long help and exit
-Vdisplay Version number and exit
-vverbose mode
-qsuppress warnings; specify twice to suppress errors too
-cforce write to standard output, even if it is the console
-ttest compressed file integrity
-mmultiple input files (implies automatic output filenames)
-roperate recursively on directories (sets also -m)
-lcompress using Legacy format (Linux kernel compression)

一些使用示例

单个文件压缩和解压

这里使用默认方式,保留原文件,不删除。

随便找一个普通文件 Max_BW.fio,进行压缩。

1
2
3
4
5
6
7
$ lz4 ./Max_BW.fio 
Compressed filename will be : ./Max_BW.fio.lz4 
Compressed 306 bytes into 267 bytes ==> 87.25%   

$ ls -l ./Max_BW.fio*
-rw-r--r-- 1 user user 306 4月  21 15:33 ./Max_BW.fio
-rw-r--r-- 1 user user 267 4月  21 15:33 ./Max_BW.fio.lz4

进行解压缩

1
2
3
4
5
6
7
8
$ rm ./Max_BW.fio
$ lz4 -d ./Max_BW.fio.lz4 
Decoding file ./Max_BW.fio 
./Max_BW.fio.lz4     : decoded 306 bytes

$ ls -l ./Max_BW*
-rw-r--r-- 1 user user 306 4月  21 15:33 ./Max_BW.fio
-rw-r--r-- 1 user user 267 4月  21 15:33 ./Max_BW.fio.lz4

对多个文件压缩

使用-m参数,这个或许不常用。

1
2
3
4
5
6
7
$ lz4 -fm ./Max_BW.fio ./Max_IOPS.fio

$ ls -l Max_*
-rw-r--r-- 1 user user 306 4月  21 15:33 Max_BW.fio
-rw-r--r-- 1 user user 267 4月  21 15:33 Max_BW.fio.lz4
-rw-r--r-- 1 user user 304 4月  21 16:25 Max_IOPS.fio
-rw-r--r-- 1 user user 265 4月  21 16:25 Max_IOPS.fio.lz4

从标准输入压缩或解压内容

从stdin获取输入内容,压缩一个字符串”my hellowd mesg”到hello.lz4,并解压到mesg.txt。

1
2
3
4
5
6
7
8
9
10
11
12
$ echo "my hellowd mesg" | lz4 - hello.lz4
Compressed 16 bytes into 35 bytes ==> 218.75% 

$ ls hello.lz4 
hello.lz4


$ cat ./hello.lz4 | lz4 -d - ./mesg.txt
stdin                : decoded 16 bytes    

$ cat ./mesg.txt 
my hellowd mesg

配合tar压缩解压一个目录

可以先用tar打包,再用lz4进行压缩,两步。也可以通过管道一步完成。

示例:有文件夹 bitbake-master

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
## 压缩部分
$ ls -ld ./bitbake-master
drwxrwxr-x 8 user user 4096 12月 21 22:14 ./bitbake-master
$ du -sh ./bitbake-master
12M	./bitbake-master


$ tar cvf - ./bitbake-master | lz4 - bitbake-master.tar.lz4
./bitbake-master/
./bitbake-master/conf/
./bitbake-master/conf/bitbake.conf
./bitbake-master/.gitignore
./bitbake-master/toaster-requirements.txt
./bitbake-master/contrib/
...
...
Compressed 10045440 bytes into 3820425 bytes ==> 38.03% 

$ ls -hl ./bitbake-master.tar.lz4 
-rw-rw-r-- 1 user user 3.7M 5月   6 14:41 ./bitbake-master.tar.lz4



## 解压缩部分

$ mv ./bitbake-master ./bkp_bitbake-master

$ lz4 -dc ./bitbake-master.tar.lz4 | tar xvf -
./bitbake-master/
./bitbake-master/conf/
./bitbake-master/conf/bitbake.conf
./bitbake-master/.gitignore
./bitbake-master/toaster-requirements.txt
./bitbake-master/contrib/
...
...


$ ls -ld ./bitbake-master
drwxrwxr-x 8 user user 4096 12月 21 22:14 ./bitbake-master
$ du -sh ./bitbake-master
12M	./bitbake-master

压缩解压缩后删除源文件

可以带上 --rm 参数。

This post is licensed under CC BY 4.0 by the author.