Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

check md5 of compressed file without unpacking it completely

I want to check the integrity of a backup of a Ubuntu disk, copied with dd onto a Windows share. There is not enough space to unpack the backup. Is there a utility to calculate the md5 of a compressed file without unpacking it completely?

like image 732
jdog Avatar asked Jun 28 '09 10:06

jdog


People also ask

How do I find the MD5 of a zip file?

Type the following command: md5sum [type file name with extension here] [path of the file] -- NOTE: You can also drag the file to the terminal window instead of typing the full path. Hit the Enter key. You'll see the MD5 sum of the file. Match it against the original value.


2 Answers

This:

gzip -d -c myfile.gz | md5sum

will stream the decompressed content into md5sum, rather than loading the whole thing into memory.

If it's a zip file, the command is unzip -p myfile.zip | md5sum

like image 93
RichieHindle Avatar answered Sep 22 '22 12:09

RichieHindle


The simple answer using gzip/zcat and piping to md5sum (which someone already posted while I was writing this) will work, but if you want to have more fun, here is a short Perl script which will do the same thing.

#!/usr/bin/perl

use strict;
use warnings;

use Archive::Zip qw/:ERROR_CODES :CONSTANTS/;
use Digest::MD5;

die "Usage: $0 zipfile filename" unless @ARGV == 2;

my $zipfile = $ARGV[0];
my $filename = $ARGV[1];

my $z = Archive::Zip->new();
die "Error reading $zipfile" unless $z->read($zipfile) == AZ_OK;
my $member = $z->memberNamed($filename);
die "Could not find $filename in $zipfile" unless $member;
$member->desiredCompressionMethod(COMPRESSION_STORED);
$member->rewindData();

my $md5 = Digest::MD5->new;
while(1) {
        my($buf,$status) = $member->readChunk();
        $md5->add($$buf) if $status == AZ_STREAM_END || $status == AZ_OK;
        last if $status == AZ_STREAM_END;
        die "IO Error" if $status != AZ_OK;
}
my $digest = $md5->hexdigest;
print "$digest  $zipfile/$filename\n";
like image 42
Adam Batkin Avatar answered Sep 19 '22 12:09

Adam Batkin