PHP解压zip文件,并且读取文件内容输出

今天接到一个任务如下

在命令行模式下,

用PHP, 解压下面zip文件(到临时文件)

解析后,读取.upg文件,在offset 8处,读32个字节出来,输出到控制台

一,解析需求

1.要在控制台读取参数

2.在PHP中解压参数中的.zip文件,然后将zip文件中包含的文件解压到一个临时目录

3.读取临时目录里的.upg文件,然后将其中的第8~32的字符读出来,将字符转换成16进制,然后转换成ascll码输出

4.删除临时目录

 

二.实现

1.读取控制台的参数

 

在PHP中,读取命令行中的参数有几种,我亲自测试成功的有以下两种

(1)使用$argv参数

echo "argv:".var_dump($argv);

 

可以看到php 后面带有的参数都被读到了再$argv参数中,而我们真正需要的只是mcw.zip这个参数,所以我们需要用到end($arr)这个函数

 

var_dump(end($argv));

 

 

到这里我们就完成了第一个任务:读取参数获得需要节要的zip文件

 

 

 

 

2.在PHP中解压参数中的.zip文件,然后将zip文件中包含的文件解压到一个临时目录

解压zip文件使用ZipArchive 这个类。先定义一个ZipArchive实例,然后将我们的zip文件解压到一个目录下面

$route = "temp";

$zip = new ZipArchive();

if ($zip->open($file) === true){

   $mcw =  $zip->extractTo($route);//解压到$route这个目录中

    $zip->close();

}

 

 

 

可以看到已经解压出来了,接下来就是要读取mcw.upg这个文件了。

 

3.读取临时目录里的.upg文件,然后将其中的第8~32的字符读出来

先把整个文件都读取出来,然后只获取第一行(因为8~32字节的数据在第一行,所以我们只需要读第一行就可以了)。

将第一行中的每个字符都转换成16进制

这里使用到了bin2hex这个函数转换


$temp = substr($line,$i,1);

$str .= bin2hex($temp);

这样将第一行每个字符都转换成16进制之后,我们就开始截取8~32字节的字符了。因为每个字符转换成16进制之后就会变成两个数字,所以每次截取字符串相邻的两个字符


  

$result = substr($str,$start*2,$offset*2);





if ($mcw){

    $this->getBytes($file1,8,32);

}



public function getBytes($file,$start,$offset){

    //转换成16进制

   $content = file($file);

   $line = $content[0];

   $str = "";

   for ($i = 0;$i < strlen($line); $i++){

       $temp = substr($line,$i,1);

       $str .= bin2hex($temp);

   }

   //截取字符

    $result = substr($str,$start*2,$offset*2);

   $this->toAscll($result);



}

到这里为止我们就将8~32个字节的字符的16进制读取出来了,但是我们仍然还需要将它转换为ascll码

 

因为遇到0就是结束了,所以在8~32个字符中,只要出现0就不再处理了。

for ($i = 0;$i < strlen($str); $i+=2){

    $temp = substr($str,$i,2);

    if ($temp == "00"){

        break;

    }

    array_push($arr,$temp);

}

 

因为上面得到的两个16进制的数字,还不是完整的16进制表达,完整的16进制表达式0x12,这样的表示,所以要在每个字符前面加上0x。然后使用chr()函数将它转换成ascll码

for ($i = 0;$i<sizeof($arr);$i++){

   $temp = "0x".$arr[$i];

   echo chr($temp);

}

 

到此,已经可以输出我们想要的结果了

 

(4)删除临时目录

因为我们把数据解压到一个临时目录里面,所以当我们程序结束之后就要把它删除了。

使用unlink($file)删除一个文件,使用rmdir($dir)删除一个目录

public function deleteFile($url,$file1,$file2){

    //删除压缩后的目录

   if (file_exists($file1)){

       if (unlink($file1) && unlink($file2)){

           rmdir($url);

       }

   }

}

全部代码如下

<?php
/**
 * Created by PhpStorm.
 * User: lwl
 * Date: 2018/11/26
 * Time: 10:19
 * function:读取zip文件中若干个字节并输出
 */
