Linux下压缩加密存档文件

先安装openssl

系统一般自带,没安装的可以安装一下

$ sudo apt install -y openssl
$ openssl version
OpenSSL 1.1.1k  31 Mar 2021

先把文件打包压缩一下

这里我推荐压缩比较高的 bzip2

$ tar -jcf data.tar.bz2 ./data/*

打包压缩一下 当前目录下 data 文件夹下所有文件

加密

openssl aes-256-cbc -a -salt -iter 5 -in data.tar.bz2 -out data.tar.bz2.enc

此处为对称加密。系统将提示您输入两次加密密码,可以是任何您想要的密码。此命令将生成一个新的 data.tar.bz2.enc 文件作为已加密的文件。请妥善保管您输入的密码。一旦丢失,该文件则无解了。

解密

openssl aes-256-cbc -d -a -iter 5 -in data.tar.bz2.enc -out data.tar.bz2

上述命令将提示您输入加密密码,然后生成一个 data.tar.bz2 的已解密文件。此时,您只需解压缩就可以使用其中文件了。

解压

$ tar jxf data.tar.bz2

会将该压缩包解压到当前目录

多线程压缩

xz

$ tar -cf - ./data/* | xz -k --threads=16 --block-size=8MiB -9 -c > data.tar.xz
$ openssl aes-256-cbc -salt -pbkdf2 -iter 100000  -in data.tar.xz -out data.tar.xz.pbkdf2  # 加密
$ openssl aes-256-cbc -d -salt -pbkdf2 -iter 100000 -in data.tar.xz.pbkdf2 -out data.tar.xz # 解密

–block-size=size

When compressing to the .xz format, split the input data into blocks of sizebytes. The blocks are compressed independently from each other, which helps with multi-threading and makes limited random-access decompression possible. This option is typically used to override the default block size in multi-threaded mode, but this option can be used in single-threaded mode too.

In multi-threaded mode about three times size bytes will be allocated in each thread for buffering input and output. The default size is three times the LZMA2 dictionary size or 1 MiB, whichever is more. Typically a good value is 2-4 times the size of the LZMA2 dictionary or at least 1 MiB. Usingsize less than the LZMA2 dictionary size is waste of RAM because then the LZMA2 dictionary buffer will never get fully used. The sizes of the blocks are stored in the block headers, which a future version of xz will use for multi-threaded decompression.

In single-threaded mode no block splitting is done by default. Setting this option doesn’t affect memory usage. No size information is stored in block headers, thus files created in single-threaded mode won’t be identical to files created in multi-threaded mode. The lack of size information also means that a future version of xz won’t be able decompress the files in multi-threaded mode.

注意,以上操作相当消耗内存。分割的块大小可以自己设置,推荐大小为字典的2-3倍,块设置的越大,内存占用也会越大,线程数越多,内存占用也会越大。所以如果压缩时报错内存不足的话,就把线程数和块都改小即可。xz这个软件还是更推荐在linux上使用,因为不容易碰到内存的问题。

pigz

安装

$ sudo apt install pigz

语法

pigz [ -cdfhikKlLmMnNqrRtz0..9,11 ] [ -b blocksize ] [ -p threads ] [ -S suffix ] [ name ...  ]
unpigz [ -cfhikKlLmMnNqrRtz ] [ -b blocksize ] [ -p threads ] [ -S suffix ] [ name ...  ]

压缩单个文件

$ pigz -k your_file_name

加上 -k 选项保留原始文件,会在当前工作目录获得压缩后的 your_file_name.gz 文件

这时候还可以用

$ pigz -l your_file_name.gz

来查看压缩文件后的压缩率

解压缩

$ unpigz -d your_file_name.gz

同样,如果需要保留.gz文件,记得加上 -k 选项

压缩文件夹

pigz没有压缩文件夹的选项,只能压缩单个文件。若想压缩文件夹,可以结合tar使用

$ tar -cvf - dir1 dir2 dir3 | pigz > output.tar.gz

解压文件夹

$ pigz -d output.tar.gz

这样得到的是output.tar,再通过tar解打包就好了

也可以直接用tar命令解压

tar -xzvf output.tar.gz

常用参数

  • -0 ~ -9 压缩等级,数字越大压缩率越高,速度越慢,默认为6
  • -k –keep 压缩后不删除原始文件
  • -l –list 列出压缩输入的内容
  • -K –zip Compress to PKWare zip (.zip) single entry format
  • -d –decompress 解压缩输入
  • -p –processes n 使用n核处理,默认为使用所有CPU核心