473,385 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Open and unzip with the ZipArchive class fails

290 100+
Hi,

So far, last Friday, every file file I try and open and unzip with
the ZipArchive class has failed.

I have just used ZipArchive to create a zip file and then
attempt to unzip it.

Even this has failed.

Does this means that my server ZipArchive class is somehow corrupted ?

Should I reinstall that php class ( I think it is via PECL )

Or is there another reason ?

here is my script for creating the zip file
and then unzipping it.

( Because I am trying to trap every error it is longer than usual )

Expand|Select|Wrap|Line Numbers
  1. $new_zip = $file_dir.'aaa_new.zip';
  2.  
  3. if (!class_exists('ZipArchive')) {
  4.    write_log("Class ZipArchive not found\r\n"); 
  5.    exit;
  6.      } 
  7. else {
  8.   write_log("Class ZipArchive OK\r\n"); 
  9.   $zip = new ZipArchive;
  10. }
  11.  
  12. $res = $zip->open($new_zip, ZipArchive::CREATE);
  13. if ($res === TRUE) {
  14.     $zip->addFromString('data1.txt', 'file content goes here');
  15.     $fin = $zip->close();
  16.         if ($fin === TRUE) {
  17.        write_log("Created zip file: $new_zip\r\n");
  18.        } 
  19.     else {
  20.        write_log("Error on close: $fin \r\n");
  21.            exit;  
  22.            }   
  23.     }
  24. else {
  25.     write_log("Could NOT Create zip file $res\r\n");  
  26. }
  27.  
  28.  
  29. if (!$new_zip) {
  30.     write_log("Could not find  $new_zip\r\n");  
  31.     exit;
  32.         } 
  33. else {
  34.     write_log("Found: $new_zip\r\n");
  35.     }
  36.  
  37. $zip = new ZipArchive;
  38.  
  39. $res = $zip->open($new_zip);
  40. if ($res === TRUE) {
  41.   write_log("File $new_zip OPENED\r\n");
  42.     }
  43. else {
  44.     write_log("Could not open $new_zip\r\n Error # $res\r\n");
  45.     exit;
  46.     }
  47.  
  48. if ($zip->extractTo($file_dir) === TRUE) {
  49.   write_log("Extracted: $new_zip\r\n");
  50.   $zip->close();
  51.     } 
  52. else {
  53.   write_log("Extraction error\r\n");
  54.   }
  55.  
  56. $zipSS = $zip->getStatusString();        
  57. write_log("StatusString: $zipSS\r\n");
  58.  
  59.  
  60. $fin = $zip->close();
  61. if ($fin === TRUE) {
  62.   write_log("Unzipped file to: $file_dir\r\n");
  63.   } 
  64. else {
  65.   write_log("Error on close: $fin \r\n");
  66.     exit;  
  67.     } 
  68. exit;  
  69.  
And YES the file "aaa_new.zip" does get created on
my server.


This is the log output:

Class ZipArchive OK
Created zip file: /home/gudfrt/public_html/stres/aaa_new.zip
Found: /home/gudfrt/public_html/stres/aaa_new.zip
File /home/gudfrt/public_html/stres/aaa_new.zip OPENED
Extracted:/home/gudfrt/public_html/stres/aaa_new.zip
StatusString:
Error on close:

Any ideas ?
Mar 1 '10 #1
8 8953
Atli
5,058 Expert 4TB
Hey.

A couple of things you should check out.
  • On lines #14 - #18, instead of testing whether the close() method returns true, check whether the addFromString() method returns true. - The way you have it now it doesn't really verify that the data was added to the zip, only that the zip object was closed properly. (Same goes for lines #60 - #68)
  • Line #29. That IF statement doesn't really verify that the file was created, it only verifies that the variable containing the location exists and is not empty. - You should use something like file_exists() to check if the file actually exists.
  • On line #50 you close the $zip object. Which means that if the zip is extracted OK, the code on lines #60 - #68 always return that there was an error; the $zip is already closed, so checking if it can be closed again will fail. - I would recommend just removing lines #60 - #68 altogether. Line #48 already checks whether the file was extracted or not. They are kind of redundant.
