You are here

function epub_unzip in Epub 8

Same name and namespace in other branches
  1. 7 epub.module \epub_unzip()
1 call to epub_unzip()
epub_file_update in ./epub.module
Implements hook_file_update().

File

./epub.module, line 91

Code

function epub_unzip($filepath, $destination) {
  $destination .= '/';
  $extracted = array();
  if ($z = zip_open($filepath)) {
    while ($entry = zip_read($z)) {
      if (zip_entry_open($z, $entry, 'r') && ($zip_entry_filesize = zip_entry_filesize($entry))) {
        $entry_name = zip_entry_name($entry);
        $data = zip_entry_read($entry, $zip_entry_filesize);
        $filepath = $destination . $entry_name;
        $parent_dir = dirname($filepath);
        if (!file_exists($parent_dir)) {
          file_prepare_directory($parent_dir, FILE_CREATE_DIRECTORY);
        }
        if ($filepath = file_unmanaged_save_data($data, $filepath, FILE_EXISTS_REPLACE)) {
          $file = new StdClass();
          $file->filename = $entry_name;
          $file->filemime = \Drupal::service('file.mime_type.guesser')
            ->guess($filepath);
          $file->filesize = filesize($filepath);
          $extracted[] = $file;
        }
        zip_entry_close($entry);
      }
    }
    zip_close($z);
  }
  return $extracted;
}