如何在不解压缩的情况下检查 Unix .tar.gz 文件是否是有效文件?
我发现了问题 如何确定数据是否是有效的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
只获取 tarball 的列表并丢弃输出而不是解压缩文件怎么样?
根据评论进行编辑。感谢 Frozen Flame! 该测试绝不意味着数据的完整性。因为它被设计为磁带归档实用程序,所以 tar 的大多数实现将允许同一文件的多个副本!
What about just getting a listing of the tarball and throw away the output, rather than decompressing the file?
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!
您可以使用 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 文件是否未损坏:
测试内部的 tar 文件是否未损坏:
作为一部分对于备份,您可能只需运行后一个命令即可
检查 $ 的值?之后为 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:
To test the tar file inside is not corrupt:
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.
如果要对 tar 文件进行实际测试提取而不提取到磁盘,请使用 -O 选项。这会将提取内容喷出到标准输出而不是文件系统。如果 tar 文件损坏,该过程将因错误而中止。
焦油球测试失败的示例...
工作示例...
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...
Working Example...
您还可以使用
pigz
(并行 gzip)检查 *.tag.gz 文件的内容,以加快存档检查速度:You can also check contents of *.tag.gz file using
pigz
(parallel gzip) to speedup the archive check:一个不错的选择是使用
tar -tvvf
添加一行报告文件类型。有效 .tar 文件中的示例:
损坏的 .tar 文件:
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:
Corrupted .tar file:
我已经尝试过以下命令并且它们运行良好。
但是,我们可以发现这两个命令非常耗时。也许我们需要一些更快速的方法来确定压缩文件的完整性。
I have tried the following command and they work well.
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.
这些都是非常次优的解决方案。来自 GZIP 规范
必须编码成您使用的任何语言。
These are all very sub-optimal solutions. From the GZIP spec
Has to be coded into whatever language you're using.
使用以下命令
tar -tvf <文件名>
例如。
tar -tvf my_data.tgz
Use below command
tar -tvf <filename>
Eg.
tar -tvf my_data.tgz
有时是,但有时不是。让我们看一个损坏文件的例子:
它显示:
即使你执行
tar 说没有错误:
但是文件被损坏了,它现在有“Fool”而不是“Pete”。
Sometimes yes, but sometimes not. Let's see an example of a corrupted file:
It shows:
Even if you execute
tar said that there was no error:
but the file was corrupted, it has now "Fool" instead of "Pete".