简体   繁体   English

从目录中的ZIP读取单个文件

[英]Read single file from ZIP within a directory

I have a ZIP file (with a VPK extension) and I wish to extract a file that is within a directory of the zip file. 我有一个ZIP文件(带有VPK扩展名),我希望提取一个压缩文件目录中的文件。 The uploaded file uploads correctly. 上传的文件正确上传。 Here is my current code. 这是我当前的代码。 but unfortunately it throws up an error. 但不幸的是,它引发了一个错误。

$hbid =  substr(md5(time()),0,16);
mkdir("pkg/".$hbid, 0700);
mkdir("pkg_image/".$hbid, 0700);
$target_dir = "pkg/" . $hbid . "/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
...
FILE UPLOADING CODE HERE
...

ERROR -> $handle = fopen('zip://./'.$target_file.'#/sce_sys/icon0.png', 'r'); 

$result = '';
if($handle){
while (!feof($handle)) {
  $result .= fread($handle, 8192);
}
fclose($handle);
$file = fopen("pkg_image/".$hbid."/icon0.png");
fwrite($file,$result);
fclose($file);

The error code is this: 错误代码是这样的:

fopen(zip://./pkg/0152cc9c0c52da70/4rows_1_1.vpk#/sce_sys/icon0.png): failed to open stream: operation failed

I've never extracted a file this way before but looking at other answers related to this, they all extract a file from the root of the zip, but the file I need is in a subdirectory of the zip file. 我以前从未以这种方式提取过文件,但是查看与此相关的其他答案,它们都从zip的根目录中提取了一个文件,但是我需要的文件位于zip文件的子目录中。 I'm not entirely sure what I am doing wrong though. 我不确定自己在做什么错。

Thanks. 谢谢。

Figured it out. 弄清楚了。 The correction is to replace the /sce_sys with #sce_sys. 纠正措施是将/ sce_sys替换为#sce_sys。 The initial / is not required for a directory. 目录不需要首字母/。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM