OC

Knowledge OS
登录 注册
全部话题 移民 创业 iOS Mac Objective-C Swift Android 招聘 求职

PHP处理JAVA的ZIP压缩后的字节流,请问用什么方法?具体见内容

nycoder
nycoder 发布于 2014年08月04日 | 更新于 2014年08月07日
无人欣赏。

是这样,我们公司现在有个项目需要处理Java利用ByteArrayOutputStream方法生成的字节流,该字节流又利用ZipOutputStream进行压缩,最后利用base64编码。现在PHP这边可以利用base64解码,但是处理zip压缩的字节流出问题了。不知道哪个方法可以实现。现在如果利用fileputcontents只能得到一个不认可的zip文件,具体代码如下。

 /**java源码
 * 使用zip进行压缩
 * 
 * @param str 压缩前的文本
 * @return 返回压缩后的文本
 */
 public static final String encrypt(String str) {
 if (str == null)
 return null;
 byte[] compressed;
 ByteArrayOutputStream out = null;
 ZipOutputStream zout = null;
 String compressedStr = null;
 try {
 out = new ByteArrayOutputStream();
 zout = new ZipOutputStream(out);
 zout.putNextEntry(new ZipEntry("0"));
 zout.write(str.getBytes());
 zout.closeEntry();
 compressed = out.toByteArray();
 compressedStr = new sun.misc.BASE64Encoder()
 .encodeBuffer(compressed);
 } 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 compressedStr;
 }
//PHP
 $string = "UEsDBBQACAgIANWu+UQAAAAAAAAAAAAAAAABAAAAMEtMSgYAUEsHCMJBJDUFAAAAAwAAAA==";
 $decodeStr = base64_decode($string);
 file_put_contents("a.zip",$decodeStr);
 
共8条回复
楼长 ·
tinyfool 回复于 2014年08月04日

你先做一个简单的测试,看看是不是你java那边和php的decoder参数设置或者传输没做对,自己调试下这种问题

2楼 ·
nycoder 回复于 2014年08月04日

1楼 @tinyfool Java传过来的只是字符串。。没其他参数。。

3楼 ·
CurveSoft 回复于 2014年08月04日

用php_zlib处理字节流,zip流和zip文件不是一回事。

4楼 ·
lilien1010 回复于 2014年08月04日

if(ord($str{0})==120 && ord($str{1})==156 ) $src_zlib = substr($str, 2, -4);

 $json = (gzinflate( $src_zlib));
5楼 ·
nycoder 回复于 2014年08月07日

4楼 @lilien1010 没有输出结果啊。。。

6楼 ·
nycoder 回复于 2014年08月07日

4楼 @lilien1010 貌似PHP版本不一样,也不行。。

7楼 ·
CurveSoft 回复于 2014年08月07日

JAVA端用GZIP

8楼 ·
frankhe 回复于 2014年08月07日

特意注册了来回答。 刚刚搞过这个问题。

参考这个就行了: http://blog.csdn.net/cheligeer1988/article/details/14164727 http://stackoverflow.com/questions/4327517/android-decompress-string-that-was-compressed-with-php-gzcompress

登录 或者 注册

AltStyle によって変換されたページ (->オリジナル) /