-1

When I tried to extract a zip file downloaded, it does'nt work. How to identifed the error ?

the response is failed;

Thank you.

File location //home/www/boutique/includes/ClicShopping/Work/IceCat/daily.index.xml.gz

     public function ExtractZip() {

  if (is_file($this->selectFile())) {
    $zip = new \ZipArchive;

    if ($zip->open($this->selectFile()) === true) {

      $zip->extractTo($this->IceCatDirectory);
      $zip->close();

      echo 'file downloaded an unzipped';
    }
  } else {
    echo 'error no file found in ' . $this->selectFile();
  }
}
yokogeri
  • 1,217
  • 2
  • 8
  • 11
  • `if ($icecat_file === true) { $zip->extractTo($icecat_file);` -> this does not look right. Why would you extract the archive to `true` instead of to a destination folder? – rickdenhaan Oct 16 '17 at 20:16
  • Sorry, it's an error of mine, I will rectifiied the code. The problem is the same with this correction – yokogeri Oct 16 '17 at 20:23
  • True. If `$icecat_file` is *not* `true`, it will be [an error code](http://php.net/manual/en/ziparchive.open.php#refsect1-ziparchive.open-returnvalues). You can check the error code to see why the open failed. – rickdenhaan Oct 16 '17 at 20:25
  • Oups: the error is : ZipArchive::close(): Invalid or uninitialized Zip object in /home/... – yokogeri Oct 16 '17 at 20:28
  • ...you've completely removed the check to see if opening the file was successful, which was the original problem. – rickdenhaan Oct 16 '17 at 20:29
  • You have to choose a directory to extract to: `$zip->extractTo('/some/place/');` You have already opened the file `$zip->open($this->selectFile());`, you just need a destination directory: http://php.net/manual/en/ziparchive.extractto.php Look at the manual carefully. – Rasclatt Oct 16 '17 at 20:33
  • Just to extract the files inside the zip – yokogeri Oct 16 '17 at 20:34
  • Extract where? You want to extract the zip into itself? Is that what you are saying? You have to have a source `/source/file.zip` that extracts all the contained files into a new spot `/somedirectory/save/filesfrom/zip/into/directory/`. – Rasclatt Oct 16 '17 at 20:35
  • ok I changed (I think it's better) the code and error, but when I look the directory, nithing is appen. – yokogeri Oct 16 '17 at 20:38
  • Make sure you do a directory check `if(!is_dir($this->IceCatDirectory)) die('folder doesn\'t exist');` before you save into it. – Rasclatt Oct 16 '17 at 20:40
  • I havent't pb with that but I have now this : ZipArchive::extractTo(): Invalid or uninitialized Zip – yokogeri Oct 16 '17 at 20:41
  • The problem is probably still the same as in the first version of your code: you're getting an error *opening* the zip file, and you need to check the result of `$zip->open()` to find out why the file can't be opened. – rickdenhaan Oct 16 '17 at 20:42
  • var_dump($this->selectFile()); var_dump($zip->open($this->selectFile())); the result is : /home/www/boutique/includes/Admin/Work/IceCat/daily.index.xml.gz" and int(19) – yokogeri Oct 16 '17 at 20:48
  • Error code 19 is `ZipArchive::ER_NOZIP` -- which is correct, because a `.gz` file is not a ZIP file (it is a Gzip file, which ZipArchive cannot open) – rickdenhaan Oct 16 '17 at 20:49
  • Ok, how to do that ? – yokogeri Oct 16 '17 at 20:51
  • Possible duplicate of [How can I unzip a .gz file with PHP?](https://stackoverflow.com/questions/3293121/how-can-i-unzip-a-gz-file-with-php) – rickdenhaan Oct 16 '17 at 20:53
  • Tk, find the solution – yokogeri Oct 16 '17 at 21:02

1 Answers1

0

Follow to comment, there the correct function

    public function ExtractGzip() {

// Raising this value may increase performance
      $buffer_size = 4096; // read 4kb at a time
      $out_file_name = str_replace('.gz', '', $this->selectFile());

// Open our files (in binary mode)
      $file = gzopen($this->selectFile(), 'rb');
      $out_file = fopen($out_file_name, 'wb');

// Keep repeating until the end of the input file
      while(!gzeof($file)) {
        // Read buffer-size bytes
        // Both fwrite and gzread and binary-safe
        fwrite($out_file, gzread($file, $buffer_size));
      }

// Files are done, close files
      fclose($out_file);
      gzclose($file);
    }
yokogeri
  • 1,217
  • 2
  • 8
  • 11