用PHP解压缩二进制文件 [英] Unpack binary file in PHP

查看:101
本文介绍了用PHP解压缩二进制文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<?php

$file = "/tmp/kok.bin";

$fp = fopen ($file, 'rb')
    or die ("File $file cannot be opened.");


$data = fread ($fp, filesize($file));
    or die ("Could not read data from file $file");

# Create the format for unpacking the header data
$header_format = 
    'Cid/' .            # Grab 1 byte      
    'Cnumber/' .        # Grab 1 byte
    'CIdentifier/' .    # Grab 1 byte     
    'CTime/' .          # Grab 8 bytes 
    'L# of Records/' .  # Grab 1 byte , number of target in a packet
    'SHeader Size/' .   # Grab 4 bytes
    'SRecord Size';     # Grab 2 bytes

# Unpack the header data
$header = unpack ($header_format, $data);
$targetRecord =$header_format['L# of Records'];

    
# Create the format for unpacking the data that describes the format of the records in the file
$record_format = 
    'CField Name/' .    # Grab 1 byte
    'fField Type1/' .   # Grab 4 bytes 
    'fField Type2/' .   # Grab 4 bytes
    'fField Type3/' .   # Grab 4 bytes 
    'fField number/' .  # Grab 4 bytes           
    'fdistance/' .      # Grab 4 bytes
    'CField id /'       # Grab 1 byte 
    'iField Precision/' # Grab 4 bytes the size of the Field Data
    'SField Data';      # Grab x bytes 
for ($offset = 0; $offset < strlen ($data); $offset ++) {
    print_r (unpack ("$record_format", $data));
}
?>





< br $>






1 first unpack $header_format to get 'L# of Records' for every packet.(have a total of 200 packets)
2 unpack  $record_format  'L# of Records' times
$record_format = 
     CField Name/' .    # Grab 1 byte
    'fField Type1/' .   # Grab 4 bytes 
    'fField Type2/' .   # Grab 4 bytes
    'fField Type3/' .   # Grab 4 bytes 
    'fField number/' .  # Grab 4 bytes           
    'fdistance/' .      # Grab 4 bytes
    'CField id /'       # Grab 1 byte 
    'iField Precision/' # Grab 4 bytes
    'SField Data';      # Grab x bytes 

3 Step 1 and 2 are for one packet from the file.
4 Repeat 1-3 until end of file is reached.  





我的尝试:





What I have tried:

for ($offset = 0; $offset < strlen ($data); $offset += 32) {
    $unpackeddata= unpack ("@$offset/$header_format", $data);
    print "";
    print_r ($unpackeddata);
    print "";
}

推荐答案

file = / tmp / kok.bin;

file = "/tmp/kok.bin";


fp = fopen(
fp = fopen (


file, ' rb'
die( 文件
file, 'rb') or die ("File


这篇关于用PHP解压缩二进制文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