Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ZipArchive extraction - single file

Tags:

php

I have the following code:

$ipaFile= '/path/file.ipa';
$iconFilePath = "Payload/myapp.app/[email protected]"; // the pathway to my image file if the ipa file is unzipped.
$iconFile = "[email protected]";
$iconSaveFile = '/path/';

$zip = new ZipArchive();

if ($zip->open($ipaFile) === TRUE) {
    if($zip->locateName($iconFilePath) !== FALSE) {

     $zip->extractTo($iconSaveFile, $iconFile);

    }
}

My purpose is to dive into this zipped file and only pull out an image file. The code as is creates a a new image file in the directory I want it to go into (The file it creates has the right name and extension, but it is blank, 0 bytes). If I change $iconFile to iconFilePath then PHP correctly extracts the whole pathway. At the end of the pathway is the file (only the single file is extracted) I want, and it is correctly extracted. However, I don't want the folders preceding the file to be extracted, just the one file.

SO, in summary, I can correctly extract the entire zip archive, I can correctly extract the single file, but the file pathway to get to the file is also extracted, but I CANNOT correctly extract the single file all by itself.

I've done lots of searching on it and I can't spot my error. Thanks for your help.

like image 965
Pete_1 Avatar asked Dec 19 '14 00:12

Pete_1


People also ask

How do I extract just one zip file?

Open File Explorer and find the zipped folder. To unzip the entire folder, right-click to select Extract All, and then follow the instructions. To unzip a single file or folder, double-click the zipped folder to open it. Then, drag or copy the item from the zipped folder to a new location.

How do I extract a PHP file?

To unzip a file with PHP, we can use the ZipArchive class. ZipArchive is a simple utility class for zipping and unzipping files. We don't require any extra additional plugins for working with zip files. ZipArchive class provides us a facility to create a zip file or to extract the existing zip file.

How to extract a zip file to a folder using python?

We create a ZipFile object in READ mode and name it as zip. printdir() method prints a table of contents for the archive. extractall() method will extract all the contents of the zip file to the current working directory. You can also call extract() method to extract any file by specifying its path in the zip file.


1 Answers

ZipArchive::extractTo() extracts the whole folder structure; have you tried ZipArchive::getFromName(), which sounds like it might work better?

$ipaFile= '/path/file.ipa';
$iconFilePath = "Payload/myapp.app/[email protected]"; // the pathway to my image file if the ipa file is unzipped.
$iconFile = "[email protected]";
$iconSaveFile = '/path/';

$zip = new ZipArchive();

if ($zip->open($ipaFile) === TRUE) {
    if($iconData = $zip->getFromName($iconFilePath)) {
        file_put_contents("$iconSaveFile$iconFile", $iconData);
    }
}

That's totally untested code, just throwing it out there! If that doesn't work, and you know all the paths involved, you could just move the file after you extract it, couldn't you?

like image 111
miken32 Avatar answered Sep 21 '22 12:09

miken32