2017-10-27 12 views
0

파일을 쓰는 동안 지정된 크기를 초과하는지 확인해야합니다. 내가 지정한 크기에 도달하면 필기를 중지하고 예외를 throw해야합니다. 정상 파일글 쓰는 동안 Gzip 및 Bzip2 파일의 오버플로를 어떻게 감지 할 수 있습니까?

예 :

$handle = fopen($filename, 'wb'); 
while (true) { 
    // its work 
    if (fstat($handle)['size'] + strlen($string) > BYTE_LIMIT) { 
     throw SizeOverflowException::withLimit(BYTE_LIMIT); 
    } 

    fwrite($this->handle, $string); 
} 
fclose($handle); 

아니면 독립적

$handle = fopen($filename, 'wb'); 
$used_bytes = 0; 
while (true) { 
    if ($used_bytes + strlen($string) > BYTE_LIMIT) { 
     throw SizeOverflowException::withLimit(BYTE_LIMIT); 
    } 

    fwrite($this->handle, $string); 
    $used_bytes += strlen($string); 
} 
fclose($handle); 

예 물품의 gzip 사용 된 바이트 수를 카운트 할 수 Bzip2의 비슷하게

$handle = gzopen($filename, 'wb9'); 
while (true) { 
    // not work 
    // fstat($handle) === false 
    //if (fstat($handle)['size'] + strlen($string) > BYTE_LIMIT) { 
    // throw SizeOverflowException::withLimit(BYTE_LIMIT); 
    //} 

    gzwrite($this->handle, $string); 
} 
gzclose($handle); 

:

$handle = bzopen($filename, 'w'); 
while (true) { 
    // not work 
    // fstat($handle) === false 
    //if (fstat($handle)['size'] + strlen($string) > BYTE_LIMIT) { 
    // throw SizeOverflowException::withLimit(BYTE_LIMIT); 
    //} 

    bzwrite($this->handle, $string); 
} 
bzclose($handle); 

이 경우 fstat()이 작동하지 않는 이유를 이해하지만 어떻게이 문제를 해결할 수 있습니까?

이제 비 압축 모드에서 사용되는 바이트 수만 계산할 수 있습니다.

답변

1

대신 ZLIB_ENCODING_GZIP 인코딩을 사용하고 deflate_adddeflate_init을 사용하여 메모리에 점진적으로 압축하고 fwrite으로 정상적으로 압축 데이터를 작성할 수있다. 그런 다음 작성된 압축 된 바이트 수를 계산하고 지정된 크기로 중지 할 수 있습니다.

+0

** 좋은 아이디어. 고마워. ** 불행히도, 나는 PHP 5.6을 지원해야하고 여전히 Bzip2 (( – ghost404