Mar 1 '10 #2
numberwhun
3,509 Expert Mod 2GB
My recommendation would be to implement the use of "$php_errormsg".

You can test it immediately after you do something like the close and see if it is populated with an error message. If so, print it out.

Try using that and see if you can determine why the error printed out.

Regards,

Jeff
Mar 1 '10 #3
jeddiki
290 100+
Thanks to both of you for your advice :)

Although these situations are a real pain, I usually learn something new and useful :)

I made the change to my php.ini file so I can use
$php_errormsg. I will write it to my log file.

At the same time I specified a text fle for php to
write errors to:

; Log errors to specified file.
error_log = my_new_log_file_path./errorlog.txt

I think that will help.

Errors are recorded in my server log, but this will be quicker to look at.

I am continuing to work on the problem ...

BTW - I noticed that I am on PHP version 5.2.10
which is odd because I thought I was on 5.2.9
I checked it two weeks ago.

I don't know how it got updated !!!
Mar 2 '10 #4
jeddiki
290 100+
OK - I have incorporated the suggestions from
both Atli and numberwhun ( thanks again ! )

Here is the new script. (below )

Unfortunately, I can not see all the wonderful
error catching code in action because the script insists on working !

My log shows this:

New record - Time Stamp: 01:00:15 Tuesday, 2 March 2010
This script: sys/auto_cb_update.php, This file: /home/guru54gt5/public_html/sys/a_log_cb_update.txt
Class ZipArchive OK
Added zip info: /home/guru54gt5/public_html/sys/aaa_new.zip
Created zip file: /home/guru54gt5/public_html/sys/aaa_new.zip
Found: /home/guru54gt5/public_html/sys/aaa_new.zip Size: 140
File /home/guru54gt5/public_html/sys/aaa_new.zip OPENED
Extracted: /home/guru54gt5/public_html/sys/aaa_new.zip
StatusString: No error
Unzipped file to: /home/guru54gt5/public_html/sys/

BUT I do have a question .. I do not seem to have given
the script a file name for the extracted data to be stored in.

So although it is Extracted - where would it put it ?

I give it the zip file name but I don't think I gave it a file name
inside the zip - I just added some data.

Where in my script should I be giving the file name ?


Here is the script with the changes made.


Expand|Select|Wrap|Line Numbers
  1. $new_zip = $file_dir.'aaa_new.zip';
  2.  
  3. if (!class_exists('ZipArchive')) {
  4.    write_log("Class ZipArchive not found\r\n"); 
  5.    exit;
  6.      } 
  7. else {
  8.   write_log("Class ZipArchive OK\r\n"); 
  9.   $zip = new ZipArchive;
  10. }
  11.  
  12. $res = $zip->open($new_zip, ZipArchive::CREATE);
  13. if ($res === TRUE) {
  14.   $add = $zip->addFromString('data1.txt', 'file content goes here');
  15.   if ($add === TRUE) {
  16.      write_log("Added zip info: $new_zip\r\n");
  17.          }
  18.     else {
  19.      write_log("Error on close: $add \r\n Error: $php_errormsg \r\n");
  20.        exit;  
  21.          }   
  22.   $fin = $zip->close();
  23.     if ($fin === TRUE) {
  24.      write_log("Created zip file: $new_zip\r\n");
  25.      } 
  26.   else {
  27.     write_log("Error on close: $fin \r\n Error: $php_errormsg \r\n");
  28.       exit;  
  29.       }   
  30.   }
  31. else {
  32.     write_log("Could NOT Create zip file $res\r\n Error: $php_errormsg \r\n");
  33. }
  34.  
  35.  
  36. if(file_exists($new_zip)) {
  37.   $fsz = filesize($new_zip);
  38.      write_log("Found: $new_zip Size: $fsz\r\n");
  39.   }
  40. else {
  41.    write_log("Could not find $new_zip\r\n Error: $php_errormsg \r\n");
  42.     }
  43.  
  44. $zip = new ZipArchive;
  45.  
  46. $res = $zip->open($new_zip);
  47. if ($res === TRUE) {
  48.   write_log("File $new_zip OPENED\r\n");
  49.     }
  50. else {
  51.     write_log("Could not open $new_zip\r\n Error # $res\r\n Error: $php_errormsg \r\n");
  52.     exit;
  53.     }
  54.  
  55. if ($zip->extractTo($file_dir) === TRUE) {
  56.   write_log("Extracted: $new_zip\r\n");
  57.     } 
  58. else {
  59.   write_log("Extraction error\r\n Error: $php_errormsg \r\n");
  60.   }
  61.  
  62. $zipSS = $zip->getStatusString();        
  63. write_log("StatusString: $zipSS\r\n");
  64.  
  65. $fin = $zip->close();
  66. if ($fin === TRUE) {
  67.   write_log("Unzipped file to: $file_dir\r\n");
  68.   } 
  69. else {
  70.   write_log("Error on close: $fin \r\n Error: $php_errormsg \r\n");
  71.     exit;  
  72.     } 
  73.  
  74. exit;  
  75.  
