0

Is it possible to show the file from the zipped file in HTML Iframe.

For example: My_File.pdf.zip contains My_File.pdf.

I have something as below <iframe src="/path of the folder/My_File.pdf.zip" />.

Here src has the zipped file. But i want to extract out the zipped file and show My_File.pdf in the iframe.

Is it possible? How can I do this?

Note: My Client side is javascript/Jquery.

prince
  • 601
  • 6
  • 15
  • 30
  • I think this should get the job done for you. http://stackoverflow.com/questions/2095697/unzipping-files – jr0207 Apr 15 '14 at 13:39
  • @jr0207 isn't that an overkill? why not open the zip file on server using `ZipArchive` and stat the contents? – Latheesan Apr 15 '14 at 13:43
  • @jr0207: I'm getting ZipFile is not defined. Do i need to add any additional library to fix this? – prince Apr 15 '14 at 14:28

1 Answers1

1

Create a file called zip_explorer.php with the following contents:

<?php

// Get zip file name
$zip_file_name = isset($_GET['zip_file']) ? $_GET['zip_file'] : '';

// Proceed if file is given
if (!empty($zip_file_name) && file_exists($zip_file_name))
{
    // Read zip file contents
    $za = new ZipArchive();
    $za->open($zip_file_name);
    for ($i = 0; $i < $za->numFiles; $i++)
    {
        $stat = $za->statIndex( $i ); 
        echo basename($stat['name']) ."<br>";
    }
    $za->close();
}
else
    exit('Invalid zip file name.');

Then create an iframe on your page pointing to that page like this for example:

<iframe src="zip_explorer.php?zip_file=My_File.pdf.zip" width="200" height="100"></iframe>

Update

Alternatively, you can do this purely with JavaScript using this library: http://gildas-lormeau.github.io/zip.js/

Latheesan
  • 23,247
  • 32
  • 107
  • 201
  • Thanks for your prompt response. But i must do it using Javascript or Jquery or by any other javascript libraries – prince Apr 15 '14 at 14:26
  • @prince - Can you elaborate? how is this meant to work on the client side? you can still have my code on your server (next to where the zip files are) and on your client side, use ajax to make a request and populate a div, rather than a iframe. – Latheesan Apr 16 '14 at 07:05
  • Alternatively, you can do this purely with JavaScript using the [zip.js](http://gildas-lormeau.github.io/zip.js/) library. – Latheesan Apr 16 '14 at 07:09
  • 2
    Latheesan Kanes is correct. I would also opt to do this in php, but I didn't submit anything related to php because it wasn't in the tags. The answer that I posted was from an earlier submission that has since broken. There are three dependencies that aren't available at this time. I did find [zip.js and it's well documented](http://gildas-lormeau.github.io/zip.js/core-api.html#full-example) on their page. You should be able to form a solution from zip.js. – jr0207 Apr 15 '14 at 15:02