4

i created a cck file field and i uploaded zip file in that content type. Now i want to display the content of zip files after uploading the zip files. i don't know how to implement it?. can you tell me how to implement it?

thanks in advance.

5

It will not be very hard to accomplish. Don't let anyone tell you otherwise :D

What you need to do (if you have some experience with PHP and creating a very simple Drupal module) is download the PCLzip module which basically integrates the pclzip library into Drupal. There is no submodule for listing zip contents at the moment but it is very simple to make one if you keep in mind that there is a specific method in the library, the ListContent method.

You can see examples of how the methods of the library are used at the related projects that do use the pclzip module like PCLzip Folder or PCLzip Content Files. If you study their code, you will be able to achieve your goal very easily (and hopefully contribute your code back to the community).

Don't listen to advice that say: "extract the contents of the zip file first" since:

  1. There is no need to extract files out of the zip file, you just need to see them
  2. It's never a good idea to add extra complexity and write custom code that will be useful to nobody but you (and after a while so much custom code will be useless to you too)

Keep it simple: use the work of other people and expand it.

Edit: Let me give you an example so that you can understand how easy it is. If you check the pclzip module you will find a function that looks like this:

function pclzip_build_archive($filename) {
  // get the temp file
  global $pclzip_temp_file;
  $pclzip_temp_file = tempnam(file_directory_temp(), $filename);
  // build the archive
  require_once('pclzip/pclzip.lib.php');
  return new PclZip($pclzip_temp_file);
}

The line:

return new PclZip($pclzip_temp_file);

is the one that uses the PclZip method to create a new archive. All you need to do is create a new function, similar to that one, in your custom module, that will use the listContent method to list the contents of the zip file. It's that simple.

Notice, that there is one extra step after retrieving the contents of the zip file, and this is: "displaying them". Since the method of the pclzip library gives you back an array, it should be simple enough to display it (you can choose how you want to do it).

I have tested the following:

  1. Installed the pclzip module
  2. Made sure that the library is in the appropriate directory (according to pclzip instructions)
  3. Added the following code in the pclzip.module file:
function pclzip_nodeapi(&$node, $op) {

  $cckZip = $node->field_zipfile;
  $zipPath = $cckZip[0]['filepath'];
  $zipView = $cckZip[0]['view'];

  if (isset($zipPath)) {
    require_once('pclzip/pclzip.lib.php');
    $zip = new PclZip("$zipPath");

    if (($list = $zip->listContent()) == 0) {
      die("Error : ".$zip->errorInfo(true));
    }

    $zipView .= "<div>";

    for ($i=0; $i<sizeof($list); $i++) {
      $zipView .= $list[$i]['filename'] . "<br />";
    }

    $zipView .= "</div>";



    switch ($op) {
      case "view":
        $node->content['body']['#value'] .= $zipView;
    }
  } 
}

Notice that:

  • field_zipfile is the name of the cck field that I used to upload the zip file
  • I have edited the pclzip module directly (which is not a very good idea). You should probably do the changes in your own module, but I just wanted to test the solution very quickly
  • You should decide where the filenames will be printed. I have just added them at the end of the node's content without any specific formatting. you can add classes and specific tags to the filenames in order to theme them if you want.
| improve this answer | |
  • now i installed PCLzip module and i have the following $zip = new PclZip("test.zip"); if (($list = $zip->listContent()) == 0) { die("Error : ".$zip->errorInfo(true)); } for ($i=0; $i<sizeof($list); $i++) { for(reset($list[$i]); $key = key($list[$i]); next($list[$i])) { echo "File $i / [$key] = ".$list[$i][$key].""; } echo ""; } – Moorthy Jun 9 '11 at 15:31
  • i dont know where i have to put the function and how can i get the path of the upload file in the node.tpl.php and display the zip file content. i am new to drupal ,can you help me how to do it? – Moorthy Jun 9 '11 at 15:35
  • First of all, we are talking about Drupal 6 right? Latest version? Then, you want to display the contents of the zip file in the node or sth? Like: MyUploaded.zip (contents: file1.txt, myvideo.avi) ? I will probably be able to write a couple of php lines to help you solve this problem and possibly add it to the pclzip project – F1234k Jun 9 '11 at 18:16
  • thanks for your help .yes, i am using drupal 6.22 only and i want to display the content of zip file after saving the content type. For example , i have content type called "zip upload" and in that , there is file field to upload zip file. if user upload any zip files and click save button means, i want to show zip file content along zip file name as title in the content page itself. – Moorthy Jun 10 '11 at 4:49
  • Last edit contains most of the code that solves your problem. – F1234k Jun 10 '11 at 20:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.