Source of file Gzip.php

Size: 4,854 Bytes - Last Modified: 2016-05-18T03:08:27+02:00

buildproject/core/module_system/system/Gzip.php

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
<?php
/*"******************************************************************************************************
*   (c) 2004-2006 by MulchProductions, www.mulchprod.de                                                 *
*   (c) 2007-2016 by Kajona, www.kajona.de                                                              *
*       Published under the GNU LGPL v2.1, see /system/licence_lgpl.txt                                 *
*-------------------------------------------------------------------------------------------------------*
*	$Id$                                               *
********************************************************************************************************/

namespace Kajona\System\System;


/**
 * This class provides a wrapper to gzip functionalities.
 * It can be used to create compressed versions of files
 *
 * @package module_system
 * @author sidler@mulchprod.de
 */
class Gzip
{

    /**
     * Constructor
     */
    public function __construct()
    {
    }

    /**
     * Tries to compress a given file.
     * If the passed filename was test.txt, the created file is named
     * test.txt.gz
     *
     * @param string $strFilename
     * @param bool $bitDeleteSource
     *
     * @throws Exception
     * @return bool
     */
    public function compressFile($strFilename, $bitDeleteSource = false)
    {
        if (strpos($strFilename, _realpath_) === false) {
            $strFilename = _realpath_.$strFilename;
        }
        //Check if sourcefile exists
        $strTargetFilename = $strFilename.".gz";
        if (file_exists($strFilename) && is_file($strFilename)) {
            //try to open target file pointer
            if ($objTargetPointer = gzopen($strTargetFilename, "wb")) {
                //try to open sourcefile
                if ($objSourcePointer = fopen($strFilename, "rb")) {
                    //Loop over filecontent
                    while (!feof($objSourcePointer)) {
                        gzwrite($objTargetPointer, fread($objSourcePointer, 1024 * 512));
                    }
                    @fclose($objSourcePointer);
                    @gzclose($objTargetPointer);
                    //Delete the sourcefile?
                    if ($bitDeleteSource) {
                        $objFilesystem = new Filesystem();
                        $objFilesystem->fileDelete(str_replace(_realpath_, "", $strFilename));
                    }
                    return true;
                }
                else {
                    @gzclose($objTargetPointer);
                    throw new Exception("can't open sourcefile", Exception::$level_ERROR);
                }
            }
            else {
                throw new Exception("can't open targetfile", Exception::$level_ERROR);
            }
        }
        else {
            throw new Exception("Sourcefile not valid", Exception::$level_ERROR);
        }

        return false;
    }

    /**
     * Tries to decompress a given file.
     * If the passed filename was test.txt.gz, the created file is named
     * test.txt
     *
     * @param string $strFilename
     *
     * @throws Exception
     * @return bool
     */
    public function decompressFile($strFilename)
    {
        if (substr($strFilename, -3) != ".gz") {
            throw new Exception("sourcefile ".$strFilename." no valid .gz file", Exception::$level_ERROR);
        }

        if (strpos($strFilename, _realpath_) === false) {
            $strFilename = _realpath_.$strFilename;
        }

        //Check if sourcefile exists
        $strTargetFilename = substr($strFilename, 0, strlen($strFilename) - 3);

        if (file_exists($strFilename) && is_file($strFilename)) {
            //try to open sourcefile
            if ($objSourcePointer = gzopen($strFilename, "rb")) {

                if ($objTargetPointer = fopen($strTargetFilename, "wb")) {
                    //Loop over filecontent
                    while (!gzeof($objSourcePointer)) {
                        fwrite($objTargetPointer, gzread($objSourcePointer, 1024 * 512));
                        //$strContent .= gzread($objSourcePointer,  1024 * 512);
                    }
                    @gzclose($objSourcePointer);
                    @fclose($objTargetPointer);

                    return true;

                }
                else {
                    @gzclose($objSourcePointer);
                    throw new Exception("can't write to targetfile ", Exception::$level_ERROR);
                }

            }
            else {
                gzclose($objSourcePointer);
                throw new Exception("can't open sourcefile", Exception::$level_ERROR);
            }

        }
        else {
            throw new Exception("Sourcefile not valid", Exception::$level_ERROR);
        }

        return false;
    }


}