attachment-server/phplib/libfilelist2zip.php

51 lines
2.0 KiB
PHP

<?php
function filelist_to_zip($fnl) {
//$fnl = [ [ (string)name, (string)data ], ...]
$res="";
$fentry_info=[];
for($a=0; $a<count($fnl); $a++) {
$fentry_info[$a]=strlen($res);
$fe="";
$fe.=pack("H*", str_replace(" ", "", "50 4B 03 04 01 00 00 08 00 00 00 00 00 00"));
$fe.=strrev(pack("H*", hash("crc32b", $fnl[$a][1])));
$fe.=strrev(pack("H*", str_pad(dechex(strlen($fnl[$a][1])), 8, "0", STR_PAD_LEFT)));
$fe.=strrev(pack("H*", str_pad(dechex(strlen($fnl[$a][1])), 8, "0", STR_PAD_LEFT)));
$fe.=strrev(pack("H*", str_pad(dechex(strlen($fnl[$a][0])), 4, "0", STR_PAD_LEFT)));
$fe.=pack("H*", str_replace(" ", "", "00 00"));
$fe.=$fnl[$a][0];
$fe.=$fnl[$a][1];
$res.=$fe;
}
unset($fe);
unset($a);
$doffset=strlen($res);
for($a=0; $a<count($fnl); $a++) {
$de="";
$de.=pack("H*", str_replace(" ", "", "50 4B 01 02 01 00 01 00 00 08 00 00 00 00 00 00"));
$de.=strrev(pack("H*", hash("crc32b", $fnl[$a][1])));
$de.=strrev(pack("H*", str_pad(dechex(strlen($fnl[$a][1])), 8, "0", STR_PAD_LEFT)));
$de.=strrev(pack("H*", str_pad(dechex(strlen($fnl[$a][1])), 8, "0", STR_PAD_LEFT)));
$de.=strrev(pack("H*", str_pad(dechex(strlen($fnl[$a][0])), 4, "0", STR_PAD_LEFT)));
$de.=pack("H*", str_replace(" ", "", "00 00 00 00 00 00 00 00 00 00 00 00"));
$de.=strrev(pack("H*", str_pad(dechex($fentry_info[$a]), 8, "0", STR_PAD_LEFT)));
$de.=$fnl[$a][0];
$res.=$de;
}
unset($de);
unset($a);
$dsize=strlen($res)-$doffset;
$res.=pack("H*", str_replace(" ", "", "50 4B 05 06 00 00 00 00"));
$res.=strrev(pack("H*", str_pad(dechex(count($fnl)), 4, "0", STR_PAD_LEFT)));
$res.=strrev(pack("H*", str_pad(dechex(count($fnl)), 4, "0", STR_PAD_LEFT)));
$res.=strrev(pack("H*", str_pad(dechex($dsize), 8, "0", STR_PAD_LEFT)));
$res.=strrev(pack("H*", str_pad(dechex($doffset), 8, "0", STR_PAD_LEFT)));
$res.=pack("H*", str_replace(" ", "", "00 00"));
return $res;
}