/** * Zip files and store the result in os file * @param array $files array with zip paths as keys (archivepath=>ospathname or archivepath=>stored_file) * @param string $archivefile path to target zip file * @return bool success */ public function archive_to_pathname($files, $archivefile) { global $CFG; if (!is_array($files)) { return false; } $ziparch = new zip_archive(); if (!$ziparch->open($archivefile, file_archive::OVERWRITE)) { return false; } foreach ($files as $archivepath => $file) { $archivepath = trim($archivepath, '/'); if (is_null($file)) { // empty directories have null as content $ziparch->add_directory($archivepath . '/'); } else { if (is_string($file)) { $this->archive_pathname($ziparch, $archivepath, $file); } else { $this->archive_stored($ziparch, $archivepath, $file); } } } return $ziparch->close(); }
/** * Perform archiving file from file path. * * @param zip_archive $ziparch zip archive instance * @param string $archivepath file path to archive * @param string $file path name of the file * @param file_progress $progress Progress indicator callback or null if not required * @return bool success */ private function archive_pathname($ziparch, $archivepath, $file, file_progress $progress = null) { // Record progress each time this function is called. if ($progress) { $progress->progress(); } if (!file_exists($file)) { return false; } if (is_file($file)) { if (!is_readable($file)) { return false; } return $ziparch->add_file_from_pathname($archivepath, $file); } if (is_dir($file)) { if ($archivepath !== '') { $ziparch->add_directory($archivepath); } $files = new DirectoryIterator($file); foreach ($files as $file) { if ($file->isDot()) { continue; } $newpath = $archivepath . '/' . $file->getFilename(); $this->archive_pathname($ziparch, $newpath, $file->getPathname(), $progress); } unset($files); // Release file handles. return true; } }
/** * @depends test_extract_to_storage */ public function test_add_files() { global $CFG; $this->resetAfterTest(false); $packer = get_file_packer('application/zip'); $archive = "{$CFG->tempdir}/archive.zip"; $this->assertFileNotExists($archive); $packer->archive_to_pathname(array(), $archive); $this->assertFileExists($archive); $zip_archive = new zip_archive(); $zip_archive->open($archive, file_archive::OPEN); $this->assertEquals(0, $zip_archive->count()); $zip_archive->add_file_from_string('test.txt', 'test'); $zip_archive->close(); $zip_archive->open($archive, file_archive::OPEN); $this->assertEquals(1, $zip_archive->count()); $zip_archive->add_directory('test2'); $zip_archive->close(); $zip_archive->open($archive, file_archive::OPEN); $files = $zip_archive->list_files(); $this->assertCount(2, $files); $this->assertEquals('test.txt', $files[0]->pathname); $this->assertEquals('test2/', $files[1]->pathname); $result = $zip_archive->add_file_from_pathname('test.txt', __DIR__ . '/nonexistent/file.txt'); $this->assertFalse($result); $zip_archive->close(); $zip_archive->open($archive, file_archive::OPEN); $this->assertEquals(2, $zip_archive->count()); $zip_archive->close(); unlink($archive); }
/** * Perform archiving file from file path * * @param zip_archive $ziparch zip archive instance * @param string $archivepath file path to archive * @param string $file path name of the file * @return bool success */ private function archive_pathname($ziparch, $archivepath, $file) { if (!file_exists($file)) { return false; } if (is_file($file)) { if (!is_readable($file)) { return false; } return $ziparch->add_file_from_pathname($archivepath, $file); } if (is_dir($file)) { if ($archivepath !== '') { $ziparch->add_directory($archivepath); } $files = new DirectoryIterator($file); foreach ($files as $file) { if ($file->isDot()) { continue; } $newpath = $archivepath . '/' . $file->getFilename(); $this->archive_pathname($ziparch, $newpath, $file->getPathname()); } unset($files); //release file handles return true; } }