Linux Zstandard 无损实时数据压缩

Linux Zstandard 无损实时数据压缩

良好组织的数据压缩是有效数据管理的必要组成部分。它使我们能够节省磁盘空间,减少数据传输时间,并提高整体数据效率。在众多越来越受欢迎的数据压缩工具中,-zstd以高性能选项脱颖而出。由Yann Collet开发,zstd是一款开源软件,提供令人印象深刻的压缩比,同时保持快速解压速度。

Zstd是一种适用于压缩各种类型数据(如文本、图像、音频和视频等)的灵活可适应的数据压缩工具。此外,它兼容多个平台,包括Linux、Windows、MacOS和FreeBSD,使其成为不同用户可靠且易用的工具。此外,zstd的灵活性还体现在用户友好的命令行界面上,它提供了多个选项和功能。

在本文中,我们将详细解释zstd的运作方式以及如何安装它。此外,我们将提供使用zstd压缩和解压文件的实际示例,并展示它的一些高级功能,如多线程、字典学习和压缩级别等。通过本文,您将全面了解zstd以及如何利用它满足您的数据压缩需求。

在Linux上安装Zstandard

Zstandard在大多数Linux发行版的官方软件仓库中都有提供,因此可以通过软件包管理器轻松安装。以下是在Ubuntu上安装Zstandard的几个命令:

输入以下命令以安装Zstandard

sudo apt-get update
sudo apt-get install zstd

Zstandard 的更新和安装将提供所有详细信息

user@ubuntu:~sudo apt-get update
[sudo] password for user: 
Get:1 http://security.ubuntu.com/ubuntu focal-security InRelease [114 kB]
Hit:2 http://us.archive.ubuntu.com/ubuntu focal InRelease                
Get:3 http://us.archive.ubuntu.com/ubuntu focal-updates InRelease [114 kB]             
Get:4 http://us.archive.ubuntu.com/ubuntu focal-backports InRelease [101 kB]             
Get:5 http://us.archive.ubuntu.com/ubuntu focal-updates/main amd64 Packages [1,341 kB]
Get:6 http://security.ubuntu.com/ubuntu focal-security/main amd64 Packages [784 kB]
Get:7 http://security.ubuntu.com/ubuntu focal-security/main Translation-en [236 kB]
Get:8 http://security.ubuntu.com/ubuntu focal-security/restricted amd64 Packages [44.7 kB]
Get:9 http://security.ubuntu.com/ubuntu focal-security/restricted Translation-en [11.6 kB]
Get:10 http://security.ubuntu.com/ubuntu focal-security/universe amd64 Packages [959 kB]
Get:11 http://security.ubuntu.com/ubuntu focal-security/universe Translation-en [172 kB]
Get:12 http://us.archive.ubuntu.com/ubuntu focal-updates/main Translation-en [341 kB]
Get:13 http://us.archive.ubuntu.com/ubuntu focal-updates/restricted amd64 Packages [70.1 kB]
Get:14 http://us.archive.ubuntu.com/ubuntu focal-updates/restricted Translation-en [12.9 kB]
Get:15 http://us.archive.ubuntu.com/ubuntu focal-updates/universe amd64 Packages [900 kB]
Get:16 http://us.archive.ubuntu.com/ubuntu focal-updates/universe Translation-en [197 kB]
Get:17 http://us.archive.ubuntu.com/ubuntu focal-backports/main amd64 Packages [8,680 B]
Get:18 http://us.archive.ubuntu.com/ubuntu focal-backports/main Translation-en [4,156 B]
Fetched 5,297 kB in 5s (1,163 kB/s)                          
Reading package lists... Done
Building dependency tree       
Reading state information... Done
All packages are up to date.
user@ubuntu:~ sudo apt-get install zstd
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following NEW packages will be installed:
  zstd
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 249 kB of archives.
After this operation, 1,120 kB of additional disk space will be used.
Get:1 http://us.archive.ubuntu.com/ubuntu focal/universe amd64 zstd amd64 1.4.5+dfsg-4 [249 kB]
Fetched 249 kB in 1s (177 kB/s)    
Selecting previously unselected package zstd.
(Reading database ... 233077 files and directories currently installed.)
Preparing to unpack .../archives/zstd_1.4.5+dfsg-4_amd64.deb ...
Unpacking zstd (1.4.5+dfsg-4) ...
Setting up zstd (1.4.5+dfsg-4) ...
Processing triggers for man-db (2.9.1-1) ...
user@ubuntu:~$

成功安装Zstandard后,我们现在可以学习一些基本的Zstd命令用法示例。

zstd example.txt

zstd命令的输出

user@ubuntu:~zstd example.txt
user@ubuntu:~

2. 解压缩文件

当您想解压缩一个使用Zstd压缩的文件时,使用命令“zstd -d”后跟压缩文件的名称。例如,“zstd -d example.txt.zst”将恢复压缩文件到其原始格式。Zstd的高压缩比和快速解压速度使其成为在Linux上处理大文件的可靠和高效的方式。

解压缩文件的命令

zstd -d example.txt.zst

3.压缩一个目录

使用Zstd命令压缩整个目录非常简单。我们只需要使用命令“tar cf – 目录名称/ | zstd > 压缩文件名称.tar.zst”。例如,假设我们有一个名为“example”的目录,我们想要压缩并创建一个名为“example.tar.zst”的压缩文件。我们需要在终端中运行这个命令“tar cf – example/ | zstd > example.tar.zst”。这将把整个目录压缩为一个文件,以便您可以使用这个命令。

压缩目录的输入命令

tar cf - example/ | zstd > example.tar.zst

4. 解压缩目录

在Linux中解压缩一个使用Zstd压缩的目录,你可以使用命令 “zstd -d example.tar.zst | tar xvf -“。使用”-d”选项指定压缩文件的名称。”tar”命令使用”-xvf”选项解压缩存档文件的内容。在解压缩之前,请记得备份压缩目录,以避免覆盖同名的已存在目录。

解压缩目录的输入

zstd -d example.tar.zst | tar xvf -

“zstd -d” 命令的输出

$ zstd -d example.tar.zst | tar xvf -
example/
example/file1.txt
example/file2.txt
example/file3.txt

5. 多线程压缩

如果你有一个需要快速压缩的大文件,我们可以使用Zstd,这是一款快速压缩工具,利用多个CPU核心来提高压缩速度。通过运行命令”zstd -T0 example.txt”,你可以激活多线程压缩,线程数为0,这将允许Zstd使用所有可用的CPU核心来压缩文件”example.txt”。通过这种方式,在Linux中可以激活多线程压缩。

多线程压缩的输入命令

zstd -T0 example.txt

这个命令的输出可能看起来像这样

zstd -T0 example.txt
example.txt         : 100%   [==================================================================================================]  10.00 MiB/s  2.20:1

6.设置压缩级别

Zstd有22个压缩级别,在压缩比和速度之间有不同的权衡。使用命令’zstd – ‘设置文件的压缩级别。较高的级别需要更多的CPU资源和时间。选择适合的压缩级别以获得最佳结果。

输入设置压缩级别的命令

zstd -3 example.txt

zstd压缩级别输出

example.txt             :  10.00 MB
--
example.txt         :  3.55 MB        (35.45%)   (x3.18)
--

结论

总的来说,Zstandard是一种有价值的压缩工具,可以节省磁盘空间并加快数据传输速度。在Linux上安装zstd是一个简单的过程,使用它进行文件压缩和解压缩也是非常容易的。我们相信本文对您初步了解zstd并指导您如何使用它来满足数据压缩需求起到了帮助作用。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程