如何在不解压缩的情况下检查 Unix .tar.gz 文件是否是有效文件?

发布于 2024-08-16 09:11:15 字数 157 浏览 6 评论 0原文

我发现了问题 如何确定数据是否是有效的 tar 文件没有文件?,但我想知道:是否有现成的命令行解决方案?

I have found the question How to determine if data is valid tar file without a file?, but I was wondering: is there a ready made command line solution?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

苦妄 2024-08-23 09:11:15

只获取 tarball 的列表并丢弃输出而不是解压缩文件怎么样?

tar -tzf my_tar.tar.gz >/dev/null

根据评论进行编辑。感谢 Frozen Flame! 该测试绝不意味着数据的完整性。因为它被设计为磁带归档实用程序,所以 tar 的大多数实现将允许同一文件的多个副本!

What about just getting a listing of the tarball and throw away the output, rather than decompressing the file?

tar -tzf my_tar.tar.gz >/dev/null

Edit as per comment. Thanks Frozen Flame! This test in no way implies integrity of the data. Because it was designed as a tape archival utility most implementations of tar will allow multiple copies of the same file!

月亮邮递员 2024-08-23 09:11:15

您可以使用 gzip -t 选项来测试文件完整性

http:// linux.about.com/od/commands/l/blcmdl1_gzip.htm

来自:http://unix.ittoolbox.com/groups/technical-function/shellscript-l/how-to-test-file-integrity-of -targz-1138880(存档版本此处)

测试 gzip 文件是否未损坏:

gunzip -t file.tar.gz

测试内部的 tar 文件是否未损坏:

gunzip -c file.tar.gz | tar -t > /dev/null

作为一部分对于备份,您可能只需运行后一个命令即可
检查 $ 的值?之后为 0(成功)值。如果焦油
或者 gzip 有问题,$?将具有非零值。

you could probably use the gzip -t option to test the files integrity

http://linux.about.com/od/commands/l/blcmdl1_gzip.htm

from: http://unix.ittoolbox.com/groups/technical-functional/shellscript-l/how-to-test-file-integrity-of-targz-1138880 (archived version here)

To test the gzip file is not corrupt:

gunzip -t file.tar.gz

To test the tar file inside is not corrupt:

gunzip -c file.tar.gz | tar -t > /dev/null

As part of the backup you could probably just run the latter command and
check the value of $? afterwards for a 0 (success) value. If either the tar
or the gzip has an issue, $? will have a non zero value.

维持三分热 2024-08-23 09:11:15

如果要对 tar 文件进行实际测试提取而不提取到磁盘,请使用 -O 选项。这会将提取内容喷出到标准输出而不是文件系统。如果 tar 文件损坏,该过程将因错误而中止。

焦油球测试失败的示例...

$ echo "this will not pass the test" > hello.tgz
$ tar -xvzf hello.tgz -O > /dev/null
gzip: stdin: not in gzip format
tar: Child returned status 1
tar: Error exit delayed from previous errors
$ rm hello.*

工作示例...

$ ls hello*
ls: hello*: No such file or directory
$ echo "hello1" > hello1.txt
$ echo "hello2" > hello2.txt
$ tar -cvzf hello.tgz hello[12].txt
hello1.txt
hello2.txt
$ rm hello[12].txt
$ ls hello*
hello.tgz
$ tar -xvzf hello.tgz -O
hello1.txt
hello1
hello2.txt
hello2
$ ls hello*
hello.tgz
$ tar -xvzf hello.tgz
hello1.txt
hello2.txt
$ ls hello*
hello1.txt  hello2.txt  hello.tgz
$ rm hello*

If you want to do a real test extract of a tar file without extracting to disk, use the -O option. This spews the extract to standard output instead of the filesystem. If the tar file is corrupt, the process will abort with an error.

Example of failed tar ball test...

$ echo "this will not pass the test" > hello.tgz
$ tar -xvzf hello.tgz -O > /dev/null
gzip: stdin: not in gzip format
tar: Child returned status 1
tar: Error exit delayed from previous errors
$ rm hello.*

Working Example...

$ ls hello*
ls: hello*: No such file or directory
$ echo "hello1" > hello1.txt
$ echo "hello2" > hello2.txt
$ tar -cvzf hello.tgz hello[12].txt
hello1.txt
hello2.txt
$ rm hello[12].txt
$ ls hello*
hello.tgz
$ tar -xvzf hello.tgz -O
hello1.txt
hello1
hello2.txt
hello2
$ ls hello*
hello.tgz
$ tar -xvzf hello.tgz
hello1.txt
hello2.txt
$ ls hello*
hello1.txt  hello2.txt  hello.tgz
$ rm hello*
痴者 2024-08-23 09:11:15

您还可以使用 pigz (并行 gzip)检查 *.tag.gz 文件的内容,以加快存档检查速度:

pigz -cvdp number_of_threads /[...]path[...]/archive_name.tar.gz | tar -tv > /dev/null

You can also check contents of *.tag.gz file using pigz (parallel gzip) to speedup the archive check:

pigz -cvdp number_of_threads /[...]path[...]/archive_name.tar.gz | tar -tv > /dev/null
妖妓 2024-08-23 09:11:15

