Пример #1
0
 /**
  * Creates an instance from a resource.
  * @param string   $filename    File name.
  * @param resource $fileHandler File handler.
  *
  * @throws FileCorruptedException
  *
  * @return FileHeader
  */
 public static function createFromResource($filename, $fileHandler)
 {
     $header = new FileHeader();
     $data = unpack("nmagicNumber/C1compressionMethod/C1flags/LmodificationTime/C1extraFlags/C1os", fread($fileHandler, 10));
     // check magic number and compression method
     if (self::MAGIC_NUMBER !== $data['magicNumber'] || self::COMPRESSION_METHOD_DEFLATE !== $data['compressionMethod']) {
         throw new FileCorruptedException($filename);
     }
     $header->setCompressionMethod($data['compressionMethod'])->setFlags($data['flags'])->setExtraFlags($data['extraFlags'])->setModificationTimeFromUnixEpoch($data['modificationTime'])->setOperatingSystem($data['os']);
     // if FLG.FEXTRA set
     if (($header->getFlags() & self::FLAGS_EXTRA) === self::FLAGS_EXTRA) {
         $extraFieldData = unpack("C2subfield/vlength", fread($fileHandler, 10));
         $header->setExtraSubfields($extraFieldData['subfield1'], $extraFieldData['subfield2'])->setExtraData(fread($fileHandler, $extraFieldData['length']));
     }
     // if FLG.FNAME set
     if (($header->getFlags() & self::FLAGS_NAME) === self::FLAGS_NAME) {
         $header->setOriginalFilename(self::readString($fileHandler));
     }
     // if FLG.FCOMMENT set
     if (($header->getFlags() & self::FLAGS_COMMENT) === self::FLAGS_COMMENT) {
         $header->setComment(self::readString($fileHandler));
     }
     // if FLG.FHCRC set
     if (($header->getFlags() & self::FLAGS_HCRC) === self::FLAGS_HCRC) {
         $crcData = unpack('vcrc', fread($fileHandler, 2));
         $header->setCrc16($crcData['crc']);
     }
     return $header;
 }