使用PHP脚本拆分小块的大型zip文件

时间:2012-10-18 11:12:40

标签: php file split

我正在使用下面的脚本将小型zip文件拆分成大块。

$filename = "pro.zip"; 
$targetfolder = '/tmp';

// File size in Mb per piece/split. 
// For a 200Mb file if piecesize=10 it will create twenty 10Mb files
$piecesize = 10; // splitted file size in MB


$buffer = 1024;
$piece = 1048576*$piecesize;
$current = 0;
$splitnum = 1;

if(!file_exists($targetfolder)) {
    if(mkdir($targetfolder)) {
        echo "Created target folder $targetfolder".br();
    }
}

if(!$handle = fopen($filename, "rb")) {
    die("Unable to open $filename for read! Make sure you edited filesplit.php correctly!".br());
}

$base_filename = basename($filename);

$piece_name = $targetfolder.'/'.$base_filename.'.'.str_pad($splitnum, 3, "0", STR_PAD_LEFT);
if(!$fw = fopen($piece_name,"w")) {
    die("Unable to open $piece_name for write. Make sure target folder is writeable.".br());
}
echo "Splitting $base_filename into $piecesize Mb files ".br()."(last piece may be smaller in size)".br();
echo "Writing $piece_name...".br();
while (!feof($handle) and $splitnum < 999) {
    if($current < $piece) {
        if($content = fread($handle, $buffer)) {
            if(fwrite($fw, $content)) {
                $current += $buffer;
            } else {
                die("filesplit.php is unable to write to target folder");
            }
        }
    } else {
        fclose($fw);
        $current = 0;
        $splitnum++;
        $piece_name = $targetfolder.'/'.$base_filename.'.'.str_pad($splitnum, 3, "0", STR_PAD_LEFT);
        echo "Writing $piece_name...".br();
        $fw = fopen($piece_name,"w");
    }
}
fclose($fw);
fclose($handle);
echo "Done! ".br();
exit;

function br() {
    return (!empty($_SERVER['SERVER_SOFTWARE']))?'<br>':"\n";
}
?> 

但是这个脚本在目标临时文件夹中拆分后不会创建小文件。脚本成功运行,没有任何错误。

请帮我看看这里有什么问题?或者如果您有任何其他类似功能的工作脚本,请提供给我。

1 个答案:

答案 0 :(得分:1)

如上面的评论中所示,您可以使用split将文件拆分为较小的片段,然后可以使用cat将它们重新组合在一起。

split -b50m filename x

并将它们放回去

 cat xaa xab xac > filename

如果您希望将zip文件拆分为跨越式存档,以便您不需要一起重新加入它们,请查看zipsplit

 zipslit -n (size) filename  

所以你可以从exec脚本中调用zipsplit,然后大多数标准的解压缩工具应该能够将它重新组合在一起。 man zipslit了解更多选项,包括设置输出路径等。