【发布时间】:2016-09-18 06:43:30
【问题描述】:
我正在尝试解压缩另一个 zip 文件中的 zip 文件以获取第二个 zip 文件中的 xml 文件。
这个文件的最大挑战是 zip 文件中的文件总是不同的,因此是未知的。所以我创建了一个函数来将归档档案的列表放入一个文本列表中。然后使用此列表取消归档每个文件并从第二个 zip 文件中的 xml 文件中提取所需的信息。
这是我目前的代码。
//Set the date
$day = date("mdY");
echo $day."<br>";
//URL to download file from for updating the prescription table
//$url = "ftp://public.nlm.nih.gov/nlmdata/.dailymed/dm_spl_daily_update_".$day.".zip";
$url = "ftp://public.nlm.nih.gov/nlmdata/.dailymed/dm_spl_daily_update_09152016.zip";
//Saving the file on the server.
file_put_contents("Prescription_update.zip", fopen($url, 'r'));
//unzip the downloaded file
$zip = new ZipArchive;
if($zip->open('Prescription_update.zip')){
$path = getcwd() . "/update/";
$path = str_replace("\\","/",$path);
//echo $path;
$zip->extractTo($path);
$zip->close();
print 'ok<br>';
} else {
print 'Failed';
}
// integer starts at 0 before counting
$i = 0;
$dir = '/update/prescription/';
if ($handle = opendir($dir)) {
while (($file = readdir($handle)) !== false){
if (!in_array($file, array('.', '..')) && !is_dir($dir.$file))
$i++;
}
}
// prints out how many were in the directory need this for the loop later
echo "There were $i files<br>";
$dh = opendir($dir);
$files = array();
while (false !== ($filename = readdir($dh))) {
$files[] = $filename." \r\n";
}
//Created a list of files in the update/prescription folder
file_put_contents("list.txt", $files);
/*
* Creating a loop here to ready the names and extract the
* XML file from the zipped files that are in the update/prescription folder
*
*/
$ii = 2;
$fileName = new SplFileObject('list.txt');
$fileName->seek($ii);
echo $fileName."<br>"; //return the first file name from list.txt
$zip = new ZipArchive;
$zipObj = getcwd()."/update/prescription/".$fileName;
$zipObj = str_replace("\\","/", $zipObj);
echo $zipObj."<br>";
if($zip->open($zipObj)){
$path = getcwd() . "/update/prescription/tmp/";
$path = str_replace("\\","/",$path);
mkdir($path);
echo $path;
$zip->extractTo($path);
$zip->close();
print 'ok<br>';
} else {
print 'Failed';
}
无法弄清楚为什么第二个 ZipArchive::extractTo: 会引发错误。我认为这可能是一个路径问题。我所以我做了第二个字符串替换,希望可以清除它,但它没有。所以,举起我的手,要求对这个进行第二次观察。
更新错误日志条目
[18-Sep-2016 02:24:24 America/Chicago] PHP 1. {main}() C:\emr-wamp\www\interface\weno\update_prescription_drug_table.php:0
[18-Sep-2016 02:24:24 America/Chicago] PHP 2. ZipArchive->extractTo() C:\emr-wamp\www\interface\weno\update_prescription_drug_table.php:76
[18-Sep-2016 02:24:24 America/Chicago] PHP Warning: ZipArchive::close(): Invalid or unitialized Zip object in C:\emr-wamp\www\interface\weno\update_prescription_drug_table.php on line 77
[18-Sep-2016 02:24:24 America/Chicago] PHP Stack trace:
[18-Sep-2016 02:24:24 America/Chicago] PHP 1. {main}() C:\emr-wamp\www\interface\weno\update_prescription_drug_table.php:0
[18-Sep-2016 02:24:24 America/Chicago] PHP 2. ZipArchive->close() C:\emr-wamp\www\interface\weno\update_prescription_drug_table.php:77
【问题讨论】:
-
错误日志是什么?使用错误日志更新您的问题。
-
我想通了。要解压缩的第二个文件中的文件名。文件名太长。我手动缩短了文件名,它解压缩得很好。