Skip to content

Commit

Permalink
Refactor unzipFile method in class.core.php to adjust buffer size…
Browse files Browse the repository at this point in the history
… and remove periodic buffer clearing for improved memory management and performance
  • Loading branch information
N6REJ committed Aug 22, 2024
1 parent 95ea74d commit 6e2fed2
Showing 1 changed file with 4 additions and 9 deletions.
13 changes: 4 additions & 9 deletions core/classes/class.core.php
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ public function unzipFile($filePath, $destination, $progressCallback = null)
}

// Command to test the archive and get the number of files
$testCommand = escapeshellarg( $sevenZipPath ) . " t " . escapeshellarg( $filePath ) . " -y -bsp1";
$testCommand = escapeshellarg( $sevenZipPath ) . ' t ' . escapeshellarg( $filePath ) . ' -y -bsp1';
$testOutput = shell_exec( $testCommand );

// Extract the number of files from the test command output
Expand All @@ -497,17 +497,17 @@ public function unzipFile($filePath, $destination, $progressCallback = null)
Util::logDebug( 'Number of files to be extracted: ' . $numFiles );

// Command to extract the archive
$command = escapeshellarg( $sevenZipPath ) . " x " . escapeshellarg( $filePath ) . " -y -bb1 -o" . escapeshellarg( $destination );
$command = escapeshellarg( $sevenZipPath ) . ' x ' . escapeshellarg( $filePath ) . ' -y -bb1 -o' . escapeshellarg( $destination );
Util::logTrace( 'Executing command: ' . $command );

$process = popen( $command, 'r' );
$process = popen( $command, 'rb' );

if ( $process ) {
$filesExtracted = 0;
$buffer = '';

while ( !feof( $process ) ) {
$buffer .= fread( $process, 262144 ); // Read in smaller chunks of 64KB
$buffer .= fread( $process, 8192 ); // Read in smaller chunks of 4KB

while ( ($newlinePos = strpos( $buffer, "\n" )) !== false ) {
$line = substr( $buffer, 0, $newlinePos + 1 );
Expand All @@ -525,11 +525,6 @@ public function unzipFile($filePath, $destination, $progressCallback = null)
}
}
}

// Clear the buffer periodically to release memory ( 128k chunks )
if ( strlen( $buffer ) > 524288 ) {
$buffer = '';
}
}

$returnVar = pclose( $process );
Expand Down

0 comments on commit 6e2fed2

Please sign in to comment.