/**
  * {@inheritDoc}
  */
 public function download(PackageInterface $package, $path)
 {
     $temporaryDir = $this->config->get('vendor-dir') . '/composer/' . substr(md5(uniqid('', true)), 0, 8);
     $retries = 3;
     while ($retries--) {
         $fileName = parent::download($package, $path);
         if ($this->io->isVerbose()) {
             $this->io->writeError('    Extracting archive');
         }
         try {
             $this->filesystem->ensureDirectoryExists($temporaryDir);
             try {
                 $this->extract($fileName, $temporaryDir);
             } catch (\Exception $e) {
                 // remove cache if the file was corrupted
                 parent::clearCache($package, $path);
                 throw $e;
             }
             $this->filesystem->unlink($fileName);
             $contentDir = $this->getFolderContent($temporaryDir);
             // only one dir in the archive, extract its contents out of it
             if (1 === count($contentDir) && is_dir(reset($contentDir))) {
                 $contentDir = $this->getFolderContent((string) reset($contentDir));
             }
             // move files back out of the temp dir
             foreach ($contentDir as $file) {
                 $file = (string) $file;
                 $this->filesystem->rename($file, $path . '/' . basename($file));
             }
             $this->filesystem->removeDirectory($temporaryDir);
             if ($this->filesystem->isDirEmpty($this->config->get('vendor-dir') . '/composer/')) {
                 $this->filesystem->removeDirectory($this->config->get('vendor-dir') . '/composer/');
             }
             if ($this->filesystem->isDirEmpty($this->config->get('vendor-dir'))) {
                 $this->filesystem->removeDirectory($this->config->get('vendor-dir'));
             }
         } catch (\Exception $e) {
             // clean up
             $this->filesystem->removeDirectory($path);
             $this->filesystem->removeDirectory($temporaryDir);
             // retry downloading if we have an invalid zip file
             if ($retries && $e instanceof \UnexpectedValueException && class_exists('ZipArchive') && $e->getCode() === \ZipArchive::ER_NOZIP) {
                 if ($this->io->isDebug()) {
                     $this->io->writeError('    Invalid zip file (' . $e->getMessage() . '), retrying...');
                 } else {
                     $this->io->writeError('    Invalid zip file, retrying...');
                 }
                 usleep(500000);
                 continue;
             }
             throw $e;
         }
         break;
     }
     $this->io->writeError('');
 }
 public function download(PackageInterface $package, $path)
 {
     $temporaryDir = $this->config->get('vendor-dir') . '/composer/' . substr(md5(uniqid('', true)), 0, 8);
     $retries = 3;
     while ($retries--) {
         $fileName = parent::download($package, $path);
         if ($this->io->isVerbose()) {
             $this->io->write('    Extracting archive');
         }
         try {
             $this->filesystem->ensureDirectoryExists($temporaryDir);
             try {
                 $this->extract($fileName, $temporaryDir);
             } catch (\Exception $e) {
                 parent::clearCache($package, $path);
                 throw $e;
             }
             unlink($fileName);
             $contentDir = $this->listFiles($temporaryDir);
             if (1 === count($contentDir) && !is_file($contentDir[0])) {
                 $contentDir = $this->listFiles($contentDir[0]);
             }
             foreach ($contentDir as $file) {
                 $this->filesystem->rename($file, $path . '/' . basename($file));
             }
             $this->filesystem->removeDirectory($temporaryDir);
         } catch (\Exception $e) {
             $this->filesystem->removeDirectory($path);
             $this->filesystem->removeDirectory($temporaryDir);
             if ($retries && $e instanceof \UnexpectedValueException && class_exists('ZipArchive') && $e->getCode() === \ZipArchive::ER_NOZIP) {
                 $this->io->write('    Invalid zip file, retrying...');
                 usleep(500000);
                 continue;
             }
             throw $e;
         }
         break;
     }
     $this->io->write('');
 }
 /**
  * {@inheritDoc}
  */
 public function download(PackageInterface $package, $path)
 {
     parent::download($package, $path);
     $fileName = $this->getFileName($package, $path);
     if ($this->io->isVerbose()) {
         $this->io->write('    Extracting archive');
     }
     $temporaryDir = sys_get_temp_dir() . '/cmp' . substr(md5(time() . mt_rand()), 0, 5);
     try {
         $this->filesystem->ensureDirectoryExists($temporaryDir);
         try {
             $this->extract($fileName, $temporaryDir);
         } catch (\Exception $e) {
             // remove cache if the file was corrupted
             parent::clearCache($package, $path);
             throw $e;
         }
         unlink($fileName);
         // get file list
         $contentDir = $this->listFiles($temporaryDir);
         // only one dir in the archive, extract its contents out of it
         if (1 === count($contentDir) && !is_file($contentDir[0])) {
             $contentDir = $this->listFiles($contentDir[0]);
         }
         // move files back out of the temp dir
         foreach ($contentDir as $file) {
             $this->filesystem->rename($file, $path . '/' . basename($file));
         }
         $this->filesystem->removeDirectory($temporaryDir);
     } catch (\Exception $e) {
         // clean up
         $this->filesystem->removeDirectory($path);
         $this->filesystem->removeDirectory($temporaryDir);
         throw $e;
     }
     $this->io->write('');
 }