/** * Determines whether a file is text or binary by checking the first few bytes in the file. * The exact number of bytes is system dependent, but it is typically several thousand. * If every byte in that part of the file is non-null, considers the file to be text; * otherwise it considers the file to be binary. * * @param string $file * @return boolean */ public static function isBinary($file) { // open file $file = new File($file, 'rb'); // get block size $stat = $file->stat(); $blockSize = $stat['blksize']; if ($blockSize < 0) { $blockSize = 1024; } if ($blockSize > $file->filesize()) { $blockSize = $file->filesize(); } if ($blockSize <= 0) { return false; } // get bytes $block = $file->read($blockSize); return strlen($block) == 0 || preg_match_all('/\\x00/', $block, $match) > 0; }
/** * Adds a file to the tar archive. * * @param string $filename * @param string $addDir * @param string $removeDir * @return boolean result */ protected function addFile($filename, $addDir, $removeDir) { $filename = FileUtil::unifyDirSeparator($filename); $storedFilename = $filename; if (!empty($removeDir)) { $storedFilename = StringUtil::replaceIgnoreCase($removeDir, '', $filename); } if (!empty($addDir)) { $storedFilename = $addDir . $storedFilename; } if (is_file($filename)) { // open file $file = new File($filename, 'rb'); // write header if (!$this->writeFileHeader($filename, $storedFilename)) { return false; } // write file content while (($buffer = $file->read(512)) != '') { $this->file->write(pack('a512', $buffer)); } // close file $file->close(); } else { // only directory header if (!$this->writeFileHeader($filename, $storedFilename)) { return false; } } return true; }
/** * Determines whether a file is text or binary by checking the first few bytes in the file. * The exact number of bytes is system dependent, but it is typically several thousand. * If every byte in that part of the file is non-null, considers the file to be text; * otherwise it considers the file to be binary. * * @param string $file * @return boolean */ public static function isBinary($file) { // open file $file = new File($file, 'rb'); // get block size $stat = $file->stat(); $blockSize = $stat['blksize']; if ($blockSize < 0) $blockSize = 1024; if ($blockSize > $file->filesize()) $blockSize = $file->filesize(); if ($blockSize <= 0) return false; // get bytes $block = $file->read($blockSize); return (strlen($block) == 0 || strpos($block, "\0") !== false); }
/** * Sends the actual file to the client. */ protected function sendFile() { if ($this->startByte > 0 || $this->endByte < $this->options['filesize'] - 1) { $file = new File($this->location, 'rb'); if ($this->startByte > 0) { $file->seek($this->startByte); } while ($this->startByte <= $this->endByte) { $remainingBytes = $this->endByte - $this->startByte; $readBytes = $remainingBytes > 1048576 ? 1048576 : $remainingBytes + 1; echo $file->read($readBytes); $this->startByte += $readBytes; } $file->close(); } else { readfile($this->location); } }