我想做什么:
1.解压缩压缩文件
2.将未压缩的文件字节插入数组
3.将每个字节转换为chr。
代码:
<?php
$list = file_get_contents('http://www.getlist.xp3.biz/list3'); //get the contents of the compressed file
$list = gzuncompress($list); //Uncompress the file
$bytes = unpack('c*', $list); //unpack the bytes of list into array
$string = implode('', array_map('chr', $bytes)); //convert each byte to chr by implode and array_map functions
echo $string; //print results
?>
这段代码几乎可以运作
结果:
http://getlist.xp3.biz/getlist.php
让我们以此行为例:
B~12ee397313ba2dd4f27fc1430744e615e4f83a44f9b206fb78fdf9b45dd9dc74fel profanador88's Room ar�7t��Bk
在该行的末尾有一些有线字符:ar�7t��Bk
此字符不是文本字节,某人(其他开发人员)转换为字节,他的php 文件返回更多信息行:
{"ver":"26","id":"12ee397313ba2dd4f27fc1430744e615e4f83a44f9b206fb78fdf9b45dd9dc74f","name":"profanador88's Room","players":"1","max_players":"10","password":false,"country":"ar","latitude":"41.0186004639","longitude":"28.9647006989","distance":1168.7633}
我有 id , roomname 和国家代码(以及werid chars(=其他信息))。
在他的行:版本, id ,名称,玩家编号,最大玩家编号,if isset密码(布尔值), contry < / strong>代码,纬度,经度和距离。
完全 7 。
也许ar�7t��Bk
包含其他信息?
我认为其他信息是(100%),我只是不知道如何转换字节(werid字符)。
我试图向其他开发人员询问这个可怕的字符,但没有答案
也许有人知道如何将这个二进制文件转换为普通字符串,
也许这些信息可以提供帮助(其他开发者的说明):
the list is a compressed binary file
which you can obtain @ /list3
it is compressed with zlib deflate
after decompression the format is as follows: (wait a bit must open the source file)
1 byte ; list format version ( currently = 1 )
4 bytes ; skip this, you don't care about them
then follows a list of RoomInfo structures until the end of the file
this is the Roominfo structure:
data.writeShort(ver); //Vrsion Short
data.writeUTF(netID); //ID String
data.writeUTF(name); //ROOMNAME string
data.writeByte(players); //PLAYERS
data.writeByte(maxPlayers); //MaxPlayers
data.writeBoolean(password); //If isset password boolean
data.writeUTF(countryCode); //country code
data.writeFloat(latitude); //Float latitude
data.writeFloat(longitude); //Float longitude
that's all
我使用chr
函数将字节转换为大字符串,hi表示该字节不仅仅是字符串,而是有字节代表:Float,Boolean,Integer,Short。
chr是仅表示字符串的字节(我使用它:$string = implode('', array_map('chr', $bytes));
),那里即时我可能需要通过foreach读取每个字节而不使用此快捷方式。
我的问题是:
是否知道如何修复代码并使其工作(使php打印缺少的其他信息(信息在那里,但以字节为单位(不是字符串)))?
的更新:
我认为我找到了将字节转换为short / boolean / float / integer的类,
现在我需要有人向我解释如何使用这个类(require('class.php')...code here
),或者有人会尝试用这个类修复我的代码(我试图单独修复它但我仍然不知道如何使用这个库或何时我需要使用lib)。
班级:
class ByteArray{
private $BytesString;
function ByteArray($bytes = "") {
$this->BytesString = $bytes;
}
function writeBoolean($value = 1) {
$this->BytesString .= $this->writeByte($value, False);
}
function writeByte($value, $noReturning=True) {
if ($noReturning) $this->BytesString .= pack("C", $value);
else return pack("C", $value);
}
function writeBytes($value) {
$this->BytesString .= $value;
}
function writeInt($value) {
$this->BytesString .= pack('N', $value);
}
function writeShort($value) {
$this->BytesString .= pack('n', $value);
}
function writeUTF($value) {
$valueSize = strlen($value);
$this->writeShort($valueSize);
$this->writeUTFBytes($value);
}
function writeUTFBytes($value) {
$this->BytesString .= $value;
}
function length() {
return strlen($this->BytesString);
}
function toString() {
return $this->BytesString;
}
function toPack() {
$value = pack('N', strlen($this->BytesString)+4);
return $value.$this->BytesString;
}
function getSize() {
$value = unpack('N', substr($this->BytesString, 0, 4));
return $value[1];
}
function readBy($Pos) {
$this->BytesString = substr($this->BytesString, $Pos);
return $this->BytesString;
}
function loc($byte) {
$loc = substr($this->BytesString, 0, $byte);
$this->BytesString = substr($this->BytesString, $byte);
return unpack('C', $loc);
}
function readInt() {
$size = unpack('N', substr($this->BytesString, 0, 4)); $size = $size[1];
$this->BytesString = substr($this->BytesString, 4);
return $size;
}
function readUTF() {
$size = unpack('n', substr($this->BytesString, 0, 2)); $size = $size[1];
$string = substr($this->BytesString, 2, $size);
$this->BytesString = substr($this->BytesString, $size + 2);
return $string;
}
function readShort() {
$size = unpack('n', substr($this->BytesString, 0, 2)); $size = $size[1];
$this->BytesString = substr($this->BytesString, 2);
return $size;
}
function readBoolean() {
$loc = unpack('C', substr($this->BytesString, 0, 1)); $loc = $loc[1];
$this->BytesString = substr($this->BytesString, 1);
if ($loc == 1) return True;
else return False;
}
function readByte() {
$byte = unpack('C', substr($this->BytesString, 0, 1)); $byte = $byte[1];
$this->BytesString = substr($this->BytesString, 1);
return $byte;
}
}
UPDATE2:
该类的第2版,如果第一个不起作用
<?php
class ByteArray {
protected $bigEndian = TRUE;
protected $byteArray;
protected $capacity;
protected $limit;
protected $mark;
public $position;
public function __construct($byteArray = '') {
$this->byteArray = $byteArray;
$this->position = 0;
$this->mark = - 1;
$this->init ();
}
private function init() {
$this->capacity = strlen ( $this->byteArray );
$this->limit = $this->capacity;
}
public function _array() {
return $this->byteArray;
}
public function clear() {
$this->limit = $this->capacity;
$this->position = 0;
$this->mark = - 1;
}
private function get($length = null) {
if ($length === null) {
$length = $this->limit - $this->position;
} elseif ($length > $this->bytesAvailable ()) {
throw new Exception ( 'bytesAvailable' );
}
$data = substr ( $this->byteArray, $this->position, $length );
$this->position += $length;
return $data;
}
private function set($bytes) {
$p1 = substr ( $this->byteArray, 0, $this->position );
$p2 = substr ( $this->byteArray, $this->position );
$len = strlen ( $bytes );
if ($len < strlen ( $p2 )) {
$p2 = substr ( $p2, $len );
} else {
$p2 = '';
}
$p1 .= $bytes . $p2;
$this->byteArray = $p1;
$this->position += $len;
$this->init ();
}
public function readBytes($length = -1, $offset = -1) {
$limit = $this->limit;
if ($offset == - 1) {
$offset = $this->position;
}
if ($length == - 1) {
$length = $limit - $offset;
}
if ($length > $limit - $offset) {
return null;
}
return substr ( $this->byteArray, $offset, $length );
}
public function writeBytes($bytes, $offset = 0, $length = 0) {
$len = strlen ( $bytes );
if ($len < 1) {
return;
}
if ($length < 1) {
$length = $len;
}
if ($offset < 1) {
$offset = 0;
}
if ($offset + $length > $len) {
return;
}
$p1 = substr ( $bytes, $offset, $length );
$this->set ( $p1 );
}
public function readBoolean() {
return $this->readByte () != 0;
}
public function writeBoolean($value) {
$this->writeByte ( $value != 0 );
}
public function readByte() {
return ord ( $this->get ( 1 ) );
}
public function readUnsignedByte() {
$data = unpack ( 'C', $this->get ( 1 ) );
return $data [1];
}
public function writeByte($value) {
$data = pack ( 'c', $value );
$this->set ( $data );
}
public function readShort() {
$data = unpack ( $this->bigEndian ? 'n' : 'v', $this->get ( 2 ) );
return $data [1];
}
public function writeShort($value) {
$data = pack ( $this->bigEndian ? 'n' : 'v', $value );
$this->set ( $data );
}
public function readInt() {
$data = unpack ( $this->bigEndian ? 'N' : 'V', $this->get ( 4 ) );
return $data [1];
}
public function writeInt($value) {
$data = pack ( $this->bigEndian ? 'N' : 'V', $value );
$this->set ( $data );
}
public function readFloat() {
$data = unpack ( 'f', $this->get ( 4 ) );
return $data [1];
}
public function writeFloat($value) {
$data = pack ( 'f', $value );
$this->set ( $data );
}
public function readDouble() {
$data = unpack ( 'd', $this->get ( 8 ) );
return $data [1];
}
public function writeDouble($value) {
$data = pack ( 'd', $value );
$this->set ( $data );
}
public function readString() {
$length = $this->readShort ();
$value = $this->get ( $length );
return $value;
}
public function writeString($value) {
$len = strlen ( $value );
$this->writeShort ( $len );
$this->writeStringBytes ( $value );
}
public function writeStringBytes($value) {
$len = strlen ( $value );
$data = pack ( 'a' . $len, $value );
$this->set ( $data );
}
public function readStringBytes($length) {
return $this->get ( $length );
}
public function bytesAvailable() {
return $this->limit - $this->position;
}
public function length() {
return $this->limit;
}
public function __toString() {
return $this->byteArray;
}
public function compress($level = 5) {
$this->byteArray = gzcompress ( $this->byteArray, $level );
$this->init ();
}
public function uncompress($level = 5) {
$this->byteArray = gzuncompress ( $this->byteArray, $level );
$this->init ();
}
}
?>