Any suggestions accepted ! :)


.
Mar 2 '10 #5
jeddiki
290 100+
I have applied all the suggestions and yet
my script that used to work every day now doesn't work.

I have even gone and downloaded a backup of the script from a week ago
and ran that .. it failed with the same error ( and yet the log for that day
showed that it succeeded at that time).

Now I have noticed something interesting.

If I break the script into two parts and do the download first. then exit.
Then afterwards, if I run the ZipArchive on the downloaded file, then it will work.

It just won't work if I run the commands in the same script.
(even though it did it fine before last Friday ).

I do an fclose() on the file handle , so it should be OK

Maybe I have not done it properly ?

This is my code:
Expand|Select|Wrap|Line Numbers
  1. $file_dir = '/home/mysite/public_html/sys/';
  2. $file_zip = $file_dir.'a_zip_file.zip'; 
  3.  
  4. $target_url = "https://example.com/get_xml_zip.php";
  5. $userAgent = 'EasyDL/3.xx';
  6. $cef = "curl_err_pdc.txt"; 
  7. $ceh = fopen($cef, 'w');
  8.  
  9. write_log("Target_url: $target_url\r\n");
  10.  
  11. // make the cURL request to $target_url
  12. $ch = curl_init();
  13. $fp = fopen("$file_zip", "wb");
  14.  
  15. if ($fp === FALSE ) { 
  16.   write_log("Problem opening $file_zip\r\n");
  17.     exit;
  18. }
  19.  
  20. curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
  21. curl_setopt($ch, CURLOPT_URL,$target_url);
  22. curl_setopt($ch, CURLOPT_FAILONERROR, true);
  23. curl_setopt($ch, CURLOPT_STDERR, $ceh);        
  24. curl_setopt($ch, CURLOPT_VERBOSE, 1);
  25. curl_setopt($ch, CURLOPT_HEADER,0); 
  26. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  27. curl_setopt($ch, CURLOPT_AUTOREFERER, true);
  28. curl_setopt($ch, CURLOPT_BINARYTRANSFER,true);
  29. curl_setopt($ch, CURLOPT_TIMEOUT, 60);
  30. curl_setopt($ch, CURLOPT_FILE, $fp);
  31.  
  32. $output = curl_exec($ch);
  33. $info = curl_getinfo($ch);
  34.  
  35. if ($output === FALSE || $info['http_code'] != 200) {
  36.   write_log("No cURL data returned for $target_url [". $info['http_code']. "]\r\n ");
  37.   if (curl_error($ch)) write_log($output."CURL error number: curl_errno($ch)\r\n CURL error: curl_error($ch)\r\n");
  38.     exit;
  39.   }
  40. else {
  41.   write_log("CURL completed successfully.\r\n");
  42.     }
  43.  
  44. curl_close($ch);
  45.  
  46. fclose($fp);
  47. fclose($ceh);
  48.  
  49.  
  50. //  IF I SPLIT HERE, IT WORKS
  51.  
  52.  
  53. if(file_exists($file_zip)) {
  54.   $fsz = filesize($file_zip);
  55.      write_log("Found: $file_zip Size: $fsz\r\n");
  56.   }
  57. else {
  58.   write_log("Could not find $file_zip\r\n Error: $php_errormsg");
  59.   }
  60.  
  61. if (!class_exists('ZipArchive')) {
  62.    write_log("Class ZipArchive not found\r\n"); 
  63.    exit;
  64.      } 
  65. else {
  66.   write_log("Class ZipArchive OK\r\n"); 
  67.   $zip = new ZipArchive;
  68. }
  69.  
  70. $res = $zip->open($file_zip);
  71. if ($res === TRUE) {
  72.   write_log("File $file_zip OPENED\r\n");
  73.     }
  74. else {
  75.     write_log("Could not open $file_zip \r\n Error # $res\r\n Error: $php_errormsg");
  76.     exit;
  77.     }
  78.  
  79. $extrt = $zip->extractTo($file_dir);
  80. if ( $extrt === TRUE) {
  81.   write_log("Extracted: $file_zip\r\n");
  82.     } 
  83. else {
  84.   write_log("Extraction error: $extrt\r\n Error: $php_errormsg");
  85.   }
  86.  
  87. $zipSS = $zip->getStatusString();        
  88. write_log("StatusString: $zipSS\r\n");
  89.  
  90. $fin = $zip->close();
  91. if ($fin === TRUE) {
  92.   write_log("Unzipped file to: $file_dir\r\n");
  93.   } 
  94. else {
  95.   write_log("Error on close: $fin \r\n");
  96.     exit;  
  97.     } 
  98.  

