jar文件打包 php zip打包下载文件

WBOY
发布: 2016-07-28 08:30:19
原创
1486 人浏览过
<ol>
<li>将文件打包成zip格式</li>
<li>下载文件的功能</li>
</ol><p>要点解析:</p><ol><li>这里我采用的是php自带的ZipArchive类</li></ol><p>    a) 我们只需要new一个ZipArchive对象,然后使用open方法创建一个zip文件,接着使用addFile方法,将要打包的文件写入刚刚创建的zip文件中,最好还得记得关闭该对象。</p><p>    b) 注意点:使用open方法的时候,第二个参数$flags是可选的,$flags用来指定对打开的zip文件的处理方式,共有四种情况</p><p>                    i.     <tt>ZIPARCHIVE::OVERWRITE</tt> 总是创建一个新的文件,如果指定的zip文件存在,则会覆盖掉</p><p>                      ii.    <tt>ZIPARCHIVE::CREATE</tt>     如果指定的zip文件不存在,则新建一个</p><p><tt>              iii.  </tt><tt>ZIPARCHIVE::EXCL      </tt><tt>如果指定的zip文件存在,则会报错   </tt></p><p><tt>               iv.  </tt><tt>ZIPARCHIVE::CHECKCON</tt></p>
登录后复制
$nid = <span>intval</span>($this-><span>_request</span>->getParam(<span>'nid'</span>))<span>;
</span>$filelist = $this-><span>model</span>->getOverenter($nid)<span>;
</span><span>foreach </span>($filelist <span>as </span>$f) {
    $row[<span>'file'</span>] = <span>ROOT</span>.<span>'/public/upload/logo/'</span>.$f[<span>'ne_file'</span>]<span>;
</span>$row[<span>'name'</span>] = $f[<span>'es_name'</span>]<span>;
</span>$ext = <span>explode</span>(<span>'.'</span><span>,</span>$f[<span>'ne_file'</span>])<span>;
</span>$row[<span>'ext'</span>] = $ext[<span>1</span>]<span>;
</span>$fileNameArr[]=$row<span>;
</span>}
$name = <span>date</span>(<span>'YmdHis'</span>).<span>'.zip'</span><span>;
</span>$filename = <span>ROOT</span>.<span>"/public/upload/"</span>.$name<span>; </span><span><em>// </em></span><span><em>最终生成的文件名(含路径)
</em></span>$zip = <span>new </span>ZipArchive ()<span>;
</span><span>if </span>(!<span>file_exists</span>($filename)) {
    $zip->open($filename<span>, </span><span>ZipArchive</span>::<span>CREATE</span>)<span>;</span><span><em>//</em></span><span><em>创建一个空的</em></span><span><em>zip</em></span><span><em>文件
</em></span><span><em></em></span><span>for </span>($i = <span>0</span><span>; </span>$i count($fileNameArr)<span>; </span>$i++) {
        $zip->addFile($fileNameArr[$i][<span>'file'</span>]<span>, </span><span>iconv</span>( <span>'UTF-8'</span><span>,</span><span>'GB2312'</span><span>, </span>$fileNameArr[$i][<span>'name'</span>]).<span>'.'</span>.$fileNameArr[$i][<span>'ext'</span>])<span>;
</span>}
    $zip->close()<span>;
</span><span><em>//</em></span><span><em>打开文件
</em></span><span><em></em></span><span>if</span>(<span>file_exists</span>($filename)){
        $file = <span>fopen</span>($filename<span>,</span><span>"r"</span>)<span>;
</span>Header(<span>"Content-type: application/octet-stream"</span>)<span>;
</span>Header(<span>"Accept-Ranges: bytes"</span>)<span>;
</span>Header(<span>"Accept-Length: "</span>.<span>filesize</span>($filename))<span>;
</span>Header(<span>"Content-Disposition: attachment; filename="</span>.$name)<span>;
</span><span>echo </span><span>fread</span>($file<span>, </span><span>filesize</span>($filename))<span>;
</span>$buffer=<span>1024</span><span>;
</span><span>while </span>(!<span>feof</span>($file)) {
            $file_data=<span>fread</span>($file<span>,</span>$buffer)<span>;
</span><span>echo </span>$file_data<span>;
</span>}
        <span>fclose</span>($file)<span>;
</span>}<span>else </span>{
        <span>echo </span><span>"<script>alert('file not exists!');</script>"</span><span>;
</span>}
}
<span>unlink</span>($filename)<span>;
</span><span>exit</span>()<span>;</span>
登录后复制

以上就介绍了jar文件打包 php zip打包下载文件,包括了jar文件打包方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板