是这样,我们公司现在有个项目需要处理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);
if(ord($str{0})==120 && ord($str{1})==156 ) $src_zlib = substr($str, 2, -4);
$json = (gzinflate( $src_zlib));