كيفية التحقق مما إذا كان ملف UNIX .tar.gz هو ملف صالح دون ضغط غير مضغوط؟
-
18-09-2019 - |
سؤال
لقد وجدت السؤال كيفية تحديد ما إذا كانت البيانات هي ملف القطران صالح دون ملف؟, ، لكنني كنت أتساءل: هل هناك حل جاهز سطر الأوامر؟
المحلول
ماذا عن مجرد الحصول على قائمة من tarball ورمي الناتج، بدلا من إلغاء ضغط الملف؟
tar -tzf my_tar.tar.gz >/dev/null
تحرير حسب التعليق. شكرا zrajm!
تحرير حسب التعليق. شكرا لهب المجمدة! هذا الاختبار بأي حال من الأحوال يعني سلامة البيانات. لأنه تم تصميمه كأداة مساعدة في الأرشيفية الشريطية ستسمح معظم تطبيقات القطران بنسخ متعددة من نفس الملف!
نصائح أخرى
ربما يمكنك استخدام خيار Gzip -T لاختبار سلامة الملفات
http://lineux.about.com/od/commands/l/blcmdl1_gzip.htm.
لاختبار ملف Gzip غير فاسد:
gunzip -t file.tar.gz
لاختبار ملف القطران الداخلي غير فاسد:
gunzip -c file.tar.gz | tar t > /dev/null
كجزء من النسخة الاحتياطية، ربما يمكنك تشغيل الأمر الأخير والتحقق من قيمة $؟ بعد ذلك للحصول على قيمة 0 (النجاح). إذا كان القطران أو gzip لديه مشكلة، $؟ سيكون لها قيمة غير صفرية.
إذا كنت ترغب في إجراء استخراج اختبار حقيقي لملف القطران دون استخراج إلى القرص، فاستخدم الخيار "". هذا يتغير المستخلص إلى الإخراج القياسي بدلا من نظام الملفات. إذا كان ملف القطران فاسد، فسوف يتم إحباط العملية بخطأ.
مثال على اختبار القطران الفاشل ...
$ 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*
يمكنك أيضا التحقق من محتويات ملف * .tag.gz باستخدام pigz
(gzip الموازي) لتسريع الاختيار الأرشيف:
pigz -cvdp number_of_threads /[...]path[...]/archive_name.tar.gz | tar -tv > /dev/null
لقد جربت الأمر التالي ويعملون بشكل جيد.
bzip2 -t file.bz2
gunzip -t file.gz
ومع ذلك، يمكننا أن نتمكن من العثور على هذين القيادة المستهلكة لفترة زمنية. ربما نحتاج إلى بعض الطرق السريعة لتحديد سليمة ملفات الضغط.
خيار جميل هو استخدام tar -tvvf <filePath>
الذي يضيف خطا يبلغ عن هذا الملف.
مثال في ملف .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.
هذه كلها حلول شبه مثالية للغاية. من gzip المواصفات
ID2 (تحديد 2)
هذه لها القيم الثابتة ID1 = 31 (0x1f، 037)، ID2 = 139 (0x8b، 213)، لتحديد الملف على أنه بتنسيق Gzip.
يجب ترميزها في أي لغة تستخدمها.
> استخدم الخيار "". [...] إذا كان ملف القطران تالف، فسوف يتم إحباط العملية بخطأ.
في بعض الأحيان نعم، ولكن في بعض الأحيان لا. دعونا نرى مثالا على ملف تالف:
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 $?
قال القطران إنه لم يكن هناك خطأ:
0
لكن الملف تالف، لديه الآن "أحمق" بدلا من "بيت".