更多和更快总是我们想要的两件事,尤其是在数据方面。问题是“更多”和“更快”通常是不一致的,所以我们有文件压缩(file compression)。在Linux(Linux)中有几种压缩和解压缩文件的方法,我们将向您展示它们背后的方法。
在 Linux 中使用 Zip 进行压缩和解压缩(Zip and Unzip Using Zip in Linux)
Linux几乎在每个发行版中都内置了几个压缩工具,通常称为Linux 发行版(Linux distros)。Zip可能是最受欢迎的。Zip实用程序可用于图形用户界面 ( GUI ) 或终端。
在 Linux GUI 中压缩(Zip in the Linux GUI)
- 导航到(Navigate to the files)要压缩的文件并选择它们。然后右键单击并选择Compress。
- 输入压缩(zipped)存档的名称。请注意,您还可以选择其他两种压缩类型。这可能因发行版而异。
- 很快你就会看到你的压缩档案。请注意,存档大小远小于放入其中的文件的总大小。
在 Linux GUI 中解压(Unzip in the Linux GUI)
- 找到要解压的压缩包并右键单击它。选择在此处提取(Extract Here)或提取到(Extract to…)...。Extra Here 将内容放入此目录。提取(Extract)到...允许您选择其他地方来放置内容。
文件被提取。请注意(Notice),它们已恢复到每个 100 MB 的完整大小。
归档管理器在 Linux GUI 中解压(Archive Manager to Unzip in the Linux GUI)
一些Linux发行版具有其他内置解压缩方式。在此示例中,您可以使用存档管理器(Manager)。
- 右键单击(Right-click)存档并选择使用存档管理器打开(Open With Archive Manager)。
- (Highlight)通过单击突出显示要提取的文件。您可以选择一个、几个或全部。然后选择左上角的提取。(Extract )
- 此时,您可以使用文件管理器选择提取文件的位置。然后选择右上角的提取。(Extract)
- 提取完成后,您可以继续或显示文件(Show the Files)。
该文件恢复为完整大小。存档中保留了一份副本。
Linux 终端中的 Zip 文件(Zip Files in Linux Terminal)
打开终端并导航到要压缩的文件所在的目录。输入命令 zip ziptest.zip *。
Zip告诉Linux使用 zip 实用程序,ziptest.zip告诉它所需的存档名称,asterisk (*)是通配符,表示压缩此目录中的所有文件。
它压缩文件,列出它们,并说明它对它们的压缩或压缩程度。
zip 命令可以使用许多操作。要查看它们,请输入zip –help,您将看到类似于下图的内容。
在 Linux 终端中解压文件(Unzip Files in Linux Terminal)
在终端中,使用命令unzip ziptest.zip,其中unzip是命令,而ziptest.zip是要解压缩的存档的名称。
它将显示正在解压缩的文件,因此您知道何时完成。
与 zip 命令一样,许多操作都可以与 unzip 命令一起使用。要查看它们,请输入unzip –help,您将看到类似于下图的内容。
Bzip2 用于在 Linux 中压缩和解压缩文件(Bzip2 for Zipping and Unzipping Files in Linux)
Bzip2是另一个内置于大多数Linux发行版的压缩实用程序。一个主要区别是 bzip2 不能将多个文件压缩到一个存档中。每个文件都有自己的压缩存档。
使用 Bzip2 在 Linux 终端中压缩文件(Zip Files in Linux Terminal With Bzip2)
输入命令bzip2 -kv9 testfile1.txt testfile2.txt其中 bzip2 是命令。
-kv9分解为k表示保留(k)原件,v表示v erbose 以便我们可以看到正在发生的事情,而9表示最高级别的压缩。您可以在 1 和 9 之间进行选择。压缩级别越高,压缩文件所需的时间越长。
输出比 zip 告诉我们更多,但最终结果几乎相同。
使用 Bzip2 在 Linux 终端中解压缩文件(Unzip Files In Linux Terminal With Bzip2)
输入命令bzip2 -kvd testfile.1.txt.bz2 testfile2.txt.bz2。-kvd选项分解为k用于保存档案(-kvd ),v用于v erbose 输出,d用于d (k)ecompress(d)。
您会看到正在解压缩的文件,并且知道何时完成。
要查看 bzip2 选项,请输入bzip2 –help,您将看到以下内容。玩弄非关键文件上的选项,看看它们能做什么。
Gzip 用于在 Linux 中压缩和解压缩文件(Gzip for Zipping and Unzipping Files in Linux)
这是大多数发行版都包含的最后一个流行的压缩实用程序。它比bzip2和zip选项更轻。但是,压缩质量仍然大致相同。
使用 Gzip 在 Linux 终端中压缩文件(Zip Files in Linux Terminal With Gzip)
输入命令gzip2 -kv9 testfile.1.txt testfile2.txt。-kv9选项分解为k表示(k )保留(k)文件(-kv9 ),v表示v erbose 输出,9表示 1 到 9 之间的最高压缩级别。
正如详细的输出所示,gzip 的工作原理与其他压缩方法一样好。
使用 Gzip 在 Linux 终端中解压缩文件(Unzip Files in Linux Terminal With Gzip)
有两种解压缩 gzip 档案的方法。一种是使用gzip,另一种是gunzip。
对于gzip命令,使用gzip -kvd testfile1.txt.gz testfile2.txt.gz。注意d选项。就是解压(d)的意思。
对于gunzip命令,使用gunzip testfile1.txt.gz testfile.2.txt.gz。与 gzip 的唯一区别是 gunzip 不需要基本解压缩选项。
在 Linux 中使用 Tar 压缩和解压缩文件怎么样?(What About Tar to Zip and Unzip Files in Linux?)
为什么还没有提到tar ?它是一种归档工具,可以将一堆文件放入一个归档文件中,以便于传输。无论文件大小如何,tar 文件的大小都将大致相同。
但是,如果您将 zip 方法与 tar 结合使用,那么您会得到一些非常酷的东西。您会得到一个压缩良好的单个文件包。
在文件目录上使用其他 zip 方法,您将获得目录中每个文件的压缩存档。在目录上使用带有 gzip 选项的 tar 会压缩所有内容并制作一个存档。
使用 Tar 和 Gzip 在 Linux 终端中压缩文件(Zip Files in Linux Terminal With Tar and Gzip)
输入命令tar -czvf Documents.tgz Documents。
-czvf选项分解为c 表示创建(c )新存档,z表示使用(z )g (c)z ip压缩, v表示v erbose(f)输出,f表示文件等于存档,这意味着存档保持原始目录的文件结构。
必须命名新存档,在此示例中为Documents.tgz 。通过使用.tgz文件扩展名,其他人将知道这是一个经过 gzip 压缩的 tar 存档。最后Documents是归档和压缩的目录。
输出如下所示。
在File Manager中,您可以看到 tar 存档并且它已被压缩。
使用 Tar 和 Gzip 在 Linux 终端中解压缩文件(Unzip Files in Linux Terminal With Tar and Gzip)
要解压缩 gzip 压缩的 tar 存档,它是相同的 tar 命令,但选项略有不同。
输入命令tar -xzvf Documents.tgz Documents。
-xzvf选项分解为 x 表示 e x tract,z表示使用g z ip解压缩,v表示详细,以便我们可以看到它发生,f表示 file=archive 表示保留文件结构。Documents.tgz是要解压和解压的存档,Documents是您希望内容进入的目录。
结果如下图所示。这两个文件恢复到完整大小,它们位于Documents目录中。
要查看 tar 选项,请输入tar –help,您将获得几页选项。请注意(Notice),除了 gzip 之外,还有其他可用的 zip 方法,因此您可以选择您喜欢的一种。
Linux 中不太流行的 Zip 工具(Less Popular Zip Tools in Linux)
大多数Linux(Linux)发行版中还有两个压缩实用程序。但是,它们并不那么受欢迎。尽管如此,它们都列在这里,所以你知道它们。
LZMA
LZMA是另一个经常在Linux发行版中找到的命令行压缩实用程序。这是 7-Zip 使用的压缩算法。
XZ
XZ 实用程序是一个命令行压缩工具,通常包含在Linux发行版中。它的选项类似于 bzip2 的选项。它基于基于LZMA的(LZMA)LZMA2算法。
可以使用命令lzma –help和xz –help找到有关这些实用程序的更多信息。
在 Linux 中没有其他方法可以压缩和解压缩吗?(Aren’t There Other Ways To Zip and Unzip in Linux?)
我们只介绍了大多数Linux(Linux)发行版中内置的压缩工具。尽管如此,Linux还有其他几种压缩和解压缩文件的方法。有些有GUI(GUIs),有些是命令行实用程序,有些既可以与它们的 GUI 一起使用,也可以通过命令行使用。您在Linux中使用这些 zip 工具之一吗?你最喜欢哪一个(Which one),为什么?让我们在评论中知道。
7 Ways to Zip and Unzip Files in Linux
More and faster are alwаys two things we want, especially when it cоmes to data. The problem is that “mоre” and “faster” are usually at odds, so we have file compression. There are several ways to zip and unzip files in Linux, and we’re going to show you the methods behind them.
Zip and Unzip Using Zip in Linux
Linux has several compression tools built into almost every distribution, commonly known as Linux distros. Zip is likely the most popular. The Zip utility can be used in the graphical user interface (GUI) or the terminal.
Zip in the Linux GUI
- Navigate to the files to be compressed and select them. Then right-click and select Compress.
- Enter a name for the zipped archive. Note that you can also choose two other types of compression. This may vary between distros.
- Soon you’ll see your zipped archive. Note that the archive size is much less than the total size of the files put into it.
Unzip in the Linux GUI
- Find the zipped archive to unzip and right-click on it. Select Extract Here or Extract to…. Extra Here puts the contents into this directory. Extract to… allows you to select somewhere else to put the contents.
The files are extracted. Notice that they’re back to their full size of 100 MB each.
Archive Manager to Unzip in the Linux GUI
Some Linux distros have other ways to unzip built into them. In this example, you can use Archive Manager.
- Right-click on the archive and select Open With Archive Manager.
- Highlight the files to extract by single-clicking on them. You can select one, a few, or all of them. Then select Extract in the top-left corner.
- At this point, you can choose where to extract the files using the file manager. Then select Extract in the top-right corner.
- When the extraction is done, you can either carry on or Show the Files.
The file is back at its full size. A copy is left inside the archive.
Zip Files in Linux Terminal
Open the terminal and navigate to the directory where the files to be zipped are located. Enter the command zip ziptest.zip *.
Zip tells Linux to use the zip utility, ziptest.zip tells it the desired name for the archive, the asterisk (*) is a wildcard meaning zip all the files in this directory.
It zips the files, lists them, and says how much it has deflated or compressed them.
Many actions can be used with the zip command. To view them, enter zip –help, and you’ll see something like the following image.
Unzip Files in Linux Terminal
In the Terminal, use the command unzip ziptest.zip, where unzip is the command and ziptest.zip is the name of the archive to unzip.
It will show the files being unzipped, so you know when it’s done.
Like the zip command, many actions can be used with the unzip command. To view them, enter unzip –help, and you’ll see something like the following image.
Bzip2 for Zipping and Unzipping Files in Linux
Bzip2 is another compression utility built into most Linux distros. A major difference is that bzip2 cannot zip multiple files into one archive. Each file gets its own zipped archive.
Zip Files in Linux Terminal With Bzip2
Enter the command bzip2 -kv9 testfile1.txt testfile2.txt where bzip2 is the command.
-kv9 breaks down to k means keep the originals, v means verbose so we can see what’s happening, and 9 for the highest level of compression. You can choose between 1 and 9. The higher the level of compression, the longer zipping the files takes.
The output tells us more than zip does, but the end result is almost the same.
Unzip Files In Linux Terminal With Bzip2
Enter the command bzip2 -kvd testfile.1.txt.bz2 testfile2.txt.bz2. The -kvd options break down as k for keep the archives, v for verbose output, and d for decompress.
You see the files being decompressed and known when it’s done.
To view the bzip2 options, enter bzip2 –help, and you’ll see the following. Play around with the options on non-critical files just to see what they can do.
Gzip for Zipping and Unzipping Files in Linux
This is the last of the popular compression utilities that are included with most distros. It’s lighter than bzip2 and zip for options. However, the quality of compression is still about the same.
Zip Files in Linux Terminal With Gzip
Enter the command gzip2 -kv9 testfile.1.txt testfile2.txt. The -kv9 options break down as k for keep the files, v for verbose output, and 9 for the highest compression level between 1 and 9.
As the verbose output shows, gzip works about as well as the other zipping methods.
Unzip Files in Linux Terminal With Gzip
There are two ways to unzip gzip archives. One is to use the gzip, and the other is gunzip.
For the gzip command, use gzip -kvd testfile1.txt.gz testfile2.txt.gz. Notice the d option. That means to decompress.
For the gunzip command, use gunzip testfile1.txt.gz testfile.2.txt.gz. The only difference from gzip is that gunzip doesn’t require options for a basic decompression.
What About Tar to Zip and Unzip Files in Linux?
Why hasn’t tar been mentioned yet? It’s an archiving tool, taking a bunch of files and putting them into one archive for easy transport. Whatever the file sizes are, the size of the tar file will be about the same.
But if you combine a zip method with tar, then you get something really cool. You get a nicely compressed single package of files.
Using the other zip methods on a directory of files, you’d get a compressed archive for each file in the directory. Using tar with the gzip option on the directory compresses everything and makes one archive.
Zip Files in Linux Terminal With Tar and Gzip
Enter the command tar -czvf Documents.tgz Documents.
The -czvf options break down as c for create a new archive, z for compress with gzip, v for verbose output, and f for file equals archive, which means the archive maintains the file structure of the original directory.
The new archive must be named, which is Documents.tgz in this example. By using the .tgz file extension, others will know that this is a tar archive that has been gzipped. Finally Documents is the directory to archive and compress.
The output looks like the following.
In File Manager, you can see the tar archive and that it’s compressed.
Unzip Files in Linux Terminal With Tar and Gzip
To unzip a gzipped tar archive, it’s the same tar command with slightly different options.
Enter the command tar -xzvf Documents.tgz Documents.
The -xzvf options break down as x for extract, z for unzip with gzip, v for verbose so we can watch it happen, and f for file=archive means keep the file structure. Documents.tgz is the archive to be unpacked and unzipped, and Documents is the directory into which you want the contents to go.
The results are shown in the image below. The two files are back to full size, and they are in the Documents directory.
To view the tar options, enter tar –help, and you’ll get several pages of options. Notice that there are different zip methods available other than gzip, so you can choose the one you like.
Less Popular Zip Tools in Linux
There are two more compression utilities found in most Linux distros. However, they’re not that popular. Nevertheless, they’re listed here, so you’re aware of them.
LZMA
LZMA is another command-line compression utility often found in Linux distros. It’s the compression algorithm used by 7-Zip.
XZ
The XZ utility is a command-line compression tool often included in Linux distros. Its options are similar to those of bzip2. It’s based on the LZMA2 algorithm, which is based on LZMA.
More information about these utilities can be found by using the commands lzma –help and xz –help.
Aren’t There Other Ways To Zip and Unzip in Linux?
We’ve only gone into the compression tools built into most Linux distros. Still, Linux has several other ways to zip and unzip files. Some have GUIs, some are command-line utilities, and some can be used both with their GUI and through the command line. Do you use one of these zip tools in Linux? Which one is your favorite and why? Let us know in the comments.