When I try and run this in one go I get this output:

CURL completed successfully.
Found: '/home/mysite/public_html/sys/a_zip_file.zip Size: 282624
Class ZipArchive OK
Could not open /home/mysite/public_html/sys/a_zip_file.zip
Error # 19
Error:
BUT if I split the script after the download it will work.

I mean BOTH parts will work, the downlaod AND the Zip Extraction.

Can anyone see where I have gone wrong ?


.
Mar 3 '10 #6
Atli
5,058 Expert 4TB
Ok, if both parts work separately, then it is fair to assume the problem isn't in the code itself. - Try to add a sleep(1) call between the two parts. See if that does anything. Perhaps the OS is trying to move the file, or something, that is causing a delay in the file being created and it being available for usage.

If not, try to figure out exactly why the file is failing to open. Do something like:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $zip = new ZipArchive;
  3. $res = $zip->open('test.zip');
  4. if ($res === TRUE)
  5. {
  6.     echo 'ok';
  7.     $zip->extractTo('test');
  8.     $zip->close();
  9. }
  10. else
  11. {
  12.     $error_values = array
  13.     (
  14.         ZIPARCHIVE::ER_EXISTS   => 'ER_EXISTS',
  15.         ZIPARCHIVE::ER_INCONS   => 'ER_INCONS',
  16.         ZIPARCHIVE::ER_INVAL    => 'ER_INVAL',
  17.         ZIPARCHIVE::ER_MEMORY   => 'ER_MEMORY',
  18.         ZIPARCHIVE::ER_NOENT    => 'ER_NOENT',
  19.         ZIPARCHIVE::ER_NOZIP    => 'ER_NOZIP',
  20.         ZIPARCHIVE::ER_OPEN     => 'ER_OPEN',
  21.         ZIPARCHIVE::ER_READ     => 'ER_READ',
  22.         ZIPARCHIVE::ER_SEEK     => 'ER_SEEK'
  23.     );
  24.     echo 'failed, code: ' . $error_values[$res];
  25. }
  26. ?>
