1

我正在使用以下脚本将大 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";
}
?> 

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

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

4

1 回答 1

1

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

split -b50m filename x

并把它们放回去

 cat xaa xab xac > filename

如果您希望将 zipfile 拆分为跨越类型的存档,这样您就不需要将它们重新连接在一起,请查看zipsplit

 zipslit -n (size) filename  

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

于 2012-10-18T11:36:22.700 回答