PHP 8.3.4 Released!

RarEntry::getCrc

(PECL rar >= 0.1)

RarEntry::getCrcВозвращает CRC элемента архива

Описание

public RarEntry::getCrc(): string

Возвращает шестнадцатеричное строковое представление CRC элемента архива.

Список параметров

У этой функции нет параметров.

Возвращаемые значения

Возвращает CRC элемента архива или false в случае возникновения ошибки.

Список изменений

Версия Описание
PECL rar 2.0.0 Теперь этот метод возвращает корректные значения для многотомных архивов.

add a note

User Contributed Notes 1 note

up
0
aterlux at mail dot ru
5 years ago
RarEntry::getCrc() returs a lowercase hex-string (e.g. 'bf6fa85c') the same as hash_... functions, using the same polynomial as 'crc32b' algorithm.
So, it can be used to check CRC after a stream unpacking:

<?php
$archive_name
= 'archive.rar';

$entry_name = 'someentry.ext';

$rar = RarArchive::open($archive_name) or die("Cannot open archive $archive_name");

if (
$rar->isBroken()) {
die(
"The archive is broken!");
}

$entry = $rar->getEntry($entry_name) or die("Cannot find entry $entry_name");

$stream = $entry->getStream() or die("Cannot open stream");

$crc = hash_init('crc32b'); // Initializing the hash function

while (!feof($stream)) {
$s = fread($stream, 8192);
if (
$s === false) {
// Error reading (do not use fread(...) or die(...), because fread can return '0'!)
die('Error reading the compressed file.');
}
hash_update($crc, $s); // updating the hash

// ...
// Do whatever with the $s
}

fclose($stream);

$got_crc = hash_final($crc);
$need_crc = $entry->getCrc();

print(
"Got CRC: $got_crc" . PHP_EOL);
print(
"Need CRC: $need_crc" . PHP_EOL);

if (
$got_crc != $need_crc) {
// rollback
print("Sorry guys, the file was incorrect!" . PHP_EOL);
} else {
print(
"Everything is ok" . PHP_EOL);
}
?>
To Top