zip文件的校验和 [英] checksums on zip files

查看:484
本文介绍了zip文件的校验和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用一个工具来上传一组文件,然后使用md5校验和将文件与上次上传的文件进行比较,并告诉您哪些文件已更改。

I am currently working on a tool that uploads a group of files, then uses md5 checksums to compare the files to the last batch that were uploaded and tells you which files have changed.

对于常规文件来说,这可以正常工作,但是一些上载的文件是zip归档文件,即使其中的文件相同,它们几乎总是已更改。

For regular files this is working fine but some of the uploaded files are zip archives, which almost always have changed, even when the files inside it are the same.

有没有一种方法可以执行不同类型的校验和,以检查这些文件是否已更改,而不必分别解压缩每个文件,然后分别比较每个文件的内容。

Is there a way to perform a different type of checksum to check if these files have changed without having to unzip each one individually and then comparing the contents of each file individually.

这是我当前的函数

function check_if_changed($date, $folder, $filename)
{
  $dh = opendir('./wp-content/uploads/Base/');
  while (($file = readdir($dh)) !== false) {
    $folders[] = $file;
  }
  sort($folders);
  $position = array_search($date, $folders);
  $prev_folder = $folders[$position - 1];
  if ($prev_folder == '.' || $prev_folder == '..')
    { return true;}
  $newhash = md5_file('./wp-content/uploads/Base/'.$date.'/'.$folder.'/'.$filename);
  $oldhash = md5_file('./wp-content/uploads/Base/'.$prev_folder.'/'.$folder.'/'.$filename);
  if ($oldhash != $newhash){
    return true;
  }
  return false;
}


推荐答案

在zip存档中,每个文件与元数据一起存储,如上次修改时间,文件名,文件大小(以字节为单位)等,以及重要部分- crc32校验和

Inside a zip archive, each "file" is stored with meta data like last modifcation time, filename, filesize in bytes, etc...and the important part - a crc32 checksum.

基本上,您可以仅以二进制方式对zip存档进行操作,找到每个文件的元数据标头,然后将校验和与先前存储的校验和进行比较。您无需进行任何解压缩即可访问zip存档中的元数据。这样会非常快。

basically, you can just operate on the zip archive in a binary fashion, finding each file's meta data header and comparing the checksum to the previously stored checksums. You don't need to do any uncompressing to access the meta data in a zip archive. This would be extremely fast.

http://en.wikipedia.org/wiki/Zip_(file_format)

编辑-实际上,ZipArchive提供了此功能。请参阅:
http://www.php.net/manual/zh-CN /ziparchive.statindex.php

edit- actually, ZipArchive offers this functionality. See: http://www.php.net/manual/en/ziparchive.statindex.php

这篇关于zip文件的校验和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