有個需求,要把java代碼裡面的jzip壓縮方法還原成php
java jzip壓縮代碼如下
public static byte[] gZip(byte[] data) {
byte[] b = null;
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
GZIPOutputStream gzip = new GZIPOutputStream(bos);
gzip.write(data);
gzip.finish();
gzip.close();
b = bos.toByteArray();
bos.close();
} catch (Exception ex) {
ex.printStackTrace();
}
return b;
}
php代碼
class {
public static function myGzencode($str){
$res = gzencode($str);
$res = self::getBytes($res);
$res[9] = 0; //這步完全是為了和java壓縮後的頭部保持一緻,也許是個坑
return $res;
}
public static function getBytes($str) {
$len = strlen($str);
$bytes = array();
for($i=0;$i
if(ord($str[$i]) >= 128){
$byte = ord($str[$i]) - 256;
}else{
$byte = ord($str[$i]);
}
$bytes[] = $byte ;
}
return $bytes;
}
}
ps:https://tools.ietf.org/html/rfc1952#section-2.2
同學給推薦了這邊文章,如下内容
0 - FAT filesystem (MS-DOS, OS/2, NT/Win32)
1 - Amiga
2 - VMS (or OpenVMS)
3 - Unix
4 - VM/CMS
5 - Atari TOS
6 - HPFS filesystem (OS/2, NT)
7 - Macintosh
8 - Z-System
9 - CP/M
10 - TOPS-20
11 - NTFS filesystem (NT)
12 - QDOS
13 - Acorn RISCOS
255 - unknown
我壓縮後轉換成byte數組,第九位是11,java那邊是0,可能我的檔案系統是NTFS filesystem (NT),java可能是FAT filesystem (MS-DOS, OS/2, NT/Win32)
分類:
程式積累
/
浏覽量: 1722
2017-1-5 17:52 Thursday
昵稱
郵件位址 (選填)
個人首頁 (選填)
評論内容
發表評論
send