使用 Zstandard (zstd) 進行無損即時資料壓縮
井然有序的資料壓縮是有效資料管理的重要組成部分。它使我們能夠節省磁碟空間,減少資料傳輸時間,並提高整體資料效率。在眾多逐年普及的資料壓縮工具中,zstd 憑藉其高效能脫穎而出。zstd 由 Yann Collet 開發,是一個開源軟體,它在保持快速解壓縮速度的同時提供了令人印象深刻的壓縮率。
Zstd 是一種靈活且適應性強的 資料壓縮工具,適用於壓縮各種型別的資料,例如文字、影像、音訊和影片等。此外,它與多個平臺相容,包括 Linux、Windows、MacOS 和 FreeBSD,使其成為不同使用者可靠且易於訪問的工具。此外,zstd 的靈活性還體現在其使用者友好的命令列介面上,該介面包含多個選項和功能。
在本文中,我們將詳細解釋 zstd,探討其工作原理以及如何安裝它。此外,我們將提供有關如何使用 zstd 壓縮和解壓縮檔案的實用示例,並演示其一些高階功能,例如多執行緒、字典學習和壓縮級別等。在閱讀完本文後,您將全面瞭解 zstd 以及如何利用它來滿足您的資料壓縮需求。
在 Linux 上安裝 Zstandard
Zstandard 在大多數 Linux 發行版的官方軟體庫中都可用,因此可以使用包管理器輕鬆安裝。下面列出了在 Ubuntu 上安裝它的一些命令:
安裝 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 命令示例的一些基本用法。
如何在 Linux 中使用“Zstd”命令的不同示例列表
1. 壓縮檔案
要使用 Zstd 壓縮檔案,請使用命令“zstd filename”。預設壓縮級別為 3,但我們可以使用“-# (數字)”進行調整。Zstd 非常適合大型檔案,可以減小其大小,以便於儲存和傳輸。它還具有快速解壓縮速度,便於在不損失效率的情況下輕鬆恢復。Zstd 是一個簡單而強大的工具,可以改進 Linux 系統上的資料管理。
壓縮檔案的輸入命令
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 - directory_name/ | zstd > compressed_file_name.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 -<level> <file>”命令設定檔案的壓縮級別。較高的級別需要更多的 CPU 資源和時間。選擇合適的壓縮級別以獲得最佳結果。
設定壓縮級別的輸入命令
zstd -3 example.txt
zstd 壓縮級別的輸出
example.txt : 10.00 MB -- example.txt : 3.55 MB (35.45%) (x3.18) --
結論
總之,Zstandard 是一種寶貴的壓縮工具,可以節省磁碟空間並加快資料傳輸速度。在 Linux 上安裝 zstd 非常簡單,並且使用它來壓縮和解壓縮檔案也很容易。我們相信本文幫助您開始使用 zstd,並指導您如何使用它來增強您的資料壓縮需求。
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP