/** * Open Archive * * @return RarArchive */ private function _open() { if ($this->_archive === null) { $this->_archive = RarArchive::open(Config::get('Gmanager', 'mode') == 'FTP' ? Gmanager::getInstance()->ftpArchiveStart($this->_name) : IOWrapper::set($this->_name)); } return $this->_archive; }
public function __construct(\Path $path) { $this->archive = \RarArchive::open($path->getPathName()); if (!$this->archive) { throw new Exception('Failed to open archive'); } }
/** * Instantiate an archive adapter object * * @param \Pop\Archive\Archive $archive * @param string $password * @throws Exception * @return Rar */ public function __construct(\Pop\Archive\Archive $archive, $password = null) { $this->path = $archive->getFullpath(); $this->password = $password; if (file_exists($this->path)) { $this->archive = \RarArchive::open($this->path, $this->password); } else { throw new Exception('Due to licensing restrictions, RAR files cannot be created and can only be extracted.'); } }
private function invokeRarFile() { if (extension_loaded('rar') && class_exists('RarArchive')) { $rar = RarArchive::open($this->file->getAbsPath()); foreach ($rar->getEntries() as $key => $value) { $files[$value->getName()] = array('CompressedSize' => Utils::formatBytes($value->getPackedSize()), 'UncompressedSize' => Utils::formatBytes($value->getUnpackedSize()), 'Encrypted' => $value->isEncrypted()); } $rar->close(); } else { $this->smarty->display('handler/default.tpl'); exit; } $this->smarty->assign("files", $files); }
/** * {@inheritdoc} */ public function extract($file, $target, Format\FormatInterface $format) { $this->checkSupport($format); $rar = @\RarArchive::open($file); if (false === $rar) { throw new FileCorruptedException($file, FileCorruptedException::SEVERITY_HIGH); } $this->getFilesystem()->mkdir($target); foreach ($rar->getEntries() as $entry) { $entry->extract($target); } $rar->close(); return true; }
/** * Return a code to know status * 0 = No problem * 1 = One file on extracted archive maybe corrupted * 2 = One file is not extracted * 3 = Archive can't be open * @param string $path * @return int */ public function isAlreadyExtracted($path) { $archive = \RarArchive::open($this->archiveFile); $entries = $archive->getEntries(); foreach ($entries as $entry) { if (!$this->fs->exists($path . $entry->getName())) { return 2; } if (!$entry->isDirectory() && filesize($path . $entry->getName()) !== $entry->getUnpackedSize()) { return 1; } } return 0; }
/** * {@inheritdoc} */ public function extract($file, $target, Format\FormatInterface $format) { $this->checkSupport($format); $rar = @\RarArchive::open($file); if (false === $rar) { return false; } $this->getFilesystem()->mkdir($target); foreach ($rar->getEntries() as $entry) { $entry->extract($target); } $rar->close(); return true; }
function doUnrar($path) { $rarName = basename($path); $dirName = nameWithoutExtension($rarName); $parentPath = dirname($path); $rar = RarArchive::open($path); if ($rarName != $dirName) { $destination = $parentPath . DIRECTORY_SEPARATOR . $dirName; if (!file_exists($destination)) { mkdir($destination); foreach ($rar->getEntries() as $entry) { $entry->extract($destination); } } else { $warning = "Impossible de décompresser l'archive <b>" . $rarName . "</b>, un dossier du même nom existe déjà."; } } else { $warning = "Impossible de traiter le fichier <b>" . $rarName . "</b>, il n'a pas d'extension."; } $rar->close(); }
/** * Extract rar archive to targeted path * * @param $source_file * @param $target_path * @param array $forbidden_directories * @param bool $files_only * @param bool $remove_archive * * @return array|bool */ public static function extractRar($source_file, $target_path = '.', $forbidden_directories = [], $files_only = true, $remove_archive = false) { $ext = self::getNormalizedExtension($source_file); if ($ext !== 'rar') { return false; } if (self::isPHPExtensionLoaded($ext)) { $rar = \RarArchive::open($source_file); $dirs = []; $files = []; foreach ($rar->getEntries() as $e) { // Checks if file is not in a forbidden directory foreach ($forbidden_directories as $fd) { if (false !== stripos($e->getName(), $fd)) { continue 2; } } $last_char = substr($target_path, -1); $target_path = $last_char == DIRECTORY_SEPARATOR ? substr($target_path, 0, -1) : $target_path; $current_path = $target_path . DIRECTORY_SEPARATOR . $e->getName(); $filename = basename($e->getName()); $file_ext = strrchr($filename, '.'); $final_filename = self::transliterator()->transliterate(substr($filename, 0, -strlen($file_ext)), '_') . $file_ext; if (!empty($final_filename) && !$e->isDirectory()) { if ($files_only) { if (strpos($e->getName(), DIRECTORY_SEPARATOR)) { $dir = strstr($e->getName(), DIRECTORY_SEPARATOR, true); if (!in_array($dir, $dirs, false)) { $dirs[] = $dir; } $final_path = $target_path . DIRECTORY_SEPARATOR . $final_filename; } else { $final_path = str_replace($filename, $final_filename, $current_path); } } else { $final_path = str_replace($filename, $final_filename, $current_path); } if (!in_array($final_path, $files, false)) { $e->extract('', $final_path); $files[] = $final_path; } } } $rar->close(); if ($remove_archive) { unlink($source_file); } return $files; } else { // Use Linux command if extension RAR is not loaded in PHP if (`command -v unrar`) { $options = '-yrep'; // Default options: // y = Yes to all queries. // r = Recurse subdirectories. // ep = Exclude paths from names. $mode = $files_only ? 'e' : 'x'; // Excluded directories $toExclude = ''; foreach ($forbidden_directories as $fd) { if ('/' === $fd[strlen($fd) - 1]) { $fd = substr($fd, 0, -1); } $toExclude .= "-x'*{$fd}/' "; } substr($toExclude, 0, -1); // remove whitespace // Normalize target_path if ('/' !== $target_path[strlen($target_path) - 1]) { $target_path .= '/'; } $command = "unrar {$mode} {$options} " . escapeshellarg($source_file) . " {$toExclude} " . escapeshellarg($target_path); putenv('LANG=fr_FR.UTF-8'); // fix issues with french accents. shell_exec($command); if ($remove_archive) { unlink($source_file); } return self::getFilesFromDir($target_path); } else { return false; } } }
<?php if (!array_key_exists("i", $_GET) || !is_numeric($_GET["i"])) { die("Index unspecified or non-numeric"); } $index = (int) $_GET["i"]; $arch = RarArchive::open("example.rar"); if ($arch === FALSE) { die("Cannot open example.rar"); } $entries = $arch->getEntries(); if ($entries === FALSE) { die("Cannot retrieve entries"); } if (!array_key_exists($index, $entries)) { die("No such index: {$index}"); } $orfilename = $entries[$index]->getName(); //UTF-8 encoded $filesize = $entries[$index]->getUnpackedSize(); /* you could check HTTP_IF_MODIFIED_SINCE here and compare with * $entries[$index]->getFileTime(). You could also send a * "Last modified" header */ $fp = $entries[$index]->getStream(); if ($fp === FALSE) { die("Cannot open file with index {$index} insided the archive."); } $arch->close(); //no longer needed; stream is independent function detectUserAgent() {
function process_rar($file_path) { debug("process_rar: " . $file_path); # to support aicromics, the new client app if (end_with($file_path, "/")) { $file_path = substr($file_path, 0, -1); debug("process_rar: " . $file_path); } $arch = RarArchive::open($file_path); $entries = $arch->getEntries(); debug(count($entries)); for ($i = 0; $i < count($entries); $i++) { $entry_name = $entries[$i]->getName(); debug("entry_name: " . $entry_name); if (is_support($entry_name, false)) { echo "{$entry_name}\n"; debug(""); } } $arch->close(); }
function is_rar($filePath) { if (class_exists('RarArchive')) { $rar = RarArchive::open($filePath); $isRar = $rar !== FALSE; if ($isRar) { $rar->close(); } return $isRar; } else { return false; } }
public function __construct($archive_name) { $this->archive = \RarArchive::open($archive_name); $this->archive_name = $archive_name; $this->archive_entries = $this->archive->getEntries(); }
echo '<td style="background-color:#1C1C1C;"></td>'; } } for ($k = 0; $k < $Last; $k++) { if ($ORDedArray[$k] <= 31) { echo '<td>.</td>'; } else { echo '<td>', $File[$i + $k], '</td>'; } } echo '</tr>'; } echo '</table>'; } elseif (!isset($_GET['OpenWith']) && $Ext === 'rar' || isset($_GET['OpenWith']) && $_GET['OpenWith'] === 'rar') { if (class_exists('RarArchive')) { $rar_arch = RarArchive::open($_GET['file']); if ($rar_arch !== FALSE) { $rar_entries = $rar_arch->list(); if ($rar_entries === FALSE) { echo 'Could not retrieve entries.'; } else { echo 'Found ', count($rar_entries), ' entries.<br />'; foreach ($rar_entries as $e) { echo $e, '<br />'; } } $rar_arch->close(); } else { echo 'Could not open RAR archive.'; } } else {
function passImage($archive, $page) { // Local browser cache $lastModified = filemtime($archive); $etagFile = md5_file(__FILE__); $ifModifiedSince = isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) ? $_SERVER['HTTP_IF_MODIFIED_SINCE'] : false; $etagHeader = isset($_SERVER['HTTP_IF_NONE_MATCH']) ? trim($_SERVER['HTTP_IF_NONE_MATCH']) : false; header("Last-Modified: " . gmdate("D, d M Y H:i:s", $lastModified) . " GMT"); header("Etag: {$etagFile}"); header('Cache-Control: public'); header("Expires: " . gmdate('D, d M Y H:i:s', strtotime("+1 year")) . " GMT"); if (@strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == $lastModified || $etagHeader == $etagFile) { header("HTTP/1.1 304 Not Modified"); exit; } if (endsWith($archive, ".cbz")) { $zip = new ZipArchive(); if ($zip->open($archive)) { if (endsWith($page, ".jpg")) { header('Content-type: image/jpeg'); fpassthru($zip->getStream($page)); } else { if (endsWith($page, ".png")) { header('Content-type: image/png'); fpassthru($zip->getStream($page)); } else { if (endsWith($page, ".gif")) { header('Content-type: image/gif'); fpassthru($zip->getStream($page)); } } } $zip->close(); } } else { if (endsWith($archive, ".cbr")) { $rar = RarArchive::open($archive); if ($rar !== false) { $rar_entries = $rar->getEntries(); for ($i = 0; $i < count($rar_entries); $i++) { if ($rar_entries[$i]->getName() == $page) { if (endsWith($page, ".jpg")) { header('Content-type: image/jpeg'); fpassthru($rar_entries[$i]->getStream()); } else { if (endsWith($page, ".png")) { header('Content-type: image/png'); fpassthru($rar_entries[$i]->getStream()); } else { if (endsWith($page, ".gif")) { header('Content-type: image/gif'); fpassthru($rar_entries[$i]->getStream()); } } } $rar->close(); return; } } } } } }
/** * Opens the file as one of supported formats. * * @param string $filename Filename * @param string $type Archive type. */ public function __construct($filename, $type) { $this->type = $type; $this->archiveSize = filesize($filename); switch ($type) { case self::ZIP: $this->zip = new \ZipArchive(); if ($this->zip->open($filename) === true) { for ($i = 0; $i < $this->zip->numFiles; $i++) { $file = $this->zip->statIndex($i); $this->files[$i] = $file['name']; $this->compressedFilesSize += $file['comp_size']; $this->uncompressedFilesSize += $file['size']; } } else { $this->zip->numFiles = $this->compressedFilesSize = $this->uncompressedFilesSize = 0; } break; case self::RAR: $this->rar = \RarArchive::open($filename); $Entries = @$this->rar->getEntries(); if ($Entries === false) { $this->rar->numberOfFiles = $this->compressedFilesSize = $this->uncompressedFilesSize = 0; } else { $this->rar->numberOfFiles = count($Entries); # rude hack foreach ($Entries as $i => $entry) { $this->files[$i] = $entry->getName(); $this->compressedFilesSize += $entry->getPackedSize(); $this->uncompressedFilesSize += $entry->getUnpackedSize(); } } break; case self::TAR: $ext = strtolower(pathinfo($filename, PATHINFO_EXTENSION)); switch ($ext) { case 'gz': $this->tar = new \Archive_Tar($filename, 'gz'); break; case 'bz2': $this->tar = new \Archive_Tar($filename, 'bz2'); break; case 'xz': $this->tar = new \Archive_Tar($filename, 'lzma2'); break; case 'z': $this->tar = new \Archive_Tar('compress.lzw://' . $filename); break; default: $this->tar = new \Archive_Tar($filename); break; } $this->tar->path = $filename; $Content = $this->tar->listContent(); $this->tar->numberOfFiles = count($Content); foreach ($Content as $i => $file) { // BUG workaround: http://pear.php.net/bugs/bug.php?id=20275 if ($file['filename'] == 'pax_global_header') { $this->tar->numberOfFiles--; continue; } $this->files[$i] = $file['filename']; $this->uncompressedFilesSize += $file['size']; } $this->compressedFilesSize = $this->archiveSize; if ($this->uncompressedFilesSize != 0) { $this->tarCompressionRatio = ceil($this->archiveSize / $this->uncompressedFilesSize); } else { $this->tarCompressionRatio = 1; } break; case self::GZIP: $this->files = array(basename($filename, '.gz')); $this->gzipStat = gzip_stat($filename); $this->gzipFilename = $filename; $this->compressedFilesSize = $this->archiveSize; $this->uncompressedFilesSize = $this->gzipStat['size']; break; case self::ISO: // load php-iso-files $this->iso = new \CISOFile(); $this->iso->open($filename); $this->iso->ISOInit(); $size = 0; $usedDesc = $this->iso->GetDescriptor(SUPPLEMENTARY_VOLUME_DESC); if (!$usedDesc) { $usedDesc = $this->iso->GetDescriptor(PRIMARY_VOLUME_DESC); } $this->isoBlockSize = $usedDesc->iBlockSize; $directories = $usedDesc->LoadMPathTable($this->iso); foreach ($directories as $Directory) { $directory = $Directory->GetFullPath($directories, false); $directory = trim($directory, '/'); if ($directory != '') { $directory .= '/'; $this->files[$Directory->Location] = $directory; } $this->isoCatalogsStructure[$Directory->Location] = $directory; $files = $Directory->LoadExtents($this->iso, $usedDesc->iBlockSize, true); if ($files) { foreach ($files as $file) { if (in_array($file->strd_FileId, array('.', '..'))) { continue; } $this->files[$file->Location] = $directory . $file->strd_FileId; $size += $file->DataLen; $this->isoFilesData[$directory . $file->strd_FileId] = array('size' => $file->DataLen, 'mtime' => strtotime((string) $file->isoRecDate)); } } } $this->uncompressedFilesSize = $this->compressedFilesSize = $size; break; } }
function getRarContent($file) { if (!preg_match("/\\.(rar|cbr)\$/", strtolower($file))) { return ""; } $rar = RarArchive::open($file); if (!$rar) { return ""; } $content = ""; $entries = $rar->getEntries(); if ($entries) { $files = array(); foreach ($entries as $entry) { $name = $entry->getName(); if (preg_match("/\\.(png|jpeg|jpg|jpe|gif)\$/", strtolower($name))) { $files[] = $name; } } if (count($files)) { sort($files); $entry = $rar->getEntry($files[0]); if ($entry) { $stream = $entry->getStream(); if ($stream) { $content = stream_get_contents($stream); } } } } $rar->close(); unset($rar); return $content; }
private function Unrar($filepath, $location) { $rar = new RarArchive(); $res = $rar->open($filepath); if ($res === TRUE) { $entries = $rar->getEntries(); foreach ($entries as $entry) { $entry->extract($location); } $rar->close(); return true; } throw new Exception('rarfailed'); }
$handle = fopen('/tmp/co_parl.zip', "w"); fwrite($handle, $bin_file); $zip = zip_open('/tmp/co_parl.zip'); while ($zip_entry = zip_read($zip)) { $buf = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry)); $zip_entry_name = zip_entry_name($zip_entry); $zen_ar = explode('.', $zip_entry_name); $zip_entry_type = end($zen_ar); $data = array('date' => $date, 'type' => $zip_entry_type, 'name' => $zip_entry_name, 'file_encode' => base64_encode($buf)); scraperwiki::save_sqlite(array('date', 'name'), $data, 'file'); } break; case 'rar': $handle = fopen('/tmp/co_parl.rar', "w"); fwrite($handle, $bin_file); $rar = RarArchive::open('/tmp/co_parl.rar'); $rar_list = $rar->getEntries(); foreach ($rar_list as $rar_file) { $ren_ar = explode('.', $rar_file->getName()); $rar_entry_type = end($ren_ar); $entry = rar_entry_get($rar, $rar_file->getName()); $entry->extract('/tmp/' . $rar_file->getName()); $buf = file_get_contents('/tmp/' . $rar_file->getName()); $data = array('date' => $date, 'type' => $rar_entry_type, 'name' => $rar_file->getName(), 'file_encode' => base64_encode($buf)); scraperwiki::save_sqlite(array('date', 'name'), $data, 'file'); } break; default: $data = array('date' => $date, 'type' => $type, 'name' => $title . '.' . $type, 'file_encode' => base64_encode($bin_file)); scraperwiki::save_sqlite(array('date', 'name'), $data, 'file'); break;
function extract_archive($archive_name, $path, $password) { if (!empty($archive_name) && !empty($path)) { $extension = pathinfo($path . $archive_name, PATHINFO_EXTENSION); $array_extension = array("tar" => 4, "gz" => 7, "bz2" => 8, "zip" => 4, "rar" => 4); $accepted_extension = false; $strlen_extension = ""; foreach ($array_extension as $archive_extension => $strlen_archive_extension) { if ($extension === $archive_extension) { $accepted_extension = true; $strlen_extension = $strlen_archive_extension; break; } } if ($accepted_extension === false) { send_json('Extension not supported !!', null); throw new Exception("Extension not supported !!"); } $only_file_name = substr($archive_name, 0, -$strlen_extension); $last_pos_only_file_name = strrpos($only_file_name, "-"); $tmp_only_file_name = substr($only_file_name, 0, $last_pos_only_file_name); $counter_duplicate = substr($only_file_name, $last_pos_only_file_name + 1); if (!is_int($last_pos_only_file_name) || !is_int($counter_duplicate)) { $tmp_only_file_name = $only_file_name; $counter_duplicate = 1; } while (file_exists($path . $only_file_name)) { $only_file_name = $tmp_only_file_name . "-" . $counter_duplicate; $counter_duplicate++; } mkdir($path . $only_file_name); if ($extension === "zip") { try { $zip = zip_open($path . $archive_name); if ($zip) { while ($zip_read = zip_read($zip)) { $zip_entry_name = zip_entry_name($zip_read); if (zip_entry_open($zip, $zip_read)) { $file_in_archive = zip_entry_read($zip_read, zip_entry_filesize($zip_read)); $exploded = explode("/", substr($path . $only_file_name . "/" . zip_entry_name($zip_read), strlen($path . $only_file_name) + 1)); for ($i = 0; $i < count($exploded) - 1; $i++) { if ($i === 0) { $path_after_onyl_file_name = $exploded[0]; } else { $path_after_onyl_file_name = $path_after_onyl_file_name . "/" . $exploded[$i]; } if (!file_exists($path . $only_file_name . "/" . $path_after_onyl_file_name)) { mkdir($path . $only_file_name . "/" . $path_after_onyl_file_name, 0777, true); } } file_put_contents($path . $only_file_name . "/" . zip_entry_name($zip_read), $file_in_archive); } } zip_close($zip); } } catch (Exception $e) { send_json($e->getMessage(), null); throw new Exception($e->getMessage()); } } elseif ($extension === "rar") { if ($password === "") { $rar = RarArchive::open($path . $archive_name); } else { $rar = RarArchive::open($path . $archive_name, $password); } if ($rar->isBroken()) { send_json("Rar archive broken !!", null); throw new Exception("Rar archive broken !!"); } $rar_file = rar_open($path . $archive_name); $list = rar_list($rar_file); foreach ($list as $file) { $entry = rar_entry_get($rar_file, $file->getName()); $entry->extract($path . $only_file_name); } rar_close($rar_file); } else { $archive = new PharData($path . $archive_name); try { $archive->extractTo($path . $only_file_name); } catch (Exception $e) { send_json($e->getMessage(), null); throw new Exception($e->getMessage()); } } send_json(null, $only_file_name); } }