public function testgetHumanReadableFolderSize() { $newDir = dirname(__FILE__) . '/new'; mkdir($newDir, 0777); $result = Folder::size($newDir, true, 2); $this->assertEquals('0 B', $result); rmdir($newDir); }
/** * @param string $filePath * @param string $destinationPath * @param bool $overwrite * @return bool * @throws Exceptions\ZipException */ public static function unzip($filePath, $destinationPath, $overwrite = false) { $destinationPath = $destinationPath . DIRECTORY_SEPARATOR; if (!file_exists($filePath) || !is_file($filePath)) { throw new \Sonrisa\Component\FileSystem\Exceptions\ZipException("File {$filePath} does not exist therefore it cannot be unzipped."); } if (!file_exists($destinationPath) || !is_dir($destinationPath)) { throw new \Sonrisa\Component\FileSystem\Exceptions\ZipException("Destination folder {$destinationPath} does not exist."); } $unzipPath = $filePath; if (file_exists($unzipPath) && $overwrite == false) { throw new \Sonrisa\Component\FileSystem\Exceptions\ZipException("Cannot unzip {$unzipPath} because it already exists a file or folder with the same name."); } $open = zip_open($unzipPath); if (is_resource($open)) { //create the Folder object because we'll be using it a couple of times. //If output directory does not exist, try creating it. if (!file_exists($destinationPath)) { try { Folder::create($destinationPath); } catch (\Sonrisa\Component\FileSystem\Exceptions\FolderException $e) { throw new \Sonrisa\Component\FileSystem\Exceptions\ZipException($e->getMessage()); } } //Now extract files in the destinationPath $_tmp = array(); while ($zip_entry = zip_read($open)) { //Read all data for each file $_tmp["filename"] = $destinationPath . DIRECTORY_SEPARATOR . zip_entry_name($zip_entry); $_tmp["stored_filename"] = $destinationPath . DIRECTORY_SEPARATOR . zip_entry_name($zip_entry); $_tmp["folder"] = $destinationPath . DIRECTORY_SEPARATOR . dirname(zip_entry_name($zip_entry)); $_tmp["size"] = zip_entry_filesize($zip_entry); //Fetch entry if (zip_entry_open($open, $zip_entry, "r")) { if (substr($_tmp["filename"], -1) == DIRECTORY_SEPARATOR) { //is dir if (!file_exists($_tmp["filename"])) { $result = mkdir($_tmp["filename"], 0777, true); if ($result == false) { throw new \Sonrisa\Component\FileSystem\Exceptions\ZipException("Could not create directory {$_tmp["filename"]}."); } } } else { //Read the file $buffer = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry)); file_put_contents($_tmp["stored_filename"], $buffer); } //Fetch the next zip entry. zip_entry_close($zip_entry); } else { throw new \Sonrisa\Component\FileSystem\Exceptions\ZipException("An error occurred while extracting {$_tmp["stored_filename"]}."); } } return true; } else { return false; } }