header('utf-8');


 class Read{
     //解压缩zip文件
     public function getZipFile($file){
         $name = explode(".",$file);
         $name = $name[0];
         $route = "temp";
         $file1 = "temp/".$name.".upg";
         $file2 = "temp/".$name.".bin";
        $zip = new ZipArchive();
        if ($zip->open($file) === true){
           $mcw =  $zip->extractTo($route);
            $zip->close();
            if ($mcw){
                $this->getBytes($file1,8,32);
                $this->deleteFile($route,$file1,$file2);
            }
        }
     }
     public function getBytes($file,$start,$offset){
         //转换成16进制
        $content = file($file);
        $line = $content[0];
        $str = "";
        for ($i = 0;$i < strlen($line); $i++){
            $temp = substr($line,$i,1);
            $str .= bin2hex($temp);
        }
        //截取字符
         $result = substr($str,$start*2,$offset*2);
        $this->toAscll($result);

     }
     public function deleteFile($url,$file1,$file2){
         //删除压缩后的目录
        if (file_exists($file1)){
            if (unlink($file1) && unlink($file2)){
                rmdir($url);
            }
        }
     }
     public function toAscll($str){
        //每两个数字作为一个asc码,遇到零就结束
         $arr = [];
        for ($i = 0;$i < strlen($str); $i+=2){
            $temp = substr($str,$i,2);
            if ($temp == "00"){
                break;
            }
            array_push($arr,$temp);
        }

        for ($i = 0;$i<sizeof($arr);$i++){
           $temp = "0x".$arr[$i];
           echo chr($temp);
        }

     }
 }
 $tempp = $argv;
 $file = end($tempp);
 $byte = new Read();
 $byte->getZipFile($file);

 

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用Java的java.util.zip包来实现解压zip文件并读取其内容。下面是一个示例代码: ```java import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; public class UnzipExample { public static void main(String[] args) { String zipFilePath = "path/to/your/zip/file.zip"; String destinationFolder = "path/to/destination/folder"; try { unzip(zipFilePath, destinationFolder); readContents(destinationFolder); } catch (IOException e) { e.printStackTrace(); } } public static void unzip(String zipFilePath, String destinationFolder) throws IOException { byte[] buffer = new byte[1024]; File folder = new File(destinationFolder); if (!folder.exists()) { folder.mkdirs(); } try (ZipInputStream zipInputStream = new ZipInputStream(new BufferedInputStream(new FileInputStream(zipFilePath)))) { ZipEntry zipEntry; while ((zipEntry = zipInputStream.getNextEntry()) != null) { String entryName = zipEntry.getName(); File entryFile = new File(destinationFolder + File.separator + entryName); if (entryFile.getParentFile() != null && !entryFile.getParentFile().exists()) { entryFile.getParentFile().mkdirs(); } if (!zipEntry.isDirectory()) { int bytesRead; while ((bytesRead = zipInputStream.read(buffer)) != -1) { entryFile.createNewFile(); FileOutputStream fileOutputStream = new FileOutputStream(entryFile); fileOutputStream.write(buffer, 0, bytesRead); fileOutputStream.close(); } } else { entryFile.mkdirs(); } zipInputStream.closeEntry(); } } } public static void readContents(String folderPath) { File folder = new File(folderPath); if (folder.exists() && folder.isDirectory()) { File[] files = folder.listFiles(); if (files != null) { for (File file : files) { if (file.isDirectory()) { System.out.println("Directory: " + file.getName()); } else { System.out.println("File: " + file.getName()); } } } } } } ``` 在上述示例代码中,你需要替换`zipFilePath`和`destinationFolder`变量的值为你实际的zip文件路径和解压后的目标文件夹路径。`unzip`方法负责解压zip文件,`readContents`方法用于读取解压后的文件内容并打印。 请确保你已经正确配置了Java环境,并导入了必要的类和包。运行示例代码后,它将解压zip文件并打印出解压后的文件文件夹列表。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值