Exemple #1
0
 /**
  * @depends test_get_packer
  */
 public function test_list_files()
 {
     $this->resetAfterTest(false);
     $files = array(__DIR__ . '/fixtures/test_moodle_22.zip', __DIR__ . '/fixtures/test_moodle.zip', __DIR__ . '/fixtures/test_tc_8.zip', __DIR__ . '/fixtures/test_7zip_927.zip', __DIR__ . '/fixtures/test_winzip_165.zip', __DIR__ . '/fixtures/test_winrar_421.zip');
     if (function_exists('normalizer_normalize')) {
         // Unfortunately there is no way to standardise UTF-8 strings without INTL extension
         $files[] = __DIR__ . '/fixtures/test_infozip_3.zip';
         $files[] = __DIR__ . '/fixtures/test_osx_1074.zip';
     }
     $packer = get_file_packer('application/zip');
     foreach ($files as $archive) {
         $archivefiles = $packer->list_files($archive);
         $this->assertTrue(is_array($archivefiles), "Archive not extracted properly: " . basename($archive) . ' ');
         $this->assertTrue(count($this->files) === count($archivefiles) or count($this->files) === count($archivefiles) - 1);
         // Some zippers create empty dirs.
         foreach ($archivefiles as $file) {
             if ($file->pathname === 'Žluťoučký/') {
                 // Some zippers create empty dirs.
                 continue;
             }
             $this->assertArrayHasKey($file->pathname, $this->files, "File {$file->pathname} not extracted properly: " . basename($archive) . ' ');
         }
     }
     // Windows packer supports only DOS encoding
     $archive = __DIR__ . '/fixtures/test_win8_de.zip';
     $archivefiles = $packer->list_files($archive);
     $this->assertTrue(is_array($archivefiles), "Archive not extracted properly: " . basename($archive) . ' ');
     $this->assertEquals(2, count($archivefiles));
     foreach ($archivefiles as $file) {
         $this->assertTrue($file->pathname === 'Prüfung.txt' or $file->pathname === 'test.test');
     }
     $zip_archive = new zip_archive();
     $zip_archive->open(__DIR__ . '/fixtures/test_win8_cz.zip', file_archive::OPEN, 'cp852');
     $archivefiles = $zip_archive->list_files();
     $this->assertTrue(is_array($archivefiles), "Archive not extracted properly: " . basename($archive) . ' ');
     $this->assertEquals(3, count($archivefiles));
     foreach ($archivefiles as $file) {
         $this->assertTrue($file->pathname === 'Žluťoučký/Koníček.txt' or $file->pathname === 'testíček.txt' or $file->pathname === 'test.test');
     }
     $zip_archive->close();
 }
Exemple #2
0
 /**
  * Returns array of info about all files in archive.
  *
  * @param string|file_archive $archivefile
  * @return array of file infos
  */
 public function list_files($archivefile)
 {
     if (!is_string($archivefile)) {
         return $archivefile->list_files();
     }
     $ziparch = new zip_archive();
     if (!$ziparch->open($archivefile, file_archive::OPEN)) {
         return false;
     }
     $list = $ziparch->list_files();
     $ziparch->close();
     return $list;
 }
 /**
  * Validate compression of log files into a zip file
  */
 public function test_compresslogsforemail()
 {
     global $CFG, $DB;
     $logids = array();
     for ($i = 1; $i <= 3; $i++) {
         // Create summary records.
         $filename = 'compress_log_' . $i . '.txt';
         $oldpath = dirname(__FILE__) . '/other/' . $filename;
         $newpath = $CFG->dataroot . '/' . $filename;
         copy($oldpath, $newpath);
         $summarylog = new stdClass();
         $summarylog->logpath = $newpath;
         $summarylog->plugin = 'dhimport_version1';
         $summarylog->userid = 9999;
         $summarylog->targetstarttime = 0;
         $summarylog->starttime = 0;
         $summarylog->endtime = 0;
         $summarylog->filesuccesses = 0;
         $summarylog->filefailures = 0;
         $summarylog->storedsuccesses = 0;
         $summarylog->storedfailures = 0;
         $summarylog->statusmessage = '';
         $summarylog->logpath = $newpath;
         $logids[] = $DB->insert_record(RLIP_LOG_TABLE, $summarylog);
     }
     $zipfilename = rlip_compress_logs_email('dhimport_version1', $logids);
     for ($i = 1; $i <= 3; $i++) {
         // Clean up copies.
         $filename = 'compress_log_' . $i . '.txt';
         @unlink($CFG->dataroot . '/' . $filename);
     }
     // Open zip_archive and verify all logs included.
     $zip = new zip_archive();
     $result = $zip->open($CFG->dataroot . '/' . $zipfilename, file_archive::OPEN);
     $this->assertTrue($result);
     $this->assertEquals(3, $zip->count());
     $files = $zip->list_files();
     // Validate zip contents.
     for ($i = 1; $i <= 3; $i++) {
         $filename = 'compress_log_' . $i . '.txt';
         $found = false;
         foreach ($files as $file) {
             if ($file->pathname == $filename) {
                 $found = true;
                 break;
             }
         }
         $this->assertTrue($found);
     }
     $zip->close();
 }
 /**
  * @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);
 }