public static function Zip( string $sourceFile, string $zipFile, string $workingDir = null )
   {

      if ( ! \class_exists( '\\ZipArchive' ) )
      {
         throw new MissingExtensionError( 'ZIP', 'IO', 'Can not ZIP the file "' . $sourceFile . '"!' );
      }

      // Remember the original used working directory
      $owd = Path::Unixize( \getcwd() );

      // Prepare the actual working directory
      if ( empty( $workingDir ) )
      {
         $workingDir = Path::Unixize( \dirname( $sourceFile ) );
      }
      else
      {
         $workingDir = Path::Unixize( $workingDir );
      }

      if ( $workingDir == $owd )
      {
         // Current working dir and original working dir are the same.
         $owd = null;
      }
      else
      {
         // Set the new working directory
         \chdir( $workingDir );
      }

      // Remove the working directory from sourceFile (if it is contained)
      $sourceFile = \preg_replace(
         '~^' . \preg_quote( $workingDir, '~' ) . '/~',
         '',
         Path::Unixize( $sourceFile )
      );

      $oldFile = null;
      if ( \file_exists( $zipFile ) )
      {
         // The target ZIP file exists. Rename it to *.zip.old
         $oldFile = $zipFile . '.old';
         try { static::Move( $zipFile, $oldFile ); }
         catch ( \Throwable $ex )
         {
            // Renaming fails
            if ( ! \is_null( $owd ) )
            {
               // Restore the original working directory
               \chdir( $owd );
            }
            throw $ex;
         }
      }

      // Init the ZipArchive
      $zip = new \ZipArchive();

      if ( true === ( $res = $zip->open( $zipFile, \ZipArchive::CREATE ) ) )
      {
         // Successful opened the ZIP file writer

         // Add the source file to ZIP file writer
         $zip->addFile( $sourceFile );

         // Setting the optional ZIP archive comment
         $zip->setArchiveComment( 'Archived Single-File' );

         // Close the ZIP file
         if ( ! $zip->close() )
         {
            // ZIP file closing fails
            if ( ! empty( $oldFile ) )
            {
               // Restore the old zip file if defined
               static::Move( $oldFile, $zipFile );
            }
            if ( ! \is_null( $owd ) )
            {
               // Restore the original working directory
               \chdir( $owd );
            }
            throw new FileAccessError(
               'IO',
               $zipFile,
               FileAccessError::ACCESS_CREATE,
               'ZIP file could not be created cause write could not be completed! (Closing file fails)'
            );
         }

         if ( ! empty( $oldFile ) )
         {
            // Delete the old zip file if defined
            static::Delete( $oldFile );
         }

         if ( ! \is_null( $owd ) )
         {
            // Restore the original working directory
            \chdir( $owd );
         }

      }
      else
      {
         // Failed to open the ZIP file writer

         if ( ! \is_null( $owd ) )
         {
            // Restore the original working directory
            \chdir( $owd );
         }

         if ( ! empty( $oldFile ) )
         {
            // Restore the old zip file if defined
            static::Move( $oldFile, $zipFile );
         }

         throw new FileAccessError(
            'IO',
            $zipFile,
            FileAccessError::ACCESS_CREATE,
            'ZIP file could not be created cause ' . static::GetZipArchiveError( $res )
         );

      }

   }
public static function Zip( string $sourceFile, string $zipFile, string $workingDir = null )
   {

      if ( ! \class_exists( '\\ZipArchive' ) )
      {
         throw new MissingExtensionError( 'ZIP', 'IO', 'Can not ZIP the file "' . $sourceFile . '"!' );
      }

      // Remember the original used working directory
      $owd = Path::Unixize( \getcwd() );

      // Prepare the actual working directory
      if ( empty( $workingDir ) )
      {
         $workingDir = Path::Unixize( \dirname( $sourceFile ) );
      }
      else
      {
         $workingDir = Path::Unixize( $workingDir );
      }

      if ( $workingDir == $owd )
      {
         // Current working dir and original working dir are the same.
         $owd = null;
      }
      else
      {
         // Set the new working directory
         \chdir( $workingDir );
      }

      // Remove the working directory from sourceFile (if it is contained)
      $sourceFile = \preg_replace(
         '~^' . \preg_quote( $workingDir, '~' ) . '/~',
         '',
         Path::Unixize( $sourceFile )
      );

      $oldFile = null;
      if ( \file_exists( $zipFile ) )
      {
         // The target ZIP file exists. Rename it to *.zip.old
         $oldFile = $zipFile . '.old';
         try { static::Move( $zipFile, $oldFile ); }
         catch ( \Throwable $ex )
         {
            // Renaming fails
            if ( ! \is_null( $owd ) )
            {
               // Restore the original working directory
               \chdir( $owd );
            }
            throw $ex;
         }
      }

      // Init the ZipArchive
      $zip = new \ZipArchive();

      if ( true === ( $res = $zip->open( $zipFile, \ZipArchive::CREATE ) ) )
      {
         // Successful opened the ZIP file writer

         // Add the source file to ZIP file writer
         $zip->addFile( $sourceFile );

         // Setting the optional ZIP archive comment
         $zip->setArchiveComment( 'Archived Single-File' );

         // Close the ZIP file
         if ( ! $zip->close() )
         {
            // ZIP file closing fails
            if ( ! empty( $oldFile ) )
            {
               // Restore the old zip file if defined
               static::Move( $oldFile, $zipFile );
            }
            if ( ! \is_null( $owd ) )
            {
               // Restore the original working directory
               \chdir( $owd );
            }
            throw new FileAccessError(
               'IO',
               $zipFile,
               FileAccessError::ACCESS_CREATE,
               'ZIP file could not be created cause write could not be completed! (Closing file fails)'
            );
         }

         if ( ! empty( $oldFile ) )
         {
            // Delete the old zip file if defined
            static::Delete( $oldFile );
         }

         if ( ! \is_null( $owd ) )
         {
            // Restore the original working directory
            \chdir( $owd );
         }

      }
      else
      {
         // Failed to open the ZIP file writer

         if ( ! \is_null( $owd ) )
         {
            // Restore the original working directory
            \chdir( $owd );
         }

         if ( ! empty( $oldFile ) )
         {
            // Restore the old zip file if defined
            static::Move( $oldFile, $zipFile );
         }

         throw new FileAccessError(
            'IO',
            $zipFile,
            FileAccessError::ACCESS_CREATE,
            'ZIP file could not be created cause ' . static::GetZipArchiveError( $res )
         );

      }

   }