That will at last tell you why this is happening.
Mar 3 '10 #7
Atli
5,058 Expert 4TB
One idea, though...

Try the following:
Expand|Select|Wrap|Line Numbers
  1. // Comment out this line (#30)
  2. #curl_setopt($ch, CURLOPT_FILE, $fp);
  3.  
  4. // Add this Curl option
  5. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  6.  
  7. // Add this after line #41
  8. fwrite($fp, $output, strlen($output));
  9.  
This, rather than having Curl write the data to the file, has Curl return the data to PHP, which then writes the file the "old-fashion" way.
Mar 3 '10 #8
jeddiki
290 100+
Hi again.

I am going to do what you suggest, just to see what it says but
during that last hour I have been looking through my logs (yum log)
and I found that on 27th my php version changed to php-5.2.10

That co-incides with the last date my script ran properly.

I have also found a file path bug report for php-5.2.10

I updated recently to php-5.2.10 and some scripts are not working anymore. The scripts added some elements to the include_path. While including some files from the include_path php echos the error that it cannot resolve the host name. I think that this error is caused by the change, that stream wrappers could be used in the include path and the PATH_SEPERATOR and the stream wrappers collidate.

Downgrading to php-5.2.9-r2 solves the problem again.
So I think I will downgrade and see if that fixes it !

I will also make sure any "automatic" updates to php are turned off cos I didn't do the update !!

Although I have learned a bit more about error testing, - I lost three/four days of forward movement on my project because of this !

Thanks for your help, I appreciate your insights :)


Added:

OK ran that change and I got this output:

Class ZipArchive OK
Could not open /home/guru54gt5/public_html/sys/a_zip_file.zip
Error # 19
Error:
Failed, code: ER_NOZIP
Mar 3 '10 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Chris | last post by:
Hi Where can I find info on unzipping file with VB.NET. I need to unzip a winzip file with my application Thanks
10
by: Yogi_Bear_79 | last post by:
pardon my ignorance as I am a self-taught hobbyist programmer. I am curious after reading up on SharpZipLib. Can I embed a zipped txt file in my program? Then either read from within the zip...
2
by: Bob Achgill | last post by:
I want to let the users of my Windows form press a button to download (from the Internet) and unpack a revised form and supporting data files. It looks like myWebClient.DownloadFile under class...
1
by: Jean Christophe Avard | last post by:
Hi! Finally I figure out what was wrong... "objZipEntry.size = strmFile" I wasn't giving the right file length... But now, I have issue with the unzip function... I can't unzip it, the unzip function...
4
by: DataSmash | last post by:
I need to unzip all zip file(s) in the current directory into their own subdirectories. The zip file name(s) always start with the string "usa" and end with ".zip". The code below will make the...
3
by: Skeleton Man | last post by:
Hi, I'm running IIS with PHP 4.3.11 and I'm trying to get zip support working correctly. I have installed the appropriate dll, it shows zip support in phpinfo() and the basic...
0
by: Ryan van Roode | last post by:
Hello, Is there a way to use php's ZipArchive and specify No Compression? Something similar to "zip -0" on the command line. Thanks very much. ~rvr
5
by: =?Utf-8?B?anVsaW8=?= | last post by:
Hi, I write a program to unzip a Tar file generated on a Unix environment file using SharpZipLib, but returns a error "Header checksum is invalid" when execute the program. This error appears...
1
by: olddocks | last post by:
I want to upload a zip file and then extract/unzip it. I am accomplishing this with php exec command. I am calling unzip from php exec command within a php script and it is not extracting files. why?...
2
by: somsub | last post by:
Hi all, Here is my samle code use strict ; use warnings ; use IO::Uncompress::Unzip ; When I compiled this three lines of code in win32 I got error like below.
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.