fileSize = SnapIO::filesize($filepath); $instance->permissions = substr(sprintf('%o', fileperms($filepath)), -4); $instance->mtime = SnapIO::filemtime($filepath); if ($instance->fileSize > self::MAX_SIZE_FOR_HASHING) { $instance->hash = "00000000000000000000000000000000"; } else { $instance->hash = hash_file('crc32b', $filepath); } $instance->relativePath = $relativeFilePath; $instance->relativePathLength = strlen($instance->relativePath); return $instance; } /** * create header from src * * @param string $src source string * @param string $relativeFilePath relative path in archvie * @param int $forceSize if 0 size is auto of content is filled of \0 char to size * * @return static */ public static function createFromSrc($src, $relativeFilePath, $forceSize = 0) { $instance = new static(); $instance->fileSize = strlen($src); $instance->permissions = '0644'; $instance->mtime = time(); $srcLen = strlen($src); if ($forceSize > 0 && $srcLen < $forceSize) { $charsToAdd = $forceSize - $srcLen; $src .= str_repeat("\0", $charsToAdd); } if ($instance->fileSize > self::MAX_SIZE_FOR_HASHING) { $instance->hash = "00000000000000000000000000000000"; } else { $instance->hash = hash('crc32b', $src); } $instance->relativePath = $relativeFilePath; $instance->relativePathLength = strlen($instance->relativePath); return $instance; } /** * Write header in archive * * @param resource $archiveHandle archive resource * * @return int bytes written */ public function writeToArchive($archiveHandle) { $headerString = '' . $this->fileSize . '' . $this->mtime . '

' . $this->permissions . '

' . $this->hash . '' . $this->relativePathLength . '' . $this->relativePath . '
'; //SnapIO::fwrite($archiveHandle, $headerString); $bytes_written = @fwrite($archiveHandle, $headerString); if ($bytes_written === false) { throw new Exception('Error writing to file.'); } else { return $bytes_written; } } }