public static function Zip ( string $ sourceFile , string $ zipFile , string $ workingDir = null ) { if ( ! \ class_exists ( '\\ZipArchive' ) ) { throw new MissingExtensionError ( 'ZIP' , 'IO' , 'Can not ZIP the file "' . $ sourceFile . '"!' ) ; } // Remember the original used working directory $ owd = Path :: Unixize ( \ getcwd ( ) ) ; // Prepare the actual working directory if ( empty ( $ workingDir ) ) { $ workingDir = Path :: Unixize ( \ dirname ( $ sourceFile ) ) ; } else { $ workingDir = Path :: Unixize ( $ workingDir ) ; } if ( $ workingDir == $ owd ) { // Current working dir and original working dir are the same. $ owd = null ; } else { // Set the new working directory \ chdir ( $ workingDir ) ; } // Remove the working directory from sourceFile (if it is contained) $ sourceFile = \ preg_replace ( '~^' . \ preg_quote ( $ workingDir , '~' ) . '/~' , '' , Path :: Unixize ( $ sourceFile ) ) ; $ oldFile = null ; if ( \ file_exists ( $ zipFile ) ) { // The target ZIP file exists. Rename it to *.zip.old $ oldFile = $ zipFile . '.old' ; try { static :: Move ( $ zipFile , $ oldFile ) ; } catch ( \ Throwable $ ex ) { // Renaming fails if ( ! \ is_null ( $ owd ) ) { // Restore the original working directory \ chdir ( $ owd ) ; } throw $ ex ; } } // Init the ZipArchive $ zip = new \ ZipArchive ( ) ; if ( true === ( $ res = $ zip -> open ( $ zipFile , \ ZipArchive :: CREATE ) ) ) { // Successful opened the ZIP file writer // Add the source file to ZIP file writer $ zip -> addFile ( $ sourceFile ) ; // Setting the optional ZIP archive comment $ zip -> setArchiveComment ( 'Archived Single-File' ) ; // Close the ZIP file if ( ! $ zip -> close ( ) ) { // ZIP file closing fails if ( ! empty ( $ oldFile ) ) { // Restore the old zip file if defined static :: Move ( $ oldFile , $ zipFile ) ; } if ( ! \ is_null ( $ owd ) ) { // Restore the original working directory \ chdir ( $ owd ) ; } throw new FileAccessError ( 'IO' , $ zipFile , FileAccessError :: ACCESS_CREATE , 'ZIP file could not be created cause write could not be completed! (Closing file fails)' ) ; } if ( ! empty ( $ oldFile ) ) { // Delete the old zip file if defined static :: Delete ( $ oldFile ) ; } if ( ! \ is_null ( $ owd ) ) { // Restore the original working directory \ chdir ( $ owd ) ; } } else { // Failed to open the ZIP file writer if ( ! \ is_null ( $ owd ) ) { // Restore the original working directory \ chdir ( $ owd ) ; } if ( ! empty ( $ oldFile ) ) { // Restore the old zip file if defined static :: Move ( $ oldFile , $ zipFile ) ; } throw new FileAccessError ( 'IO' , $ zipFile , FileAccessError :: ACCESS_CREATE , 'ZIP file could not be created cause ' . static :: GetZipArchiveError ( $ res ) ) ; } }
Compresses the defined source file to defined ZIP archive file.

@param  string $sourceFile This file will be compressed by the zip archive
@param  string $zipFile    The target/destination ZIP file. (will be created or overwrite a existing)
@param  string $workingDir A optional working folder. (Usual its the folder, containing the $sourceFile)
@throws \Beluga\MissingExtensionError     If the require \ZipArchive class (ZIP extension) not exists
@throws \Beluga\IO\FileAccessError
@throws \Throwable
Compresses the defined source file to defined ZIP archive file .