PHP处理JAVA的ZIP压缩后的字节流

1 篇文章 0 订阅
1 篇文章 0 订阅

PHP处理JAVA的ZIP压缩后的字节流


有遇到php与java对接接口的同学可以参考下,php zip压缩与java压缩方式不同,php兼容java字节流问题,此方法只适用于 本文中java解析加密 ,如有遇到相同问题可赐予解决,如有不同 检查java返回的zip流 修改php解压方法,头尾重新拼即可。

PHP 压缩解压方法

   #压缩方法
    public static function compress($str)
    {
        $zipPath = "/tmp/" .md5(rand(1000,99999999)) . "/";
        mkdir($zipPath);
        $name = md5("guopiao".rand(1000,999999)).".zip";
        //压缩
        $filename = $zipPath . $name;
        $zip = new ZipArchive();
        $zip->open($filename, ZipArchive::CREATE);   //打开压缩包
        $zip->addFromString("0", $str);
        $zip->close();  //关闭压缩包
        //base64
        $base64 = base64_encode(file_get_contents($filename));
        exec("rm -rf  " . $zipPath );
        return $base64;
    }

    #解压方法
    public static function decompress($str)
    {
        $str = base64_decode($str);
        $binArr = unpack("c*", $str);
        $head = [80, 75, 1, 2, 20, 0, 20, 0, 8, 8, 8, 0];
        $headcrc = [];
        for ($i = 11; $i < 15; $i++) {
            $headcrc[] = $binArr[$i];
        }
        $sufcrc = [];
        $binlength = sizeof($binArr);
        for ($i = -11; $i <= 0; $i++) {
            $sufcrc[] = $binArr[$binlength + $i];
        }
        $suf = [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 48, 80, 75, 5, 6, 0, 0, 0, 0, 1, 0, 1, 0, 47, 0, 0, 0, 50, 0, 0, 0, 0, 0];
        $byte = array_merge($binArr, $head, $headcrc, $sufcrc, $suf);
        $pack = pack("c*", ...$byte);
        $zipPath = "/tmp/" .md5(rand(1000,99999999)) . "/";
        mkdir($zipPath);
        $name = "0.zip";
        $filename = $zipPath . $name;
        file_put_contents($filename, $pack);
        //解压 zip解压有一个文件大小的问题没法拼 有可能报错 采取以下方式解压zip
        exec("unzip -o -d " . $zipPath . " " . $filename);
        $r = file_get_contents($zipPath ."/0");
        exec("rm -rf  " . $zipPath );
        //创建simpleXML对象
        $xml = simplexml_load_string($r);
        return base64_decode($xml->BODY[0], true)??[];
    }

JAVA 压缩解压方法

/**
     * 
     * @param req 请求报文
     * @param iszip 默认true
     * @return
     */
    public static String zipEncode(String req,boolean iszip){
        String encodeStr="";
        try{

            if(iszip){
                encodeStr=new sun.misc.BASE64Encoder().encodeBuffer(compress(req));
            }
            else{
                encodeStr=new sun.misc.BASE64Encoder().encodeBuffer(req.getBytes("UTF-8")).trim();
            }
            if(null!=encodeStr){
                encodeStr = encodeStr.replaceAll("\r\n", "").replaceAll("\n","");
            }
        }catch(Exception e){
            e.printStackTrace();
        }

        return encodeStr;
    }

    private static byte[] compress(String str) {
        if (str == null)
            return null;

        byte[] compressed;
        ByteArrayOutputStream out = null;
        ZipOutputStream zout = null;

        try {
            out = new ByteArrayOutputStream();
            zout = new ZipOutputStream(out);
            zout.putNextEntry(new ZipEntry("0"));
            zout.write(str.getBytes("UTF-8"));
            zout.closeEntry();
            compressed = out.toByteArray();
        } catch (IOException e) {
            compressed = null;
        } finally {
            if (zout != null) {
                try {
                    zout.close();
                } catch (IOException e) {
                }
            }
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                }
            }
        }
        return compressed;
    }


/**
     * 解密(压缩解密)
     * (先base64解密,在解压)
     * @param resp 相应报文
     * @param iszip    默认true
     * @return
     */
    public static String unzipDecode(String resp,boolean iszip) {    
        String unzipStr="";
        try{
             byte[] unzip=new sun.misc.BASE64Decoder().decodeBuffer(resp);
             if(iszip){                
                unzipStr=decompress(unzip);
             }
             else{
             unzipStr= new String(unzip,"UTF-8");
            }
        }
        catch(Exception e){
            e.printStackTrace();
        }

        return unzipStr;
    }

    /**
     * 解压
     * @param compressed
     * @return
     */
    private static String decompress(byte[] compressed) {
        if (compressed == null)
            return null;

        ByteArrayOutputStream out = null;
        ByteArrayInputStream in = null;
        ZipInputStream zin = null;
        String decompressed;
        try {
            out = new ByteArrayOutputStream();
            in = new ByteArrayInputStream(compressed);
            zin = new ZipInputStream(in);
            ZipEntry entry = zin.getNextEntry();
            byte[] buffer = new byte[1024];
            int offset = -1;
            while ((offset = zin.read(buffer)) != -1) {
                out.write(buffer, 0, offset);
            }
            decompressed = out.toString("UTF-8");
        } catch (IOException e) {
            decompressed = null;
        } finally {
            if (zin != null) {
                try {
                    zin.close();
                } catch (IOException e) {
                }
            }
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                }
            }
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                }
            }
        }

        return decompressed;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值