一个不错的选择是使用 tar -tvvf 添加一行报告文件类型。

有效 .tar 文件中的示例:

> tar -tvvf filename.tar 
drwxr-xr-x  0 diegoreymendez staff       0 Jul 31 12:46 ./testfolder2/
-rw-r--r--  0 diegoreymendez staff      82 Jul 31 12:46 ./testfolder2/._.DS_Store
-rw-r--r--  0 diegoreymendez staff    6148 Jul 31 12:46 ./testfolder2/.DS_Store
drwxr-xr-x  0 diegoreymendez staff       0 Jul 31 12:42 ./testfolder2/testfolder/
-rw-r--r--  0 diegoreymendez staff      82 Jul 31 12:42 ./testfolder2/testfolder/._.DS_Store
-rw-r--r--  0 diegoreymendez staff    6148 Jul 31 12:42 ./testfolder2/testfolder/.DS_Store
-rw-r--r--  0 diegoreymendez staff  325377 Jul  5 09:50 ./testfolder2/testfolder/Scala.pages
Archive Format: POSIX ustar format,  Compression: none

损坏的 .tar 文件:

> tar -tvvf corrupted.tar 
tar: Unrecognized archive format
Archive Format: (null),  Compression: none
tar: Error exit delayed from previous errors.

A nice option is to use tar -tvvf <filePath> which adds a line that reports the kind of file.

Example in a valid .tar file:

> tar -tvvf filename.tar 
drwxr-xr-x  0 diegoreymendez staff       0 Jul 31 12:46 ./testfolder2/
-rw-r--r--  0 diegoreymendez staff      82 Jul 31 12:46 ./testfolder2/._.DS_Store
-rw-r--r--  0 diegoreymendez staff    6148 Jul 31 12:46 ./testfolder2/.DS_Store
drwxr-xr-x  0 diegoreymendez staff       0 Jul 31 12:42 ./testfolder2/testfolder/
-rw-r--r--  0 diegoreymendez staff      82 Jul 31 12:42 ./testfolder2/testfolder/._.DS_Store
-rw-r--r--  0 diegoreymendez staff    6148 Jul 31 12:42 ./testfolder2/testfolder/.DS_Store
-rw-r--r--  0 diegoreymendez staff  325377 Jul  5 09:50 ./testfolder2/testfolder/Scala.pages
Archive Format: POSIX ustar format,  Compression: none

Corrupted .tar file:

> tar -tvvf corrupted.tar 
tar: Unrecognized archive format
Archive Format: (null),  Compression: none
tar: Error exit delayed from previous errors.
茶色山野 2024-08-23 09:11:15

我已经尝试过以下命令并且它们运行良好。

bzip2 -t file.bz2
gunzip -t file.gz

但是,我们可以发现这两个命令非常耗时。也许我们需要一些更快速的方法来确定压缩文件的完整性。

I have tried the following command and they work well.

bzip2 -t file.bz2
gunzip -t file.gz

However, we can found these two command are time-consuming. Maybe we need some more quick way to determine the intact of the compress files.

两仪 2024-08-23 09:11:15

这些都是非常次优的解决方案。来自 GZIP 规范

ID2(身份识别2)
这些具有固定值 ID1 = 31 (0x1f, \037), ID2 = 139
(0x8b, \213),将文件识别为 gzip 格式。

必须编码成您使用的任何语言。

These are all very sub-optimal solutions. From the GZIP spec

ID2 (IDentification 2)
These have the fixed values ID1 = 31 (0x1f, \037), ID2 = 139
(0x8b, \213), to identify the file as being in gzip format.

Has to be coded into whatever language you're using.

以歌曲疗慰 2024-08-23 09:11:15

使用以下命令
tar -tvf <文件名>
例如。
tar -tvf my_data.tgz

Use below command
tar -tvf <filename>
Eg.
tar -tvf my_data.tgz

断舍离 2024-08-23 09:11:15

>使用 -O 选项。 [...] 如果 tar 文件损坏,进程将因错误而中止。

有时是,但有时不是。让我们看一个损坏文件的例子:

echo Pete > my_name
tar -cf my_data.tar my_name 

# // Simulate a corruption
sed < my_data.tar 's/Pete/Fool/' > my_data_now.tar
# // "my_data_now.tar" is the corrupted file

tar -xvf my_data_now.tar -O

它显示:

my_name
Fool  

即使你执行

echo $?

tar 说没有错误:

0

但是文件被损坏了,它现在有“Fool”而不是“Pete”。

> use the -O option. [...] If the tar file is corrupt, the process will abort with an error.

Sometimes yes, but sometimes not. Let's see an example of a corrupted file:

echo Pete > my_name
tar -cf my_data.tar my_name 

# // Simulate a corruption
sed < my_data.tar 's/Pete/Fool/' > my_data_now.tar
# // "my_data_now.tar" is the corrupted file

tar -xvf my_data_now.tar -O

It shows:

my_name
Fool  

Even if you execute

echo $?

tar said that there was no error:

0

but the file was corrupted, it has now "Fool" instead of "Pete".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文