Selaa lähdekoodia

[GENERIC] Zend_Filter_Compress:

- promoted Zend_Filter_Compress to core

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@18311 44c647ce-9c0f-0410-b52a-842ac1e357ba
thomas 16 vuotta sitten
vanhempi
commit
b2b6abae1c

+ 635 - 0
documentation/manual/en/module_specs/Zend_Filter-Compress.xml

@@ -0,0 +1,635 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!-- Reviewed: no -->
+<sect2 id="zend.filter.set.compress">
+    <title>Compress and Decompress</title>
+    <para>
+        These two filters are capable of compressing and decompressing strings, files, and
+        directories. They make use of adapters and support the following compression formats:
+    </para>
+
+    <itemizedlist>
+        <listitem>
+            <para>
+                <emphasis>Bz2</emphasis>
+            </para>
+        </listitem>
+
+        <listitem>
+            <para>
+                <emphasis>Gz</emphasis>
+            </para>
+        </listitem>
+
+        <listitem>
+            <para>
+                <emphasis>Lzf</emphasis>
+            </para>
+        </listitem>
+
+        <listitem>
+            <para>
+                <emphasis>Rar</emphasis>
+            </para>
+        </listitem>
+
+        <listitem>
+            <para>
+                <emphasis>Tar</emphasis>
+            </para>
+        </listitem>
+
+        <listitem>
+            <para>
+                <emphasis>Zip</emphasis>
+            </para>
+        </listitem>
+    </itemizedlist>
+
+    <para>
+        Each compression format has different capabilities as described below. All compression
+        filters may be used in approximately the same ways, and differ primarily in the options
+        available and the type of compression they offer (both algorithmically as well as string vs.
+        file vs. directory)
+    </para>
+
+    <sect3 id="zend.filter.set.compress.generic">
+        <title>Generic handling</title>
+
+        <para>
+            To create a compression filter you need to select the compression format you want to
+            use. The following description takes the <emphasis>Bz2</emphasis> adapter. Details for
+            all other adapters are described after this section.
+        </para>
+
+        <para>
+            The two filters are basically identical, in that they utilize the same backends.
+            <classname>Zend_Filter_Compress</classname> should be used when you wish to compress
+            items, and <classname>Zend_Filter_Decompress</classname> should be used when you wish to
+            decompress items.
+        </para>
+
+        <para>
+            For instance, if we want to compress a string, we have to initiate
+            <classname>Zend_Filter_Compress</classname> and indicate the desired adapter.
+        </para>
+
+        <programlisting language="php"><![CDATA[
+$filter = new Zend_Filter_Compress('Bz2');
+]]></programlisting>
+
+        <para>
+            To use a different adapter, you simply specify it to the constructor.
+        </para>
+
+        <para>
+            You may also provide an array of options or <classname>Zend_Config</classname> object.
+            If you do, provide minimally the key "adapter", and then either the key "options" or
+            "adapterOptions" (which should be an array of options to provide to the adapter on
+            instantiation).
+        </para>
+
+        <programlisting language="php"><![CDATA[
+$filter = new Zend_Filter_Compress(array(
+    'adapter' => 'Bz2',
+    'options' => array(
+        'blocksize' => 8,
+    ),
+));
+]]></programlisting>
+
+        <note>
+            <title>Default compression Adapter</title>
+
+            <para>
+                When no compression adapter is given, then the <emphasis>Gz</emphasis> adapter will
+                be used.
+            </para>
+        </note>
+
+        <para>
+            Almost the same usage is we want to decompress a string. We just have to use the
+            decompression filter in this case.
+        </para>
+
+        <programlisting language="php"><![CDATA[
+$filter = new Zend_Filter_Decompress('Bz2');
+]]></programlisting>
+
+        <para>
+            To get the compressed string, we have to give the original string. The filtered value is
+            the compressed version of the original string.
+        </para>
+
+        <programlisting language="php"><![CDATA[
+$filter     = new Zend_Filter_Compress('Bz2');
+$compressed = $filter->filter('Uncompressed string');
+// Returns the compressed string
+]]></programlisting>
+
+        <para>
+            Decompression works the same way.
+        </para>
+
+        <programlisting language="php"><![CDATA[
+$filter     = new Zend_Filter_Decompress('Bz2');
+$compressed = $filter->filter('Compressed string');
+// Returns the uncompressed string
+]]></programlisting>
+
+        <note>
+            <title>Note on string compression</title>
+
+            <para>
+                Not all adapters support string compression. Compression formats like
+                <emphasis>Rar</emphasis> can only handle files and directories. For details, consult
+                the section for the adapter you wish to use.
+            </para>
+        </note>
+    </sect3>
+
+    <sect3 id="zend.filter.set.compress.archive">
+        <title>Creating an archive</title>
+
+        <para>
+            Creating an archive file works almost the same as compressing a string. However, in this
+            case we need an additional parameter which holds the name of the archive we want to
+            create.
+        </para>
+
+        <programlisting language="php"><![CDATA[
+$filter     = new Zend_Filter_Compress(array(
+    'adapter' => 'Bz2', 
+    'options' => array(
+        'archive' => 'filename.bz2',
+    ),
+));
+$compressed = $filter->filter('Uncompressed string');
+// Returns true on success and creates the archive file
+]]></programlisting>
+
+        <para>
+            In the above example the uncompressed string is compressed, and is then written into
+            the given archive file.
+        </para>
+
+        <note>
+            <title>Existing archives will be overwritten</title>
+
+            <para>
+                The content of any existing file will be overwritten when the given filename of
+                the archive already exists.
+            </para>
+        </note>
+
+        <para>
+            When you want to compress a file, then you must give the name of the file with its path.
+        </para>
+
+        <programlisting language="php"><![CDATA[
+$filter     = new Zend_Filter_Compress(array(
+    'adapter' => 'Bz2', 
+    'options' => array(
+        'archive' => 'filename.bz2'
+    ),
+));
+$compressed = $filter->filter('C:\temp\compressme.txt');
+// Returns true on success and creates the archive file
+]]></programlisting>
+
+        <para>
+            You may also specify a directory instead of a filename. In this case the whole directory
+            with all its files and subdirectories will be compressed into the archive.
+        </para>
+
+        <programlisting language="php"><![CDATA[
+$filter     = new Zend_Filter_Compress(array(
+    'adapter' => 'Bz2', 
+    'options' => array(
+        'archive' => 'filename.bz2'
+    ),
+));
+$compressed = $filter->filter('C:\temp\compressme.txt');
+// Returns true on success and creates the archive file
+]]></programlisting>
+
+        <note>
+            <title>Do not compress large or base directories</title>
+
+            <para>
+                You should never compress large or base directories like a complete partition.
+                Compressing a complete partition is a very time consuming task which can lead
+                to massive problems on your server when there is not enough space or your
+                script takes too much time.
+            </para>
+        </note>
+    </sect3>
+
+    <sect3 id="zend.filter.set.compress.decompress">
+        <title>Decompressing an archive</title>
+
+        <para>
+            Decompressing an archive file works almost like compressing it. You must specify either
+            the <property>archive</property> parameter, or give the filename of the archive when you
+            decompress the file.
+        </para>
+
+        <programlisting language="php"><![CDATA[
+$filter     = new Zend_Filter_Decompress('Bz2');
+$compressed = $filter->filter('filename.bz2');
+// Returns true on success and decompresses the archive file
+]]></programlisting>
+
+        <para>
+            Some adapters support decompressing the archive into another subdirectory. In this
+            case you can set the <property>target</property> parameter.
+        </para>
+
+        <programlisting language="php"><![CDATA[
+$filter     = new Zend_Filter_Decompress(array(
+    'adapter' => 'Zip', 
+    'options' => array(
+        'target' => 'C:\temp',
+    )
+));
+$compressed = $filter->filter('filename.zip');
+// Returns true on success and decompresses the archive file
+// into the given target directory
+]]></programlisting>
+
+        <note>
+            <title>Directories to extract to must exist</title>
+
+            <para>
+                When you want to decompress an archive into a directory, then that directory must
+                exist.
+            </para>
+        </note>
+    </sect3>
+
+    <sect3 id="zend.filter.set.compress.bz2">
+        <title>Bz2 Adapter</title>
+
+        <para>
+            The Bz2 Adapter can compress and decompress:
+        </para>
+
+        <itemizedlist>
+            <listitem>
+                <para>Strings</para>
+            </listitem>
+
+            <listitem>
+                <para>Files</para>
+            </listitem>
+
+            <listitem>
+                <para>Directories</para>
+            </listitem>
+        </itemizedlist>
+
+        <para>
+            This adapter makes use of <acronym>PHP</acronym>'s Bz2 extension.
+        </para>
+
+        <para>
+            To customize compression, this adapter supports the following options:
+        </para>
+
+        <itemizedlist>
+            <listitem>
+                <para>
+                    <emphasis>Archive</emphasis>: This parameter sets the archive file which should
+                    be used or created.
+                </para>
+            </listitem>
+
+            <listitem>
+                <para>
+                    <emphasis>Blocksize</emphasis>: This parameter sets the blocksize to use. It
+                    can be from '0' to '9'. The default value is '4'.
+                </para>
+            </listitem>
+        </itemizedlist>
+
+        <para>
+            All options can be set at instantiation or by using a related method. For example, the
+            related methods for 'Blocksize' are <methodname>getBlocksize()</methodname> and
+            <methodname>setBlocksize()</methodname>. You can also use the
+            <methodname>setOptions()</methodname> method which accepts all options as array.
+        </para>
+    </sect3>
+
+    <sect3 id="zend.filter.set.compress.gz">
+        <title>Gz Adapter</title>
+
+        <para>
+            The Gz Adapter can compress and decompress:
+        </para>
+
+        <itemizedlist>
+            <listitem>
+                <para>Strings</para>
+            </listitem>
+
+            <listitem>
+                <para>Files</para>
+            </listitem>
+
+            <listitem>
+                <para>Directories</para>
+            </listitem>
+        </itemizedlist>
+
+        <para>
+            This adapter makes use of <acronym>PHP</acronym>'s Zlib extension.
+        </para>
+
+        <para>
+            To customize the compression this adapter supports the following options:
+        </para>
+
+        <itemizedlist>
+            <listitem>
+                <para>
+                    <emphasis>Archive</emphasis>: This parameter sets the archive file which should
+                    be used or created.
+                </para>
+            </listitem>
+
+            <listitem>
+                <para>
+                    <emphasis>Level</emphasis>: This compression level to use. It can be from '0' to
+                    '9'. The default value is '9'.
+                </para>
+            </listitem>
+
+            <listitem>
+                <para>
+                    <emphasis>Mode</emphasis>: There are two supported modes. 'compress' and
+                    'deflate'. The default value is 'compress'.
+                </para>
+            </listitem>
+        </itemizedlist>
+
+        <para>
+            All options can be set at initiation or by using a related method. For example, the
+            related methods for 'Level' are <methodname>getLevel()</methodname> and
+            <methodname>setLevel()</methodname>. You can also use the
+            <methodname>setOptions()</methodname> method which accepts all options as array.
+        </para>
+    </sect3>
+
+    <sect3 id="zend.filter.set.compress.lzf">
+        <title>Lzf Adapter</title>
+
+        <para>
+            The Lzf Adapter can compress and decompress:
+        </para>
+
+        <itemizedlist>
+            <listitem>
+                <para>Strings</para>
+            </listitem>
+        </itemizedlist>
+
+        <note>
+            <title>Lzf supports only strings</title>
+
+            <para>
+                The Lzf adapter can not handle files and directories.
+            </para>
+        </note>
+
+        <para>
+            This adapter makes use of <acronym>PHP</acronym>'s Lzf extension.
+        </para>
+
+        <para>
+            There are no options available to customize this adapter.
+        </para>
+    </sect3>
+
+    <sect3 id="zend.filter.set.compress.rar">
+        <title>Rar Adapter</title>
+
+        <para>
+            The Rar Adapter can compress and decompress:
+        </para>
+
+        <itemizedlist>
+            <listitem>
+                <para>Files</para>
+            </listitem>
+
+            <listitem>
+                <para>Directories</para>
+            </listitem>
+        </itemizedlist>
+
+        <note>
+            <title>Rar does not support strings</title>
+
+            <para>
+                The Rar Adapter can not handle strings.
+            </para>
+        </note>
+
+        <para>
+            This adapter makes use of <acronym>PHP</acronym>'s Rar extension.
+        </para>
+
+        <note>
+            <title>Rar compression not supported</title>
+
+            <para>
+                Due to restrictions with the Rar compression format, there is no compression available
+                for free. When you want to compress files into a new Rar archive, you must provide a
+                callback to the adapter that can invoke a Rar compression program.
+            </para>
+        </note>
+
+        <para>
+            To customize the compression this adapter supports the following options:
+        </para>
+
+        <itemizedlist>
+            <listitem>
+                <para>
+                    <emphasis>Archive</emphasis>: This parameter sets the archive file which should
+                    be used or created.
+                </para>
+            </listitem>
+
+            <listitem>
+                <para>
+                    <emphasis>Callback</emphasis>: A callback which provides compression support to
+                    this adapter.
+                </para>
+            </listitem>
+
+            <listitem>
+                <para>
+                    <emphasis>Password</emphasis>: The password which has to be used for
+                    decompression.
+                </para>
+            </listitem>
+
+            <listitem>
+                <para>
+                    <emphasis>Target</emphasis>: The target where the decompressed files will be
+                    written to.
+                </para>
+            </listitem>
+        </itemizedlist>
+
+        <para>
+            All options can be set at instantiation or by using a related method. For example, the
+            related methods for 'Target' are <methodname>getTarget()</methodname> and
+            <methodname>setTarget()</methodname>. You can also use the
+            <methodname>setOptions()</methodname> method which accepts all options as array.
+        </para>
+    </sect3>
+
+    <sect3 id="zend.filter.set.compress.tar">
+        <title>Tar Adapter</title>
+
+        <para>
+            The Tar Adapter can compress and decompress:
+        </para>
+
+        <itemizedlist>
+            <listitem>
+                <para>Files</para>
+            </listitem>
+
+            <listitem>
+                <para>Directories</para>
+            </listitem>
+        </itemizedlist>
+
+        <note>
+            <title>Tar does not support strings</title>
+
+            <para>
+                The Tar Adapter can not handle strings.
+            </para>
+        </note>
+
+        <para>
+            This adapter makes use of <acronym>PEAR</acronym>'s <classname>Archive_Tar</classname>
+            component.
+        </para>
+
+        <para>
+            To customize the compression this adapter supports the following options:
+        </para>
+
+        <itemizedlist>
+            <listitem>
+                <para>
+                    <emphasis>Archive</emphasis>: This parameter sets the archive file which should
+                    be used or created.
+                </para>
+            </listitem>
+
+            <listitem>
+                <para>
+                    <emphasis>Mode</emphasis>: A mode to use for compression. Supported are either
+                    'null' which means no compression at all, 'Gz' which makes use of
+                    <acronym>PHP</acronym>'s Zlib extension and 'Bz2' which makes use of
+                    <acronym>PHP</acronym>'s Bz2 extension. The default value is 'null'.
+                </para>
+            </listitem>
+
+            <listitem>
+                <para>
+                    <emphasis>Target</emphasis>: The target where the decompressed files will be
+                    written to.
+                </para>
+            </listitem>
+        </itemizedlist>
+
+        <para>
+            All options can be set at instantiation or by using a related method. For example, the
+            related methods for 'Target' are <methodname>getTarget()</methodname> and
+            <methodname>setTarget()</methodname>. You can also use the
+            <methodname>setOptions()</methodname> method which accepts all options as array.
+        </para>
+
+        <note>
+            <title>Directory usage</title>
+
+            <para>
+                When compressing directories with Tar then the complete file path is used. This
+                means that created Tar files will not only have the subdirectory but the complete
+                path for the compressed file.
+            </para>
+        </note>
+    </sect3>
+
+    <sect3 id="zend.filter.set.compress.zip">
+        <title>Zip Adapter</title>
+
+        <para>
+            The Zip Adapter can compress and decompress:
+        </para>
+
+        <itemizedlist>
+            <listitem>
+                <para>Strings</para>
+            </listitem>
+
+            <listitem>
+                <para>Files</para>
+            </listitem>
+
+            <listitem>
+                <para>Directories</para>
+            </listitem>
+        </itemizedlist>
+
+        <note>
+            <title>Zip does not support string decompression</title>
+
+            <para>
+                The Zip Adapter can not handle decompression to a string; decompression will
+                always be written to a file.
+            </para>
+        </note>
+
+        <para>
+            This adapter makes use of <acronym>PHP</acronym>'s <classname>Zip</classname>
+            extension.
+        </para>
+
+        <para>
+            To customize the compression this adapter supports the following options:
+        </para>
+
+        <itemizedlist>
+            <listitem>
+                <para>
+                    <emphasis>Archive</emphasis>: This parameter sets the archive file which should
+                    be used or created.
+                </para>
+            </listitem>
+
+            <listitem>
+                <para>
+                    <emphasis>Target</emphasis>: The target where the decompressed files will be
+                    written to.
+                </para>
+            </listitem>
+        </itemizedlist>
+
+        <para>
+            All options can be set at instantiation or by using a related method. For example, the
+            related methods for 'Target' are <methodname>getTarget()</methodname> and
+            <methodname>setTarget()</methodname>. You can also use the
+            <methodname>setOptions()</methodname> method which accepts all options as array.
+        </para>
+    </sect3>
+</sect2>
+<!--
+vim:se ts=4 sw=4 et:
+-->

+ 1 - 0
documentation/manual/en/module_specs/Zend_Filter-Set.xml

@@ -43,6 +43,7 @@
     </sect2>
     </sect2>
 
 
     <xi:include href="Zend_Filter-Callback.xml" />
     <xi:include href="Zend_Filter-Callback.xml" />
+    <xi:include href="Zend_Filter-Compress.xml" />
     <xi:include href="Zend_Filter-Decryption.xml" />
     <xi:include href="Zend_Filter-Decryption.xml" />
 
 
     <sect2 id="zend.filter.set.digits">
     <sect2 id="zend.filter.set.digits">

+ 197 - 0
library/Zend/Filter/Compress.php

@@ -0,0 +1,197 @@
+<?php
+/**
+ * Zend Framework
+ *
+ * LICENSE
+ *
+ * This source file is subject to the new BSD license that is bundled
+ * with this package in the file LICENSE.txt.
+ * It is also available through the world-wide-web at this URL:
+ * http://framework.zend.com/license/new-bsd
+ * If you did not receive a copy of the license and are unable to
+ * obtain it through the world-wide-web, please send an email
+ * to license@zend.com so we can send you a copy immediately.
+ *
+ * @category   Zend
+ * @package    Zend_Filter
+ * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ * @version    $Id: $
+ */
+
+/**
+ * @see Zend_Filter_Interface
+ */
+require_once 'Zend/Filter/Interface.php';
+
+/**
+ * Compresses a given string
+ *
+ * @category   Zend
+ * @package    Zend_Filter
+ * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ */
+class Zend_Filter_Compress implements Zend_Filter_Interface
+{
+    /**
+     * Compression adapter
+     */
+    protected $_adapter = 'Gz';
+
+    /**
+     * Compression adapter constructor options
+     */
+    protected $_adapterOptions = array();
+
+    /**
+     * Class constructor
+     *
+     * @param string|array $options (Optional) Options to set
+     */
+    public function __construct($options = null)
+    {
+        if ($options instanceof Zend_Config) {
+            $options = $options->toArray();
+        }
+        if (is_string($options)) {
+            $this->setAdapter($options);
+        } elseif ($options instanceof Zend_Filter_Compress_CompressInterface) {
+            $this->setAdapter($options);
+        } elseif (is_array($options)) {
+            $this->setOptions($options);
+        }
+    }
+
+    /**
+     * Set filter setate
+     * 
+     * @param  array $options 
+     * @return Zend_Filter_Compress
+     */
+    public function setOptions(array $options)
+    {
+        foreach ($options as $key => $value) {
+            if ($key == 'options') {
+                $key = 'adapterOptions';
+            }
+            $method = 'set' . ucfirst($key);
+            if (method_exists($this, $method)) {
+                $this->$method($value);
+            }
+        }
+        return $this;
+    }
+
+    /**
+     * Returns the current adapter, instantiating it if necessary
+     *
+     * @return string
+     */
+    public function getAdapter()
+    {
+        if ($this->_adapter instanceof Zend_Filter_Compress_CompressInterface) {
+            return $this->_adapter;
+        }
+
+        $adapter = $this->_adapter;
+        $options = $this->getAdapterOptions();
+        if (!class_exists($adapter)) {
+            require_once 'Zend/Loader.php';
+            if (Zend_Loader::isReadable('Zend/Filter/Compress/' . ucfirst($adapter) . '.php')) {
+                $adapter = 'Zend_Filter_Compress_' . ucfirst($adapter);
+            }
+            Zend_Loader::loadClass($adapter);
+        }
+
+        $this->_adapter = new $adapter($options);
+        if (!$this->_adapter instanceof Zend_Filter_Compress_CompressInterface) {
+            require_once 'Zend/Filter/Exception.php';
+            throw new Zend_Filter_Exception("Compression adapter '" . $adapter . "' does not implement Zend_Filter_Compress_CompressInterface");
+        }
+        return $this->_adapter;
+    }
+
+    /**
+     * Retrieve adapter name
+     * 
+     * @return string
+     */
+    public function getAdapterName()
+    {
+        return $this->getAdapter()->toString();
+    }
+
+    /**
+     * Sets compression adapter
+     *
+     * @param  string|Zend_Filter_Compress_CompressInterface $adapter Adapter to use
+     * @return Zend_Filter_Compress
+     */
+    public function setAdapter($adapter)
+    {
+        if ($adapter instanceof Zend_Filter_Compress_CompressInterface) {
+            $this->_adapter = $adapter;
+            return $this;
+        }
+        if (!is_string($adapter)) {
+            require_once 'Zend/Filter/Exception.php';
+            throw new Zend_Filter_Exception('Invalid adapter provided; must be string or instance of Zend_Filter_Compress_CompressInterface');
+        }
+        $this->_adapter = $adapter;
+
+        return $this;
+    }
+
+    /**
+     * Retrieve adapter options
+     * 
+     * @return array
+     */
+    public function getAdapterOptions()
+    {
+        return $this->_adapterOptions;
+    }
+
+    /**
+     * Set adapter options
+     * 
+     * @param  array $options 
+     * @return void
+     */
+    public function setAdapterOptions(array $options)
+    {
+        $this->_adapterOptions = $options;
+        return $this;
+    }
+
+    /**
+     * Calls adapter methods
+     *
+     * @param string       $method  Method to call
+     * @param string|array $options Options for this method
+     */
+    public function __call($method, $options)
+    {
+        $adapter = $this->getAdapter();
+        if (!method_exists($adapter, $method)) {
+            require_once 'Zend/Filter/Exception.php';
+            throw new Zend_Filter_Exception("Unknown method '{$method}'");
+        }
+
+        return call_user_func_array(array($adapter, $method), $options);
+    }
+
+    /**
+     * Defined by Zend_Filter_Interface
+     *
+     * Compresses the content $value with the defined settings
+     *
+     * @param  string $value Content to compress
+     * @return string The compressed content
+     */
+    public function filter($value)
+    {
+        return $this->getAdapter()->compress($value);
+    }
+}

+ 188 - 0
library/Zend/Filter/Compress/Bz2.php

@@ -0,0 +1,188 @@
+<?php
+/**
+ * Zend Framework
+ *
+ * LICENSE
+ *
+ * This source file is subject to the new BSD license that is bundled
+ * with this package in the file LICENSE.txt.
+ * It is also available through the world-wide-web at this URL:
+ * http://framework.zend.com/license/new-bsd
+ * If you did not receive a copy of the license and are unable to
+ * obtain it through the world-wide-web, please send an email
+ * to license@zend.com so we can send you a copy immediately.
+ *
+ * @category   Zend
+ * @package    Zend_Filter
+ * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ * @version    $Id: $
+ */
+
+/**
+ * @see Zend_Filter_Compress_CompressAbstract
+ */
+require_once 'Zend/Filter/Compress/CompressAbstract.php';
+
+/**
+ * Compression adapter for Bz2
+ *
+ * @category   Zend
+ * @package    Zend_Filter
+ * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ */
+class Zend_Filter_Compress_Bz2 extends Zend_Filter_Compress_CompressAbstract
+{
+    /**
+     * Compression Options
+     * array(
+     *     'blocksize' => Blocksize to use from 0-9
+     *     'archive'   => Archive to use
+     * )
+     *
+     * @var array
+     */
+    protected $_options = array(
+        'blocksize' => 4,
+        'archive'   => null,
+    );
+
+    /**
+     * Class constructor
+     *
+     * @param array|Zend_Config $options (Optional) Options to set
+     */
+    public function __construct($options = null)
+    {
+        if (!extension_loaded('bz2')) {
+            require_once 'Zend/Filter/Exception.php';
+            throw new Zend_Filter_Exception('This filter needs the bz2 extension');
+        }
+        parent::__construct($options);
+    }
+
+    /**
+     * Returns the set blocksize
+     *
+     * @return integer
+     */
+    public function getBlocksize()
+    {
+        return $this->_options['blocksize'];
+    }
+
+    /**
+     * Sets a new blocksize
+     *
+     * @param integer $level
+     * @return Zend_Filter_Compress_Bz2
+     */
+    public function setBlocksize($blocksize)
+    {
+        if (($blocksize < 0) || ($blocksize > 9)) {
+            require_once 'Zend/Filter/Exception.php';
+            throw new Zend_Filter_Exception('Blocksize must be between 0 and 9');
+        }
+
+        $this->_options['blocksize'] = (int) $blocksize;
+        return $this;
+    }
+
+    /**
+     * Returns the set archive
+     *
+     * @return string
+     */
+    public function getArchive()
+    {
+        return $this->_options['archive'];
+    }
+
+    /**
+     * Sets the archive to use for de-/compression
+     *
+     * @param string $archive Archive to use
+     * @return Zend_Filter_Compress_Bz2
+     */
+    public function setArchive($archive)
+    {
+        $this->_options['archive'] = (string) $archive;
+        return $this;
+    }
+
+    /**
+     * Compresses the given content
+     *
+     * @param  string $content
+     * @return string
+     */
+    public function compress($content)
+    {
+        $archive = $this->getArchive();
+        if (!empty($archive)) {
+            $file = bzopen($archive, 'w');
+            if (!$file) {
+                require_once 'Zend/Filter/Exception.php';
+                throw new Zend_Filter_Exception("Error opening the archive '" . $archive . "'");
+            }
+
+            bzwrite($file, $content);
+            bzclose($file);
+            $compressed = true;
+        } else {
+            $compressed = bzcompress($content, $this->getBlocksize());
+        }
+
+        if (is_int($compressed)) {
+            require_once 'Zend/Filter/Exception.php';
+            throw new Zend_Filter_Exception('Error during compression');
+        }
+
+        return $compressed;
+    }
+
+    /**
+     * Decompresses the given content
+     *
+     * @param  string $content
+     * @return string
+     */
+    public function decompress($content)
+    {
+        $archive = $this->getArchive();
+        if (file_exists($content)) {
+            $archive = $content;
+        }
+
+        if (file_exists($archive)) {
+            $file = bzopen($archive, 'r');
+            if (!$file) {
+                require_once 'Zend/Filter/Exception.php';
+                throw new Zend_Filter_Exception("Error opening the archive '" . $content . "'");
+            }
+
+            $compressed = bzread($file);
+            bzclose($file);
+        } else {
+            $compressed = bzdecompress($content);
+        }
+
+        if (is_int($compressed)) {
+            require_once 'Zend/Filter/Exception.php';
+            throw new Zend_Filter_Exception('Error during decompression');
+        }
+
+        return $compressed;
+    }
+
+    /**
+     * Returns the adapter name
+     *
+     * @return string
+     */
+    public function toString()
+    {
+        return 'Bz2';
+    }
+}

+ 89 - 0
library/Zend/Filter/Compress/CompressAbstract.php

@@ -0,0 +1,89 @@
+<?php
+/**
+ * Zend Framework
+ *
+ * LICENSE
+ *
+ * This source file is subject to the new BSD license that is bundled
+ * with this package in the file LICENSE.txt.
+ * It is also available through the world-wide-web at this URL:
+ * http://framework.zend.com/license/new-bsd
+ * If you did not receive a copy of the license and are unable to
+ * obtain it through the world-wide-web, please send an email
+ * to license@zend.com so we can send you a copy immediately.
+ *
+ * @category   Zend
+ * @package    Zend_Filter
+ * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ * @version    $Id: $
+ */
+
+/**
+ * @see Zend_Filter_Compress_CompressInterface
+ */
+require_once 'Zend/Filter/Compress/CompressInterface.php';
+
+/**
+ * Abstract compression adapter
+ *
+ * @category   Zend
+ * @package    Zend_Filter
+ * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ */
+abstract class Zend_Filter_Compress_CompressAbstract implements Zend_Filter_Compress_CompressInterface
+{
+    /**
+     * Class constructor
+     *
+     * @param array|Zend_Config $options (Optional) Options to set
+     */
+    public function __construct($options = null)
+    {
+        if ($options instanceof Zend_Config) {
+            $options = $options->toArray();
+        }
+
+        if (is_array($options)) {
+            $this->setOptions($options);
+        }
+    }
+
+    /**
+     * Returns one or all set options
+     *
+     * @param string $option (Optional) Option to return
+     * @return mixed
+     */
+    public function getOptions($option = null)
+    {
+        if ($option === null) {
+            return $this->_options;
+        }
+
+        if (!array_key_exists($option, $this->_options)) {
+            return null;
+        }
+
+        return $this->_options[$option];
+    }
+
+    /**
+     * Sets all or one option
+     *
+     * @param  array $options
+     * @return Zend_Filter_Compress_Bz2
+     */
+    public function setOptions(array $options)
+    {
+        foreach ($options as $key => $option) {
+            $method = 'set' . $key;
+            if (method_exists($this, $method)) {
+                $this->$method($option);
+            }
+        }
+
+        return $this;
+    }
+}

+ 54 - 0
library/Zend/Filter/Compress/CompressInterface.php

@@ -0,0 +1,54 @@
+<?php
+/**
+ * Zend Framework
+ *
+ * LICENSE
+ *
+ * This source file is subject to the new BSD license that is bundled
+ * with this package in the file LICENSE.txt.
+ * It is also available through the world-wide-web at this URL:
+ * http://framework.zend.com/license/new-bsd
+ * If you did not receive a copy of the license and are unable to
+ * obtain it through the world-wide-web, please send an email
+ * to license@zend.com so we can send you a copy immediately.
+ *
+ * @category   Zend
+ * @package    Zend_Filter
+ * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ * @version    $Id: $
+ */
+
+/**
+ * Compression interface
+ *
+ * @category   Zend
+ * @package    Zend_Filter
+ * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ */
+interface Zend_Filter_Compress_CompressInterface
+{
+    /**
+     * Compresses $value with the defined settings
+     *
+     * @param  string $value Data to compress
+     * @return string The compressed data
+     */
+    public function compress($value);
+
+    /**
+     * Decompresses $value with the defined settings
+     *
+     * @param  string $value Data to decompress
+     * @return string The decompressed data
+     */
+    public function decompress($value);
+
+    /**
+     * Return the adapter name
+     * 
+     * @return string
+     */
+    public function toString();
+}

+ 228 - 0
library/Zend/Filter/Compress/Gz.php

@@ -0,0 +1,228 @@
+<?php
+/**
+ * Zend Framework
+ *
+ * LICENSE
+ *
+ * This source file is subject to the new BSD license that is bundled
+ * with this package in the file LICENSE.txt.
+ * It is also available through the world-wide-web at this URL:
+ * http://framework.zend.com/license/new-bsd
+ * If you did not receive a copy of the license and are unable to
+ * obtain it through the world-wide-web, please send an email
+ * to license@zend.com so we can send you a copy immediately.
+ *
+ * @category   Zend
+ * @package    Zend_Filter
+ * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ * @version    $Id: $
+ */
+
+/**
+ * @see Zend_Filter_Compress_CompressAbstract
+ */
+require_once 'Zend/Filter/Compress/CompressAbstract.php';
+
+/**
+ * Compression adapter for Gzip (ZLib)
+ *
+ * @category   Zend
+ * @package    Zend_Filter
+ * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ */
+class Zend_Filter_Compress_Gz extends Zend_Filter_Compress_CompressAbstract
+{
+    /**
+     * Compression Options
+     * array(
+     *     'level'    => Compression level 0-9
+     *     'mode'     => Compression mode, can be 'compress', 'deflate'
+     *     'archive'  => Archive to use
+     * )
+     *
+     * @var array
+     */
+    protected $_options = array(
+        'level'   => 9,
+        'mode'    => 'compress',
+        'archive' => null,
+    );
+
+    /**
+     * Class constructor
+     *
+     * @param array|Zend_Config|null $options (Optional) Options to set
+     */
+    public function __construct($options = null)
+    {
+        if (!extension_loaded('zlib')) {
+            require_once 'Zend/Filter/Exception.php';
+            throw new Zend_Filter_Exception('This filter needs the zlib extension');
+        }
+        parent::__construct($options);
+    }
+
+    /**
+     * Returns the set compression level
+     *
+     * @return integer
+     */
+    public function getLevel()
+    {
+        return $this->_options['level'];
+    }
+
+    /**
+     * Sets a new compression level
+     *
+     * @param integer $level
+     * @return Zend_Filter_Compress_Gz
+     */
+    public function setLevel($level)
+    {
+        if (($level < 0) || ($level > 9)) {
+            require_once 'Zend/Filter/Exception.php';
+            throw new Zend_Filter_Exception('Level must be between 0 and 9');
+        }
+
+        $this->_options['level'] = (int) $level;
+        return $this;
+    }
+
+    /**
+     * Returns the set compression mode
+     *
+     * @return string
+     */
+    public function getMode()
+    {
+        return $this->_options['mode'];
+    }
+
+    /**
+     * Sets a new compression mode
+     *
+     * @param string $mode Supported are 'compress', 'deflate' and 'file'
+     */
+    public function setMode($mode)
+    {
+        if (($mode != 'compress') && ($mode != 'deflate')) {
+            require_once 'Zend/Filter/Exception.php';
+            throw new Zend_Filter_Exception('Given compression mode not supported');
+        }
+
+        $this->_options['mode'] = $mode;
+        return $this;
+    }
+
+    /**
+     * Returns the set archive
+     *
+     * @return string
+     */
+    public function getArchive()
+    {
+        return $this->_options['archive'];
+    }
+
+    /**
+     * Sets the archive to use for de-/compression
+     *
+     * @param string $archive Archive to use
+     * @return Zend_Filter_Compress_Gz
+     */
+    public function setArchive($archive)
+    {
+        $this->_options['archive'] = (string) $archive;
+        return $this;
+    }
+
+    /**
+     * Compresses the given content
+     *
+     * @param  string $content
+     * @return string
+     */
+    public function compress($content)
+    {
+        $archive = $this->getArchive();
+        if (!empty($archive)) {
+            $file = gzopen($archive, 'w' . $this->getLevel());
+            if (!$file) {
+                require_once 'Zend/Filter/Exception.php';
+                throw new Zend_Filter_Exception("Error opening the archive '" . $this->_options['archive'] . "'");
+            }
+
+            gzwrite($file, $content);
+            gzclose($file);
+            $compressed = true;
+        } else if ($this->_options['mode'] == 'deflate') {
+            $compressed = gzdeflate($content, $this->getLevel());
+        } else {
+            $compressed = gzcompress($content, $this->getLevel());
+        }
+
+        if (!$compressed) {
+            require_once 'Zend/Filter/Exception.php';
+            throw new Zend_Filter_Exception('Error during compression');
+        }
+
+        return $compressed;
+    }
+
+    /**
+     * Decompresses the given content
+     *
+     * @param  string $content
+     * @return string
+     */
+    public function decompress($content)
+    {
+        $archive = $this->getArchive();
+        $mode    = $this->getMode();
+        if (file_exists($content)) {
+            $archive = $content;
+        }
+
+        if (file_exists($archive)) {
+            $handler = fopen($archive, "rb");
+            if (!$handler) {
+                require_once 'Zend/Filter/Exception.php';
+                throw new Zend_Filter_Exception("Error opening the archive '" . $archive . "'");
+            }
+
+            fseek($handler, -4, SEEK_END);
+            $packet = fread($handler, 4);
+            $bytes  = unpack("V", $packet);
+            $size   = end($bytes);
+            fclose($handler);
+
+            $file       = gzopen($archive, 'r');
+            $compressed = gzread($file, $size);
+            gzclose($file);
+        } else if ($mode == 'deflate') {
+            $compressed = gzinflate($content);
+        } else {
+            $compressed = gzuncompress($content);
+        }
+
+        if (!$compressed) {
+            require_once 'Zend/Filter/Exception.php';
+            throw new Zend_Filter_Exception('Error during compression');
+        }
+
+        return $compressed;
+    }
+
+    /**
+     * Returns the adapter name
+     *
+     * @return string
+     */
+    public function toString()
+    {
+        return 'Gz';
+    }
+}

+ 91 - 0
library/Zend/Filter/Compress/Lzf.php

@@ -0,0 +1,91 @@
+<?php
+/**
+ * Zend Framework
+ *
+ * LICENSE
+ *
+ * This source file is subject to the new BSD license that is bundled
+ * with this package in the file LICENSE.txt.
+ * It is also available through the world-wide-web at this URL:
+ * http://framework.zend.com/license/new-bsd
+ * If you did not receive a copy of the license and are unable to
+ * obtain it through the world-wide-web, please send an email
+ * to license@zend.com so we can send you a copy immediately.
+ *
+ * @category   Zend
+ * @package    Zend_Filter
+ * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ * @version    $Id: $
+ */
+
+/**
+ * @see Zend_Filter_Compress_CompressInterface
+ */
+require_once 'Zend/Filter/Compress/CompressInterface.php';
+
+/**
+ * Compression adapter for Lzf
+ *
+ * @category   Zend
+ * @package    Zend_Filter
+ * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ */
+class Zend_Filter_Compress_Lzf implements Zend_Filter_Compress_CompressInterface
+{
+    /**
+     * Class constructor
+     */
+    public function __construct()
+    {
+        if (!extension_loaded('lzf')) {
+            require_once 'Zend/Filter/Exception.php';
+            throw new Zend_Filter_Exception('This filter needs the lzf extension');
+        }
+    }
+
+    /**
+     * Compresses the given content
+     *
+     * @param  string $content
+     * @return string
+     */
+    public function compress($content)
+    {
+        $compressed = lzf_compress($content);
+        if (!$compressed) {
+            require_once 'Zend/Filter/Exception.php';
+            throw new Zend_Filter_Exception('Error during compression');
+        }
+
+        return $compressed;
+    }
+
+    /**
+     * Decompresses the given content
+     *
+     * @param  string $content
+     * @return string
+     */
+    public function decompress($content)
+    {
+        $compressed = lzf_decompress($content);
+        if (!$compressed) {
+            require_once 'Zend/Filter/Exception.php';
+            throw new Zend_Filter_Exception('Error during compression');
+        }
+
+        return $compressed;
+    }
+
+    /**
+     * Returns the adapter name
+     *
+     * @return string
+     */
+    public function toString()
+    {
+        return 'Lzf';
+    }
+}

+ 252 - 0
library/Zend/Filter/Compress/Rar.php

@@ -0,0 +1,252 @@
+<?php
+/**
+ * Zend Framework
+ *
+ * LICENSE
+ *
+ * This source file is subject to the new BSD license that is bundled
+ * with this package in the file LICENSE.txt.
+ * It is also available through the world-wide-web at this URL:
+ * http://framework.zend.com/license/new-bsd
+ * If you did not receive a copy of the license and are unable to
+ * obtain it through the world-wide-web, please send an email
+ * to license@zend.com so we can send you a copy immediately.
+ *
+ * @category   Zend
+ * @package    Zend_Filter
+ * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ * @version    $Id: $
+ */
+
+/**
+ * @see Zend_Filter_Compress_CompressAbstract
+ */
+require_once 'Zend/Filter/Compress/CompressAbstract.php';
+
+/**
+ * Compression adapter for Rar
+ *
+ * @category   Zend
+ * @package    Zend_Filter
+ * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ */
+class Zend_Filter_Compress_Rar extends Zend_Filter_Compress_CompressAbstract
+{
+    /**
+     * Compression Options
+     * array(
+     *     'callback' => Callback for compression
+     *     'archive'  => Archive to use
+     *     'password' => Password to use
+     *     'target'   => Target to write the files to
+     * )
+     *
+     * @var array
+     */
+    protected $_options = array(
+        'callback' => null,
+        'archive'  => null,
+        'password' => null,
+        'target'   => '.',
+    );
+
+    /**
+     * Class constructor
+     *
+     * @param array $options (Optional) Options to set
+     */
+    public function __construct($options = null)
+    {
+        if (!extension_loaded('rar')) {
+            require_once 'Zend/Filter/Exception.php';
+            throw new Zend_Filter_Exception('This filter needs the rar extension');
+        }
+        parent::__construct($options);
+    }
+
+    /**
+     * Returns the set callback for compression
+     *
+     * @return string
+     */
+    public function getCallback()
+    {
+        return $this->_options['callback'];
+    }
+
+    /**
+     * Sets the callback to use
+     *
+     * @param string $callback
+     * @return Zend_Filter_Compress_Rar
+     */
+    public function setCallback($callback)
+    {
+        if (!is_callable($callback)) {
+            require_once 'Zend/Filter/Exception.php';
+            throw new Zend_Filter_Exception('Callback can not be accessed');
+        }
+
+        $this->_options['callback'] = $callback;
+        return $this;
+    }
+
+    /**
+     * Returns the set archive
+     *
+     * @return string
+     */
+    public function getArchive()
+    {
+        return $this->_options['archive'];
+    }
+
+    /**
+     * Sets the archive to use for de-/compression
+     *
+     * @param string $archive Archive to use
+     * @return Zend_Filter_Compress_Rar
+     */
+    public function setArchive($archive)
+    {
+        $archive = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $archive);
+        $this->_options['archive'] = (string) $archive;
+
+        return $this;
+    }
+
+    /**
+     * Returns the set password
+     *
+     * @return string
+     */
+    public function getPassword()
+    {
+        return $this->_options['password'];
+    }
+
+    /**
+     * Sets the password to use
+     *
+     * @param string $password
+     * @return Zend_Filter_Compress_Rar
+     */
+    public function setPassword($password)
+    {
+        $this->_options['password'] = (string) $password;
+        return $this;
+    }
+
+    /**
+     * Returns the set targetpath
+     *
+     * @return string
+     */
+    public function getTarget()
+    {
+        return $this->_options['target'];
+    }
+
+    /**
+     * Sets the targetpath to use
+     *
+     * @param string $target
+     * @return Zend_Filter_Compress_Rar
+     */
+    public function setTarget($target)
+    {
+        if (!file_exists(dirname($target))) {
+            require_once 'Zend/Filter/Exception.php';
+            throw new Zend_Filter_Exception("The directory '$target' does not exist");
+        }
+
+        $target = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $target);
+        $this->_options['target'] = (string) $target;
+        return $this;
+    }
+
+    /**
+     * Compresses the given content
+     *
+     * @param  string|array $content
+     * @return string
+     */
+    public function compress($content)
+    {
+        $callback = $this->getCallback();
+        if (is_null($callback)) {
+            require_once 'Zend/Filter/Exception.php';
+            throw new Zend_Filter_Exception('No compression callback available');
+        }
+
+        $options = $this->getOptions();
+        unset($options['callback']);
+
+        $result = call_user_func($callback, $options, $content);
+        if ($result !== true) {
+            require_once 'Zend/Filter/Exception.php';
+            throw new Zend_Filter_Exception('Error compressing the RAR Archive');
+        }
+
+        return $this->getArchive();
+    }
+
+    /**
+     * Decompresses the given content
+     *
+     * @param  string $content
+     * @return boolean
+     */
+    public function decompress($content)
+    {
+        $archive = $this->getArchive();
+        if (file_exists($content)) {
+            $archive = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, realpath($content));
+        } elseif (empty($archive) || !file_exists($archive)) {
+            require_once 'Zend/Filter/Exception.php';
+            throw new Zend_Filter_Exception('RAR Archive not found');
+        }
+
+        $password = $this->getPassword();
+        if (!is_null($password)) {
+            $archive = rar_open($archive, $password);
+        } else {
+            $archive = rar_open($archive);
+        }
+
+        if (!$archive) {
+            require_once 'Zend/Filter/Exception.php';
+            throw new Zend_Filter_Exception("Error opening the RAR Archive");
+        }
+
+        $target = $this->getTarget();
+        if (!is_dir($target)) {
+            $target = dirname($target);
+        }
+
+        $filelist = rar_list($archive);
+        if (!$filelist) {
+            require_once 'Zend/Filter/Exception.php';
+            throw new Zend_Filter_Exception("Error reading the RAR Archive");
+        }
+
+        foreach($filelist as $file) {
+            $file->extract($target);
+        }
+
+        rar_close($archive);
+        return true;
+    }
+
+    /**
+     * Returns the adapter name
+     *
+     * @return string
+     */
+    public function toString()
+    {
+        return 'Rar';
+    }
+}

+ 245 - 0
library/Zend/Filter/Compress/Tar.php

@@ -0,0 +1,245 @@
+<?php
+/**
+ * Zend Framework
+ *
+ * LICENSE
+ *
+ * This source file is subject to the new BSD license that is bundled
+ * with this package in the file LICENSE.txt.
+ * It is also available through the world-wide-web at this URL:
+ * http://framework.zend.com/license/new-bsd
+ * If you did not receive a copy of the license and are unable to
+ * obtain it through the world-wide-web, please send an email
+ * to license@zend.com so we can send you a copy immediately.
+ *
+ * @category   Zend
+ * @package    Zend_Filter
+ * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ * @version    $Id: $
+ */
+
+/**
+ * @see Zend_Filter_Compress_CompressAbstract
+ */
+require_once 'Zend/Filter/Compress/CompressAbstract.php';
+
+/**
+ * Compression adapter for Tar
+ *
+ * @category   Zend
+ * @package    Zend_Filter
+ * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ */
+class Zend_Filter_Compress_Tar extends Zend_Filter_Compress_CompressAbstract
+{
+    /**
+     * Compression Options
+     * array(
+     *     'archive'  => Archive to use
+     *     'target'   => Target to write the files to
+     * )
+     *
+     * @var array
+     */
+    protected $_options = array(
+        'archive'  => null,
+        'target'   => '.',
+        'mode'     => null,
+    );
+
+    /**
+     * Class constructor
+     *
+     * @param array $options (Optional) Options to set
+     */
+    public function __construct($options = null)
+    {
+        if (!class_exists('Archive_Tar')) {
+            require_once 'Zend/Loader.php';
+            try {
+                Zend_Loader::loadClass('Archive_Tar');
+            } catch (Zend_Exception $e) {
+                require_once 'Zend/Filter/Exception.php';
+                throw new Zend_Filter_Exception('This filter needs PEARs Archive_Tar');
+            }
+        }
+
+        parent::__construct($options);
+    }
+
+    /**
+     * Returns the set archive
+     *
+     * @return string
+     */
+    public function getArchive()
+    {
+        return $this->_options['archive'];
+    }
+
+    /**
+     * Sets the archive to use for de-/compression
+     *
+     * @param string $archive Archive to use
+     * @return Zend_Filter_Compress_Tar
+     */
+    public function setArchive($archive)
+    {
+        $archive = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $archive);
+        $this->_options['archive'] = (string) $archive;
+
+        return $this;
+    }
+
+    /**
+     * Returns the set targetpath
+     *
+     * @return string
+     */
+    public function getTarget()
+    {
+        return $this->_options['target'];
+    }
+
+    /**
+     * Sets the targetpath to use
+     *
+     * @param string $target
+     * @return Zend_Filter_Compress_Tar
+     */
+    public function setTarget($target)
+    {
+        if (!file_exists(dirname($target))) {
+            require_once 'Zend/Filter/Exception.php';
+            throw new Zend_Filter_Exception("The directory '$target' does not exist");
+        }
+
+        $target = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $target);
+        $this->_options['target'] = (string) $target;
+        return $this;
+    }
+
+    /**
+     * Returns the set compression mode
+     */
+    public function getMode()
+    {
+        return $this->_options['mode'];
+    }
+
+    /**
+     * Compression mode to use
+     * Eighter Gz or Bz2
+     *
+     * @param string $mode
+     */
+    public function setMode($mode)
+    {
+        $mode = ucfirst(strtolower($mode));
+        if (($mode != 'Bz2') && ($mode != 'Gz')) {
+            require_once 'Zend/Filter/Exception.php';
+            throw new Zend_Filter_Exception("The mode '$mode' is unknown");
+        }
+
+        if (($mode == 'Bz2') && (!extension_loaded('bz2'))) {
+            require_once 'Zend/Filter/Exception.php';
+            throw new Zend_Filter_Exception('This mode needs the bz2 extension');
+        }
+
+        if (($mode == 'Gz') && (!extension_loaded('zlib'))) {
+            require_once 'Zend/Filter/Exception.php';
+            throw new Zend_Filter_Exception('This mode needs the zlib extension');
+        }
+    }
+
+    /**
+     * Compresses the given content
+     *
+     * @param  string $content
+     * @return string
+     */
+    public function compress($content)
+    {
+        $archive = new Archive_Tar($this->getArchive(), $this->getMode());
+        if (!file_exists($content)) {
+            $file = $this->getTarget();
+            if (is_dir($file)) {
+                $file .= DIRECTORY_SEPARATOR . "tar.tmp";
+            }
+
+            $result = file_put_contents($file, $content);
+            if ($result === false) {
+                require_once 'Zend/Filter/Exception.php';
+                throw new Zend_Filter_Exception('Error creating the temporary file');
+            }
+
+            $content = $file;
+        }
+
+        if (is_dir($content)) {
+            // collect all file infos
+            foreach (new RecursiveIteratorIterator(
+                        new RecursiveDirectoryIterator($content, RecursiveDirectoryIterator::KEY_AS_PATHNAME),
+                        RecursiveIteratorIterator::SELF_FIRST
+                    ) as $directory => $info
+            ) {
+                if ($info->isFile()) {
+                    $file[] = $directory;
+                }
+            }
+
+            $content = $file;
+        }
+
+        $result  = $archive->create($content);
+        if ($result === false) {
+            require_once 'Zend/Filter/Exception.php';
+            throw new Zend_Filter_Exception('Error creating the Tar archive');
+        }
+
+        return $this->getArchive();
+    }
+
+    /**
+     * Decompresses the given content
+     *
+     * @param  string $content
+     * @return boolean
+     */
+    public function decompress($content)
+    {
+        $archive = $this->getArchive();
+        if (file_exists($content)) {
+            $archive = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, realpath($content));
+        } elseif (empty($archive) || !file_exists($archive)) {
+            require_once 'Zend/Filter/Exception.php';
+            throw new Zend_Filter_Exception('Tar Archive not found');
+        }
+
+        $archive = new Archive_Tar($archive, $this->getMode());
+        $target  = $this->getTarget();
+        if (!is_dir($target)) {
+            $target = dirname($target);
+        }
+
+        $result = $archive->extract($target);
+        if ($result === false) {
+            require_once 'Zend/Filter/Exception.php';
+            throw new Zend_Filter_Exception('Error while extracting the Tar archive');
+        }
+
+        return true;
+    }
+
+    /**
+     * Returns the adapter name
+     *
+     * @return string
+     */
+    public function toString()
+    {
+        return 'Tar';
+    }
+}

+ 333 - 0
library/Zend/Filter/Compress/Zip.php

@@ -0,0 +1,333 @@
+<?php
+/**
+ * Zend Framework
+ *
+ * LICENSE
+ *
+ * This source file is subject to the new BSD license that is bundled
+ * with this package in the file LICENSE.txt.
+ * It is also available through the world-wide-web at this URL:
+ * http://framework.zend.com/license/new-bsd
+ * If you did not receive a copy of the license and are unable to
+ * obtain it through the world-wide-web, please send an email
+ * to license@zend.com so we can send you a copy immediately.
+ *
+ * @category   Zend
+ * @package    Zend_Filter
+ * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ * @version    $Id: $
+ */
+
+/**
+ * @see Zend_Filter_Compress_CompressAbstract
+ */
+require_once 'Zend/Filter/Compress/CompressAbstract.php';
+
+/**
+ * Compression adapter for zip
+ *
+ * @category   Zend
+ * @package    Zend_Filter
+ * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ */
+class Zend_Filter_Compress_Zip extends Zend_Filter_Compress_CompressAbstract
+{
+    /**
+     * Compression Options
+     * array(
+     *     'archive'  => Archive to use
+     *     'password' => Password to use
+     *     'target'   => Target to write the files to
+     * )
+     *
+     * @var array
+     */
+    protected $_options = array(
+        'archive' => null,
+        'target'  => '.',
+    );
+
+    /**
+     * Class constructor
+     *
+     * @param string|array $options (Optional) Options to set
+     */
+    public function __construct($options = null)
+    {
+        if (!extension_loaded('zip')) {
+            require_once 'Zend/Filter/Exception.php';
+            throw new Zend_Filter_Exception('This filter needs the zip extension');
+        }
+        parent::__construct($options);
+    }
+
+    /**
+     * Returns the set archive
+     *
+     * @return string
+     */
+    public function getArchive()
+    {
+        return $this->_options['archive'];
+    }
+
+    /**
+     * Sets the archive to use for de-/compression
+     *
+     * @param string $archive Archive to use
+     * @return Zend_Filter_Compress_Rar
+     */
+    public function setArchive($archive)
+    {
+        $archive = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $archive);
+        $this->_options['archive'] = (string) $archive;
+
+        return $this;
+    }
+
+    /**
+     * Returns the set targetpath
+     *
+     * @return string
+     */
+    public function getTarget()
+    {
+        return $this->_options['target'];
+    }
+
+    /**
+     * Sets the target to use
+     *
+     * @param string $target
+     * @return Zend_Filter_Compress_Rar
+     */
+    public function setTarget($target)
+    {
+        if (!file_exists(dirname($target))) {
+            require_once 'Zend/Filter/Exception.php';
+            throw new Zend_Filter_Exception("The directory '$target' does not exist");
+        }
+
+        $target = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $target);
+        $this->_options['target'] = (string) $target;
+        return $this;
+    }
+
+    /**
+     * Compresses the given content
+     *
+     * @param  string $content
+     * @return string Compressed archive
+     */
+    public function compress($content)
+    {
+        $zip = new ZipArchive();
+        $res = $zip->open($this->getArchive(), ZipArchive::CREATE | ZipArchive::OVERWRITE);
+
+        if ($res !== true) {
+            require_once 'Zend/Filter/Exception.php';
+            throw new Zend_Filter_Exception($this->_errorString($res));
+        }
+
+        if (file_exists($content)) {
+            $content  = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, realpath($content));
+            $basename = substr($content, strrpos($content, DIRECTORY_SEPARATOR) + 1);
+            if (is_dir($content)) {
+                $index    = strrpos($content, DIRECTORY_SEPARATOR) + 1;
+                $content .= DIRECTORY_SEPARATOR;
+                $stack    = array($content);
+                while (!empty($stack)) {
+                    $current = array_pop($stack);
+                    $files   = array();
+
+                    $dir = dir($current);
+                    while (false !== ($node = $dir->read())) {
+                        if (($node == '.') || ($node == '..')) {
+                            continue;
+                        }
+
+                        if (is_dir($current . $node)) {
+                            array_push($stack, $current . $node . DIRECTORY_SEPARATOR);
+                        }
+
+                        if (is_file($current . $node)) {
+                            $files[] = $node;
+                        }
+                    }
+
+                    $local = substr($current, $index);
+                    $zip->addEmptyDir(substr($local, 0, -1));
+
+                    foreach ($files as $file) {
+                        $zip->addFile($current . $file, $local . $file);
+                        if ($res !== true) {
+                            require_once 'Zend/Filter/Exception.php';
+                            throw new Zend_Filter_Exception($this->_errorString($res));
+                        }
+                    }
+                }
+            } else {
+                $res = $zip->addFile($content, $basename);
+                if ($res !== true) {
+                    require_once 'Zend/Filter/Exception.php';
+                    throw new Zend_Filter_Exception($this->_errorString($res));
+                }
+            }
+        } else {
+            $file = $this->getTarget();
+            if (!is_dir($file)) {
+                $file = basename($file);
+            } else {
+                $file = "zip.tmp";
+            }
+
+            $res = $zip->addFromString($file, $content);
+            if ($res !== true) {
+                require_once 'Zend/Filter/Exception.php';
+                throw new Zend_Filter_Exception($this->_errorString($res));
+            }
+        }
+
+        $zip->close();
+        return $this->_options['archive'];
+    }
+
+    /**
+     * Decompresses the given content
+     *
+     * @param  string $content
+     * @return string
+     */
+    public function decompress($content)
+    {
+        $archive = $this->getArchive();
+        if (file_exists($content)) {
+            $archive = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, realpath($content));
+        } elseif (empty($archive) || !file_exists($archive)) {
+            require_once 'Zend/Filter/Exception.php';
+            throw new Zend_Filter_Exception('ZIP Archive not found');
+        }
+
+        $zip = new ZipArchive();
+        $res = $zip->open($archive);
+
+        $target = $this->getTarget();
+        if (!is_dir($target)) {
+            $target = dirname($target);
+        }
+
+        $target = $target . DIRECTORY_SEPARATOR;
+        if (empty($target)) {
+            require_once 'Zend/Filter/Exception.php';
+            throw new Zend_Filter_Exception('No target for ZIP decompression set');
+        }
+
+        if ($res !== true) {
+            require_once 'Zend/Filter/Exception.php';
+            throw new Zend_Filter_Exception($this->_errorString($res));
+        }
+
+        $res = @$zip->extractTo($target);
+        if ($res !== true) {
+            require_once 'Zend/Filter/Exception.php';
+            throw new Zend_Filter_Exception($this->_errorString($res));
+        }
+
+        $zip->close();
+        return $target;
+    }
+
+    /**
+     * Returns the proper string based on the given error constant
+     *
+     * @param string $error
+     */
+    protected function _errorString($error)
+    {
+        switch($error) {
+            case ZipArchive::ER_MULTIDISK :
+                return 'Multidisk ZIP Archives not supported';
+
+            case ZipArchive::ER_RENAME :
+                return 'Failed to rename the temporary file for ZIP';
+
+            case ZipArchive::ER_CLOSE :
+                return 'Failed to close the ZIP Archive';
+
+            case ZipArchive::ER_SEEK :
+                return 'Failure while seeking the ZIP Archive';
+
+            case ZipArchive::ER_READ :
+                return 'Failure while reading the ZIP Archive';
+
+            case ZipArchive::ER_WRITE :
+                return 'Failure while writing the ZIP Archive';
+
+            case ZipArchive::ER_CRC :
+                return 'CRC failure within the ZIP Archive';
+
+            case ZipArchive::ER_ZIPCLOSED :
+                return 'ZIP Archive already closed';
+
+            case ZipArchive::ER_NOENT :
+                return 'No such file within the ZIP Archive';
+
+            case ZipArchive::ER_EXISTS :
+                return 'ZIP Archive already exists';
+
+            case ZipArchive::ER_OPEN :
+                return 'Can not open ZIP Archive';
+
+            case ZipArchive::ER_TMPOPEN :
+                return 'Failure creating temporary ZIP Archive';
+
+            case ZipArchive::ER_ZLIB :
+                return 'ZLib Problem';
+
+            case ZipArchive::ER_MEMORY :
+                return 'Memory allocation problem while working on a ZIP Archive';
+
+            case ZipArchive::ER_CHANGED :
+                return 'ZIP Entry has been changed';
+
+            case ZipArchive::ER_COMPNOTSUPP :
+                return 'Compression method not supported within ZLib';
+
+            case ZipArchive::ER_EOF :
+                return 'Premature EOF within ZIP Archive';
+
+            case ZipArchive::ER_INVAL :
+                return 'Invalid argument for ZLIB';
+
+            case ZipArchive::ER_NOZIP :
+                return 'Given file is no zip archive';
+
+            case ZipArchive::ER_INTERNAL :
+                return 'Internal error while working on a ZIP Archive';
+
+            case ZipArchive::ER_INCONS :
+                return 'Inconsistent ZIP archive';
+
+            case ZipArchive::ER_REMOVE :
+                return 'Can not remove ZIP Archive';
+
+            case ZipArchive::ER_DELETED :
+                return 'ZIP Entry has been deleted';
+
+            default :
+                return 'Unknown error within ZIP Archive';
+        }
+    }
+
+    /**
+     * Returns the adapter name
+     *
+     * @return string
+     */
+    public function toString()
+    {
+        return 'Zip';
+    }
+}

+ 49 - 0
library/Zend/Filter/Decompress.php

@@ -0,0 +1,49 @@
+<?php
+/**
+ * Zend Framework
+ *
+ * LICENSE
+ *
+ * This source file is subject to the new BSD license that is bundled
+ * with this package in the file LICENSE.txt.
+ * It is also available through the world-wide-web at this URL:
+ * http://framework.zend.com/license/new-bsd
+ * If you did not receive a copy of the license and are unable to
+ * obtain it through the world-wide-web, please send an email
+ * to license@zend.com so we can send you a copy immediately.
+ *
+ * @category   Zend
+ * @package    Zend_Filter
+ * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ * @version    $Id: $
+ */
+
+/**
+ * @see Zend_Filter_Compress
+ */
+require_once 'Zend/Filter/Compress.php';
+
+/**
+ * Decompresses a given string
+ *
+ * @category   Zend
+ * @package    Zend_Filter
+ * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ */
+class Zend_Filter_Decompress extends Zend_Filter_Compress
+{
+    /**
+     * Defined by Zend_Filter_Interface
+     *
+     * Decompresses the content $value with the defined settings
+     *
+     * @param  string $value Content to decompress
+     * @return string The decompressed content
+     */
+    public function filter($value)
+    {
+        return $this->getAdapter()->decompress($value);
+    }
+}

+ 28 - 0
tests/Zend/Filter/AllTests.php

@@ -52,6 +52,16 @@ require_once 'Zend/Filter/BaseNameTest.php';
 require_once 'Zend/Filter/CallbackTest.php';
 require_once 'Zend/Filter/CallbackTest.php';
 
 
 /**
 /**
+ * @see Zend_Filter_CompressTest
+ */
+require_once 'Zend/Filter/CompressTest.php';
+
+/**
+ * @see Zend_Filter_DecompressTest
+ */
+require_once 'Zend/Filter/DecompressTest.php';
+
+/**
  * @see Zend_Filter_DecryptTest
  * @see Zend_Filter_DecryptTest
  */
  */
 require_once 'Zend/Filter/DecryptTest.php';
 require_once 'Zend/Filter/DecryptTest.php';
@@ -132,6 +142,16 @@ require_once 'Zend/Filter/StripNewlinesTest.php';
 require_once 'Zend/Filter/StripTagsTest.php';
 require_once 'Zend/Filter/StripTagsTest.php';
 
 
 /**
 /**
+ * Compress filter tests
+ */
+require_once 'Zend/Filter/Compress/Bz2Test.php';
+require_once 'Zend/Filter/Compress/GzTest.php';
+require_once 'Zend/Filter/Compress/LzfTest.php';
+require_once 'Zend/Filter/Compress/RarTest.php';
+require_once 'Zend/Filter/Compress/TarTest.php';
+require_once 'Zend/Filter/Compress/ZipTest.php';
+
+/**
  * Encrypt filter tests
  * Encrypt filter tests
  */
  */
 require_once 'Zend/Filter/Encrypt/McryptTest.php';
 require_once 'Zend/Filter/Encrypt/McryptTest.php';
@@ -195,6 +215,8 @@ class Zend_Filter_AllTests
         $suite->addTestSuite('Zend_Filter_AlphaTest');
         $suite->addTestSuite('Zend_Filter_AlphaTest');
         $suite->addTestSuite('Zend_Filter_BaseNameTest');
         $suite->addTestSuite('Zend_Filter_BaseNameTest');
         $suite->addTestSuite('Zend_Filter_CallbackTest');
         $suite->addTestSuite('Zend_Filter_CallbackTest');
+        $suite->addTestSuite('Zend_Filter_CompressTest');
+        $suite->addTestSuite('Zend_Filter_DecompressTest');
         $suite->addTestSuite('Zend_Filter_DecryptTest');
         $suite->addTestSuite('Zend_Filter_DecryptTest');
         $suite->addTestSuite('Zend_Filter_DigitsTest');
         $suite->addTestSuite('Zend_Filter_DigitsTest');
         $suite->addTestSuite('Zend_Filter_DirTest');
         $suite->addTestSuite('Zend_Filter_DirTest');
@@ -210,6 +232,12 @@ class Zend_Filter_AllTests
         $suite->addTestSuite('Zend_Filter_StringTrimTest');
         $suite->addTestSuite('Zend_Filter_StringTrimTest');
         $suite->addTestSuite('Zend_Filter_StripNewlinesTest');
         $suite->addTestSuite('Zend_Filter_StripNewlinesTest');
         $suite->addTestSuite('Zend_Filter_StripTagsTest');
         $suite->addTestSuite('Zend_Filter_StripTagsTest');
+        $suite->addTestSuite('Zend_Filter_Compress_Bz2Test');
+        $suite->addTestSuite('Zend_Filter_Compress_GzTest');
+        $suite->addTestSuite('Zend_Filter_Compress_LzfTest');
+        $suite->addTestSuite('Zend_Filter_Compress_RarTest');
+        $suite->addTestSuite('Zend_Filter_Compress_TarTest');
+        $suite->addTestSuite('Zend_Filter_Compress_ZipTest');
         $suite->addTestSuite('Zend_Filter_Encrypt_McryptTest');
         $suite->addTestSuite('Zend_Filter_Encrypt_McryptTest');
         $suite->addTestSuite('Zend_Filter_Encrypt_OpensslTest');
         $suite->addTestSuite('Zend_Filter_Encrypt_OpensslTest');
         $suite->addTestSuite('Zend_Filter_File_DecryptTest');
         $suite->addTestSuite('Zend_Filter_File_DecryptTest');

+ 103 - 0
tests/Zend/Filter/Compress/AllTests.php

@@ -0,0 +1,103 @@
+<?php
+/**
+ * Zend Framework
+ *
+ * LICENSE
+ *
+ * This source file is subject to the new BSD license that is bundled
+ * with this package in the file LICENSE.txt.
+ * It is also available through the world-wide-web at this URL:
+ * http://framework.zend.com/license/new-bsd
+ * If you did not receive a copy of the license and are unable to
+ * obtain it through the world-wide-web, please send an email
+ * to license@zend.com so we can send you a copy immediately.
+ *
+ * @category   Zend
+ * @package    Zend_Filter
+ * @subpackage UnitTests
+ * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ * @version    $Id: AllTests.php 16225 2009-06-21 20:34:55Z thomas $
+ */
+
+if (!defined('PHPUnit_MAIN_METHOD')) {
+    define('PHPUnit_MAIN_METHOD', 'Zend_Filter_Compress_AllTests::main');
+}
+
+/**
+ * Test helper
+ */
+require_once dirname(__FILE__) . '/../../../TestHelper.php';
+
+/**
+ * @see Zend_Filter_Compress_Bz2Test
+ */
+require_once 'Zend/Filter/Compress/Bz2Test.php';
+
+/**
+ * @see Zend_Filter_Compress_GzTest
+ */
+require_once 'Zend/Filter/Compress/GzTest.php';
+
+/**
+ * @see Zend_Filter_Compress_LzfTest
+ */
+require_once 'Zend/Filter/Compress/LzfTest.php';
+
+/**
+ * @see Zend_Filter_Compress_RarTest
+ */
+require_once 'Zend/Filter/Compress/RarTest.php';
+
+/**
+ * @see Zend_Filter_Compress_TarTest
+ */
+require_once 'Zend/Filter/Compress/TarTest.php';
+
+/**
+ * @see Zend_Filter_Compress_ZipTest
+ */
+require_once 'Zend/Filter/Compress/ZipTest.php';
+
+/**
+ * @category   Zend
+ * @package    Zend_Filter
+ * @subpackage UnitTests
+ * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ */
+class Zend_Filter_Compress_AllTests
+{
+    /**
+     * Runs this test suite
+     *
+     * @return void
+     */
+    public static function main()
+    {
+        PHPUnit_TextUI_TestRunner::run(self::suite());
+    }
+
+    /**
+     * Creates and returns this test suite
+     *
+     * @return PHPUnit_Framework_TestSuite
+     */
+    public static function suite()
+    {
+        $suite = new PHPUnit_Framework_TestSuite('Zend Framework - Zend_Filter_Compress');
+
+        $suite->addTestSuite('Zend_Filter_Compress_Bz2Test');
+        $suite->addTestSuite('Zend_Filter_Compress_GzTest');
+        $suite->addTestSuite('Zend_Filter_Compress_LzfTest');
+        $suite->addTestSuite('Zend_Filter_Compress_RarTest');
+        $suite->addTestSuite('Zend_Filter_Compress_TarTest');
+        $suite->addTestSuite('Zend_Filter_Compress_ZipTest');
+
+        return $suite;
+    }
+}
+
+if (PHPUnit_MAIN_METHOD == 'Zend_Filter_Compress_AllTests::main') {
+    Zend_Filter_Compress_AllTests::main();
+}

+ 213 - 0
tests/Zend/Filter/Compress/Bz2Test.php

@@ -0,0 +1,213 @@
+<?php
+/**
+ * Zend Framework
+ *
+ * LICENSE
+ *
+ * This source file is subject to the new BSD license that is bundled
+ * with this package in the file LICENSE.txt.
+ * It is also available through the world-wide-web at this URL:
+ * http://framework.zend.com/license/new-bsd
+ * If you did not receive a copy of the license and are unable to
+ * obtain it through the world-wide-web, please send an email
+ * to license@zend.com so we can send you a copy immediately.
+ *
+ * @category   Zend
+ * @package    Zend_Filter
+ * @subpackage UnitTests
+ * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ * @version    $Id: $
+ */
+
+if (!defined('PHPUnit_MAIN_METHOD')) {
+    define('PHPUnit_MAIN_METHOD', 'Zend_Filter_Compress_Bz2Test::main');
+}
+
+/**
+ * Test helper
+ */
+require_once dirname(__FILE__) . '/../../../TestHelper.php';
+
+/**
+ * @see Zend_Filter_Compress_Bz2
+ */
+require_once 'Zend/Filter/Compress/Bz2.php';
+
+/**
+ * @category   Zend
+ * @package    Zend_Filter
+ * @subpackage UnitTests
+ * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ */
+class Zend_Filter_Compress_Bz2Test extends PHPUnit_Framework_TestCase
+{
+    /**
+     * Runs this test suite
+     *
+     * @return void
+     */
+    public static function main()
+    {
+        $suite  = new PHPUnit_Framework_TestSuite('Zend_Filter_Compress_Bz2Test');
+        $result = PHPUnit_TextUI_TestRunner::run($suite);
+    }
+
+    public function setUp()
+    {
+        if (!extension_loaded('bz2')) {
+            $this->markTestSkipped('This adapter needs the bz2 extension');
+        }
+    }
+
+    public function tearDown()
+    {
+        if (file_exists(dirname(__FILE__) . '/../_files/compressed.bz2')) {
+            unlink(dirname(__FILE__) . '/../_files/compressed.bz2');
+        }
+    }
+
+    /**
+     * Basic usage
+     *
+     * @return void
+     */
+    public function testBasicUsage()
+    {
+        $filter  = new Zend_Filter_Compress_Bz2();
+
+        $content = $filter->compress('compress me');
+        $this->assertNotEquals('compress me', $content);
+
+        $content = $filter->decompress($content);
+        $this->assertEquals('compress me', $content);
+    }
+
+    /**
+     * Setting Options
+     *
+     * @return void
+     */
+    public function testBz2GetSetOptions()
+    {
+        $filter = new Zend_Filter_Compress_Bz2();
+        $this->assertEquals(array('blocksize' => 4, 'archive' => null), $filter->getOptions());
+
+        $this->assertEquals(4, $filter->getOptions('blocksize'));
+
+        $this->assertNull($filter->getOptions('nooption'));
+
+        $filter->setOptions(array('blocksize' => 6));
+        $this->assertEquals(6, $filter->getOptions('blocksize'));
+
+        $filter->setOptions(array('archive' => 'test.txt'));
+        $this->assertEquals('test.txt', $filter->getOptions('archive'));
+
+        $filter->setOptions(array('nooption' => 0));
+        $this->assertNull($filter->getOptions('nooption'));
+    }
+
+    /**
+     * Setting Options through constructor
+     *
+     * @return void
+     */
+    public function testBz2GetSetOptionsInConstructor()
+    {
+        $filter2= new Zend_Filter_Compress_Bz2(array('blocksize' => 8));
+        $this->assertEquals(array('blocksize' => 8, 'archive' => null), $filter2->getOptions());
+    }
+
+    /**
+     * Setting Blocksize
+     *
+     * @return void
+     */
+    public function testBz2GetSetBlocksize()
+    {
+        $filter = new Zend_Filter_Compress_Bz2();
+        $this->assertEquals(4, $filter->getBlocksize());
+        $filter->setBlocksize(6);
+        $this->assertEquals(6, $filter->getOptions('blocksize'));
+
+        try {
+            $filter->setBlocksize(15);
+            $this->fail('Exception expected');
+        } catch(Zend_Filter_Exception $e) {
+            $this->assertContains('must be between', $e->getMessage());
+        }
+    }
+
+    /**
+     * Setting Archive
+     *
+     * @return void
+     */
+    public function testBz2GetSetArchive()
+    {
+        $filter = new Zend_Filter_Compress_Bz2();
+        $this->assertEquals(null, $filter->getArchive());
+        $filter->setArchive('Testfile.txt');
+        $this->assertEquals('Testfile.txt', $filter->getArchive());
+        $this->assertEquals('Testfile.txt', $filter->getOptions('archive'));
+    }
+
+    /**
+     * Setting Archive
+     *
+     * @return void
+     */
+    public function testBz2CompressToFile()
+    {
+        $filter   = new Zend_Filter_Compress_Bz2();
+        $archive = dirname(__FILE__) . '/../_files/compressed.bz2';
+        $filter->setArchive($archive);
+
+        $content = $filter->compress('compress me');
+        $this->assertTrue($content);
+
+        $filter2  = new Zend_Filter_Compress_Bz2();
+        $content2 = $filter2->decompress($archive);
+        $this->assertEquals('compress me', $content2);
+
+        $filter3 = new Zend_Filter_Compress_Bz2();
+        $filter3->setArchive($archive);
+        $content3 = $filter3->decompress(null);
+        $this->assertEquals('compress me', $content3);
+    }
+
+    /**
+     * testing toString
+     *
+     * @return void
+     */
+    public function testBz2ToString()
+    {
+        $filter = new Zend_Filter_Compress_Bz2();
+        $this->assertEquals('Bz2', $filter->toString());
+    }
+
+    /**
+     * Basic usage
+     *
+     * @return void
+     */
+    public function testBz2DecompressArchive()
+    {
+        $filter   = new Zend_Filter_Compress_Bz2();
+        $archive = dirname(__FILE__) . '/../_files/compressed.bz2';
+        $filter->setArchive($archive);
+
+        $content = $filter->compress('compress me');
+        $this->assertTrue($content);
+
+        $filter2  = new Zend_Filter_Compress_Bz2();
+        $content2 = $filter2->decompress($archive);
+        $this->assertEquals('compress me', $content2);
+    }
+}
+
+if (PHPUnit_MAIN_METHOD == 'Zend_Filter_Compress_Bz2Test::main') {
+    Zend_Filter_Compress_Bz2Test::main();
+}

+ 232 - 0
tests/Zend/Filter/Compress/GzTest.php

@@ -0,0 +1,232 @@
+<?php
+/**
+ * Zend Framework
+ *
+ * LICENSE
+ *
+ * This source file is subject to the new BSD license that is bundled
+ * with this package in the file LICENSE.txt.
+ * It is also available through the world-wide-web at this URL:
+ * http://framework.zend.com/license/new-bsd
+ * If you did not receive a copy of the license and are unable to
+ * obtain it through the world-wide-web, please send an email
+ * to license@zend.com so we can send you a copy immediately.
+ *
+ * @category   Zend
+ * @package    Zend_Filter
+ * @subpackage UnitTests
+ * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ * @version    $Id: $
+ */
+
+if (!defined('PHPUnit_MAIN_METHOD')) {
+    define('PHPUnit_MAIN_METHOD', 'Zend_Filter_Compress_GzTest::main');
+}
+
+/**
+ * Test helper
+ */
+require_once dirname(__FILE__) . '/../../../TestHelper.php';
+
+/**
+ * @see Zend_Filter_Compress_Gz
+ */
+require_once 'Zend/Filter/Compress/Gz.php';
+
+/**
+ * @category   Zend
+ * @package    Zend_Filter
+ * @subpackage UnitTests
+ * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ */
+class Zend_Filter_Compress_GzTest extends PHPUnit_Framework_TestCase
+{
+    /**
+     * Runs this test suite
+     *
+     * @return void
+     */
+    public static function main()
+    {
+        $suite  = new PHPUnit_Framework_TestSuite('Zend_Filter_Compress_GzTest');
+        $result = PHPUnit_TextUI_TestRunner::run($suite);
+    }
+
+    public function setUp()
+    {
+        if (!extension_loaded('zlib')) {
+            $this->markTestSkipped('This adapter needs the zlib extension');
+        }
+    }
+
+    public function tearDown()
+    {
+        if (file_exists(dirname(__FILE__) . '/../_files/compressed.gz')) {
+            unlink(dirname(__FILE__) . '/../_files/compressed.gz');
+        }
+    }
+
+    /**
+     * Basic usage
+     *
+     * @return void
+     */
+    public function testBasicUsage()
+    {
+        $filter  = new Zend_Filter_Compress_Gz();
+
+        $content = $filter->compress('compress me');
+        $this->assertNotEquals('compress me', $content);
+
+        $content = $filter->decompress($content);
+        $this->assertEquals('compress me', $content);
+    }
+
+    /**
+     * Setting Options
+     *
+     * @return void
+     */
+    public function testGzGetSetOptions()
+    {
+        $filter = new Zend_Filter_Compress_Gz();
+        $this->assertEquals(array('mode' => 'compress', 'level' => 9, 'archive' => null), $filter->getOptions());
+
+        $this->assertEquals(9, $filter->getOptions('level'));
+
+        $this->assertNull($filter->getOptions('nooption'));
+        $filter->setOptions(array('nooption' => 'foo'));
+        $this->assertNull($filter->getOptions('nooption'));
+
+        $filter->setOptions(array('level' => 6));
+        $this->assertEquals(6, $filter->getOptions('level'));
+
+        $filter->setOptions(array('mode' => 'deflate'));
+        $this->assertEquals('deflate', $filter->getOptions('mode'));
+
+        $filter->setOptions(array('archive' => 'test.txt'));
+        $this->assertEquals('test.txt', $filter->getOptions('archive'));
+    }
+
+    /**
+     * Setting Options through constructor
+     *
+     * @return void
+     */
+    public function testGzGetSetOptionsInConstructor()
+    {
+        $filter2= new Zend_Filter_Compress_Gz(array('level' => 8));
+        $this->assertEquals(array('mode' => 'compress', 'level' => 8, 'archive' => null), $filter2->getOptions());
+    }
+
+    /**
+     * Setting Level
+     *
+     * @return void
+     */
+    public function testGzGetSetLevel()
+    {
+        $filter = new Zend_Filter_Compress_Gz();
+        $this->assertEquals(9, $filter->getLevel());
+        $filter->setLevel(6);
+        $this->assertEquals(6, $filter->getOptions('level'));
+
+        try {
+            $filter->setLevel(15);
+            $this->fail('Exception expected');
+        } catch(Zend_Filter_Exception $e) {
+            $this->assertContains('must be between', $e->getMessage());
+        }
+    }
+
+    /**
+     * Setting Mode
+     *
+     * @return void
+     */
+    public function testGzGetSetMode()
+    {
+        $filter = new Zend_Filter_Compress_Gz();
+        $this->assertEquals('compress', $filter->getMode());
+        $filter->setMode('deflate');
+        $this->assertEquals('deflate', $filter->getOptions('mode'));
+
+        try {
+            $filter->setMode('unknown');
+            $this->fail('Exception expected');
+        } catch(Zend_Filter_Exception $e) {
+            $this->assertContains('mode not supported', $e->getMessage());
+        }
+    }
+
+    /**
+     * Setting Archive
+     *
+     * @return void
+     */
+    public function testGzGetSetArchive()
+    {
+        $filter = new Zend_Filter_Compress_Gz();
+        $this->assertEquals(null, $filter->getArchive());
+        $filter->setArchive('Testfile.txt');
+        $this->assertEquals('Testfile.txt', $filter->getArchive());
+        $this->assertEquals('Testfile.txt', $filter->getOptions('archive'));
+    }
+
+    /**
+     * Setting Archive
+     *
+     * @return void
+     */
+    public function testGzCompressToFile()
+    {
+        $filter   = new Zend_Filter_Compress_Gz();
+        $archive = dirname(__FILE__) . '/../_files/compressed.gz';
+        $filter->setArchive($archive);
+
+        $content = $filter->compress('compress me');
+        $this->assertTrue($content);
+
+        $filter2  = new Zend_Filter_Compress_Gz();
+        $content2 = $filter2->decompress($archive);
+        $this->assertEquals('compress me', $content2);
+
+        $filter3 = new Zend_Filter_Compress_Gz();
+        $filter3->setArchive($archive);
+        $content3 = $filter3->decompress(null);
+        $this->assertEquals('compress me', $content3);
+    }
+
+    /**
+     * Test deflate
+     *
+     * @return void
+     */
+    public function testGzDeflate()
+    {
+        $filter  = new Zend_Filter_Compress_Gz(array('mode' => 'deflate'));
+
+        $content = $filter->compress('compress me');
+        $this->assertNotEquals('compress me', $content);
+
+        $content = $filter->decompress($content);
+        $this->assertEquals('compress me', $content);
+    }
+
+    /**
+     * testing toString
+     *
+     * @return void
+     */
+    public function testGzToString()
+    {
+        $filter = new Zend_Filter_Compress_Gz();
+        $this->assertEquals('Gz', $filter->toString());
+    }
+}
+
+if (PHPUnit_MAIN_METHOD == 'Zend_Filter_Compress_GzTest::main') {
+    Zend_Filter_Compress_GzTest::main();
+}

+ 95 - 0
tests/Zend/Filter/Compress/LzfTest.php

@@ -0,0 +1,95 @@
+<?php
+/**
+ * Zend Framework
+ *
+ * LICENSE
+ *
+ * This source file is subject to the new BSD license that is bundled
+ * with this package in the file LICENSE.txt.
+ * It is also available through the world-wide-web at this URL:
+ * http://framework.zend.com/license/new-bsd
+ * If you did not receive a copy of the license and are unable to
+ * obtain it through the world-wide-web, please send an email
+ * to license@zend.com so we can send you a copy immediately.
+ *
+ * @category   Zend
+ * @package    Zend_Filter
+ * @subpackage UnitTests
+ * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ * @version    $Id: $
+ */
+
+if (!defined('PHPUnit_MAIN_METHOD')) {
+    define('PHPUnit_MAIN_METHOD', 'Zend_Filter_Compress_LzfTest::main');
+}
+
+/**
+ * Test helper
+ */
+require_once dirname(__FILE__) . '/../../../TestHelper.php';
+
+/**
+ * @see Zend_Filter_Compress_Lzf
+ */
+require_once 'Zend/Filter/Compress/Lzf.php';
+
+/**
+ * @category   Zend
+ * @package    Zend_Filter
+ * @subpackage UnitTests
+ * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ */
+class Zend_Filter_Compress_LzfTest extends PHPUnit_Framework_TestCase
+{
+    /**
+     * Runs this test suite
+     *
+     * @return void
+     */
+    public static function main()
+    {
+        $suite  = new PHPUnit_Framework_TestSuite('Zend_Filter_Compress_LzfTest');
+        $result = PHPUnit_TextUI_TestRunner::run($suite);
+    }
+
+    public function setUp()
+    {
+        if (!extension_loaded('lzf')) {
+            $this->markTestSkipped('This adapter needs the lzf extension');
+        }
+    }
+
+    /**
+     * Basic usage
+     *
+     * @return void
+     */
+    public function testBasicUsage()
+    {
+        $filter  = new Zend_Filter_Compress_Lzf();
+
+        $text       = 'compress me';
+        $compressed = $filter->compress($text);
+        $this->assertNotEquals($text, $compressed);
+
+        $decompressed = $filter->decompress($compressed);
+        $this->assertEquals($text, $decompressed);
+    }
+
+    /**
+     * testing toString
+     *
+     * @return void
+     */
+    public function testLzfToString()
+    {
+        $filter = new Zend_Filter_Compress_Lzf();
+        $this->assertEquals('Lzf', $filter->toString());
+    }
+}
+
+if (PHPUnit_MAIN_METHOD == 'Zend_Filter_Compress_LzfTest::main') {
+    Zend_Filter_Compress_LzfTest::main();
+}

+ 332 - 0
tests/Zend/Filter/Compress/RarTest.php

@@ -0,0 +1,332 @@
+<?php
+/**
+ * Zend Framework
+ *
+ * LICENSE
+ *
+ * This source file is subject to the new BSD license that is bundled
+ * with this package in the file LICENSE.txt.
+ * It is also available through the world-wide-web at this URL:
+ * http://framework.zend.com/license/new-bsd
+ * If you did not receive a copy of the license and are unable to
+ * obtain it through the world-wide-web, please send an email
+ * to license@zend.com so we can send you a copy immediately.
+ *
+ * @category   Zend
+ * @package    Zend_Filter
+ * @subpackage UnitTests
+ * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ * @version    $Id: $
+ */
+
+if (!defined('PHPUnit_MAIN_METHOD')) {
+    define('PHPUnit_MAIN_METHOD', 'Zend_Filter_Compress_RarTest::main');
+}
+
+/**
+ * Test helper
+ */
+require_once dirname(__FILE__) . '/../../../TestHelper.php';
+
+/**
+ * @see Zend_Filter_Compress_Rar
+ */
+require_once 'Zend/Filter/Compress/Rar.php';
+
+/**
+ * @category   Zend
+ * @package    Zend_Filter
+ * @subpackage UnitTests
+ * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ */
+class Zend_Filter_Compress_RarTest extends PHPUnit_Framework_TestCase
+{
+    /**
+     * Runs this test suite
+     *
+     * @return void
+     */
+    public static function main()
+    {
+        $suite  = new PHPUnit_Framework_TestSuite('Zend_Filter_Compress_RarTest');
+        $result = PHPUnit_TextUI_TestRunner::run($suite);
+    }
+
+    public function setUp()
+    {
+        if (!extension_loaded('rar')) {
+            $this->markTestSkipped('This adapter needs the rar extension');
+        }
+
+        $files = array(
+            dirname(__FILE__) . '/../_files/zipextracted.txt',
+            dirname(__FILE__) . '/../_files/_compress/Compress/First/Second/zipextracted.txt',
+            dirname(__FILE__) . '/../_files/_compress/Compress/First/Second',
+            dirname(__FILE__) . '/../_files/_compress/Compress/First/zipextracted.txt',
+            dirname(__FILE__) . '/../_files/_compress/Compress/First',
+            dirname(__FILE__) . '/../_files/_compress/Compress/zipextracted.txt',
+            dirname(__FILE__) . '/../_files/_compress/Compress',
+            dirname(__FILE__) . '/../_files/_compress/zipextracted.txt',
+            dirname(__FILE__) . '/../_files/_compress'
+        );
+
+        foreach($files as $file) {
+            if (file_exists($file)) {
+                if (is_dir($file)) {
+                    rmdir($file);
+                } else {
+                    unlink($file);
+                }
+            }
+        }
+    }
+
+    public function tearDown()
+    {
+        $files = array(
+            dirname(__FILE__) . '/../_files/zipextracted.txt',
+            dirname(__FILE__) . '/../_files/_compress/Compress/First/Second/zipextracted.txt',
+            dirname(__FILE__) . '/../_files/_compress/Compress/First/Second',
+            dirname(__FILE__) . '/../_files/_compress/Compress/First/zipextracted.txt',
+            dirname(__FILE__) . '/../_files/_compress/Compress/First',
+            dirname(__FILE__) . '/../_files/_compress/Compress/zipextracted.txt',
+            dirname(__FILE__) . '/../_files/_compress/Compress',
+            dirname(__FILE__) . '/../_files/_compress/zipextracted.txt',
+            dirname(__FILE__) . '/../_files/_compress'
+        );
+
+        foreach($files as $file) {
+            if (file_exists($file)) {
+                if (is_dir($file)) {
+                    rmdir($file);
+                } else {
+                    unlink($file);
+                }
+            }
+        }
+    }
+
+    /**
+     * Basic usage
+     *
+     * @return void
+     */
+    public function testBasicUsage()
+    {
+        $filter  = new Zend_Filter_Compress_Rar(
+            array(
+                'archive'  => dirname(__FILE__) . '/../_files/compressed.rar',
+                'target'   => dirname(__FILE__) . '/../_files/zipextracted.txt',
+                'callback' => array('Zend_Filter_Compress_RarTest', 'rarCompress')
+            )
+        );
+
+        $content = $filter->compress('compress me');
+        $this->assertEquals(dirname(__FILE__) . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '_files'
+                            . DIRECTORY_SEPARATOR . 'compressed.rar', $content);
+
+        $content = $filter->decompress($content);
+        $this->assertEquals(dirname(__FILE__) . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR, $content);
+        $content = file_get_contents(dirname(__FILE__) . '/../_files/zipextracted.txt');
+        $this->assertEquals('compress me', $content);
+    }
+
+    /**
+     * Setting Options
+     *
+     * @return void
+     */
+    public function testRarGetSetOptions()
+    {
+        $filter = new Zend_Filter_Compress_Rar();
+        $this->assertEquals(
+            array(
+                'archive'  => null,
+                'callback' => null,
+                'password' => null,
+                'target'   => '.',
+            ),
+            $filter->getOptions()
+        );
+
+        $this->assertEquals(null, $filter->getOptions('archive'));
+
+        $this->assertNull($filter->getOptions('nooption'));
+        $filter->setOptions(array('nooption' => 'foo'));
+        $this->assertNull($filter->getOptions('nooption'));
+
+        $filter->setOptions(array('archive' => 'temp.txt'));
+        $this->assertEquals('temp.txt', $filter->getOptions('archive'));
+    }
+
+    /**
+     * Setting Archive
+     *
+     * @return void
+     */
+    public function testRarGetSetArchive()
+    {
+        $filter = new Zend_Filter_Compress_Rar();
+        $this->assertEquals(null, $filter->getArchive());
+        $filter->setArchive('Testfile.txt');
+        $this->assertEquals('Testfile.txt', $filter->getArchive());
+        $this->assertEquals('Testfile.txt', $filter->getOptions('archive'));
+    }
+
+    /**
+     * Setting Password
+     *
+     * @return void
+     */
+    public function testRarGetSetPassword()
+    {
+        $filter = new Zend_Filter_Compress_Rar();
+        $this->assertEquals(null, $filter->getPassword());
+        $filter->setPassword('test');
+        $this->assertEquals('test', $filter->getPassword());
+        $this->assertEquals('test', $filter->getOptions('password'));
+        $filter->setOptions(array('password' => 'test2'));
+        $this->assertEquals('test2', $filter->getPassword());
+        $this->assertEquals('test2', $filter->getOptions('password'));
+    }
+
+    /**
+     * Setting Target
+     *
+     * @return void
+     */
+    public function testRarGetSetTarget()
+    {
+        $filter = new Zend_Filter_Compress_Rar();
+        $this->assertEquals('.', $filter->getTarget());
+        $filter->setTarget('Testfile.txt');
+        $this->assertEquals('Testfile.txt', $filter->getTarget());
+        $this->assertEquals('Testfile.txt', $filter->getOptions('target'));
+
+        try {
+            $filter->setTarget('/unknown/path/to/file.txt');
+            $this->fails('Exception expected');
+        } catch(Zend_Filter_Exception $e) {
+            $this->assertContains('does not exist', $e->getMessage());
+        }
+    }
+
+    /**
+     * Setting Callback
+     *
+     * @return void
+     */
+    public function testSettingCallback()
+    {
+        $filter = new Zend_Filter_Compress_Rar();
+
+        try {
+            $filter->compress('test.txt');
+            $this->fails('Exception expected');
+        } catch (Exception $e) {
+            $this->assertContains('No compression callback', $e->getMessage());
+        }
+
+        try {
+            $filter->setCallback('invalidCallback');
+            $this->fails('Exception expected');
+        } catch (Exception $e) {
+            $this->assertContains('Callback can not be accessed', $e->getMessage());
+        }
+
+        $callback = array('Zend_Filter_Compress_RarTest', 'rarCompress');
+        $filter->setCallback($callback);
+        $this->assertEquals($callback, $filter->getCallback());
+
+    }
+
+    /**
+     * Compress to Archive
+     *
+     * @return void
+     */
+    public function testRarCompressFile()
+    {
+        $filter  = new Zend_Filter_Compress_Rar(
+            array(
+                'archive'  => dirname(__FILE__) . '/../_files/compressed.rar',
+                'target'   => dirname(__FILE__) . '/../_files/zipextracted.txt',
+                'callback' => array('Zend_Filter_Compress_RarTest', 'rarCompress')
+            )
+        );
+        file_put_contents(dirname(__FILE__) . '/../_files/zipextracted.txt', 'compress me');
+
+        $content = $filter->compress(dirname(__FILE__) . '/../_files/zipextracted.txt');
+        $this->assertEquals(dirname(__FILE__) . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '_files'
+                            . DIRECTORY_SEPARATOR . 'compressed.rar', $content);
+
+        $content = $filter->decompress($content);
+        $this->assertEquals(dirname(__FILE__) . DIRECTORY_SEPARATOR . '..'
+                            . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR, $content);
+        $content = file_get_contents(dirname(__FILE__) . '/../_files/zipextracted.txt');
+        $this->assertEquals('compress me', $content);
+    }
+
+    /**
+     * Compress directory to Filename
+     *
+     * @return void
+     */
+    public function testRarCompressDirectory()
+    {
+        $filter  = new Zend_Filter_Compress_Rar(
+            array(
+                'archive'  => dirname(__FILE__) . '/../_files/compressed.rar',
+                'target'   => dirname(__FILE__) . '/../_files/_compress',
+                'callback' => array('Zend_Filter_Compress_RarTest', 'rarCompress')
+            )
+        );
+        $content = $filter->compress(dirname(__FILE__) . '/../_files/Compress');
+        $this->assertEquals(dirname(__FILE__) . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '_files'
+                            . DIRECTORY_SEPARATOR . 'compressed.rar', $content);
+
+        mkdir(dirname(__FILE__) . DIRECTORY_SEPARATOR . '..'
+            . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . '_compress');
+        $content = $filter->decompress($content);
+        $this->assertEquals(dirname(__FILE__) . DIRECTORY_SEPARATOR . '..'
+                            . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . '_compress'
+                            . DIRECTORY_SEPARATOR, $content);
+
+        $base = dirname(__FILE__) . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '_files'
+              . DIRECTORY_SEPARATOR . '_compress' . DIRECTORY_SEPARATOR . 'Compress' . DIRECTORY_SEPARATOR;
+        $this->assertTrue(file_exists($base));
+        $this->assertTrue(file_exists($base . 'zipextracted.txt'));
+        $this->assertTrue(file_exists($base . 'First' . DIRECTORY_SEPARATOR . 'zipextracted.txt'));
+        $this->assertTrue(file_exists($base . 'First' . DIRECTORY_SEPARATOR .
+                          'Second' . DIRECTORY_SEPARATOR . 'zipextracted.txt'));
+        $content = file_get_contents(dirname(__FILE__) . '/../_files/Compress/zipextracted.txt');
+        $this->assertEquals('compress me', $content);
+    }
+
+    /**
+     * testing toString
+     *
+     * @return void
+     */
+    public function testRarToString()
+    {
+        $filter = new Zend_Filter_Compress_Rar();
+        $this->assertEquals('Rar', $filter->toString());
+    }
+
+    /**
+     * Test callback for compression
+     *
+     * @return unknown
+     */
+    public static function rarCompress()
+    {
+        return true;
+    }
+}
+
+if (PHPUnit_MAIN_METHOD == 'Zend_Filter_Compress_RarTest::main') {
+    Zend_Filter_Compress_RarTest::main();
+}

+ 261 - 0
tests/Zend/Filter/Compress/TarTest.php

@@ -0,0 +1,261 @@
+<?php
+/**
+ * Zend Framework
+ *
+ * LICENSE
+ *
+ * This source file is subject to the new BSD license that is bundled
+ * with this package in the file LICENSE.txt.
+ * It is also available through the world-wide-web at this URL:
+ * http://framework.zend.com/license/new-bsd
+ * If you did not receive a copy of the license and are unable to
+ * obtain it through the world-wide-web, please send an email
+ * to license@zend.com so we can send you a copy immediately.
+ *
+ * @category   Zend
+ * @package    Zend_Filter
+ * @subpackage UnitTests
+ * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ * @version    $Id: $
+ */
+
+if (!defined('PHPUnit_MAIN_METHOD')) {
+    define('PHPUnit_MAIN_METHOD', 'Zend_Filter_Compress_TarTest::main');
+}
+
+/**
+ * Test helper
+ */
+require_once dirname(__FILE__) . '/../../../TestHelper.php';
+
+/**
+ * @see Zend_Filter_Compress_Tar
+ */
+require_once 'Zend/Filter/Compress/Tar.php';
+
+/**
+ * @category   Zend
+ * @package    Zend_Filter
+ * @subpackage UnitTests
+ * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ */
+class Zend_Filter_Compress_TarTest extends PHPUnit_Framework_TestCase
+{
+    /**
+     * Runs this test suite
+     *
+     * @return void
+     */
+    public static function main()
+    {
+        $suite  = new PHPUnit_Framework_TestSuite('Zend_Filter_Compress_TarTest');
+        $result = PHPUnit_TextUI_TestRunner::run($suite);
+    }
+
+    public function setUp()
+    {
+        if (!class_exists('Archive_Tar')) {
+            require_once 'Zend/Loader.php';
+            try {
+                Zend_Loader::loadClass('Archive_Tar');
+            } catch (Zend_Exception $e) {
+                $this->markTestSkipped('This filter needs PEARs Archive_Tar');
+            }
+        }
+
+        $files = array(
+            dirname(__FILE__) . '/../_files/zipextracted.txt',
+            dirname(__FILE__) . '/../_files/_compress/Compress/First/Second/zipextracted.txt',
+            dirname(__FILE__) . '/../_files/_compress/Compress/First/Second',
+            dirname(__FILE__) . '/../_files/_compress/Compress/First/zipextracted.txt',
+            dirname(__FILE__) . '/../_files/_compress/Compress/First',
+            dirname(__FILE__) . '/../_files/_compress/Compress/zipextracted.txt',
+            dirname(__FILE__) . '/../_files/_compress/Compress',
+            dirname(__FILE__) . '/../_files/_compress/zipextracted.txt',
+            dirname(__FILE__) . '/../_files/_compress',
+            dirname(__FILE__) . '/../_files/compressed.tar'
+        );
+
+        foreach($files as $file) {
+            if (file_exists($file)) {
+                if (is_dir($file)) {
+                    rmdir($file);
+                } else {
+                    unlink($file);
+                }
+            }
+        }
+    }
+
+    public function tearDown()
+    {
+        $files = array(
+            dirname(__FILE__) . '/../_files/zipextracted.txt',
+            dirname(__FILE__) . '/../_files/_compress/Compress/First/Second/zipextracted.txt',
+            dirname(__FILE__) . '/../_files/_compress/Compress/First/Second',
+            dirname(__FILE__) . '/../_files/_compress/Compress/First/zipextracted.txt',
+            dirname(__FILE__) . '/../_files/_compress/Compress/First',
+            dirname(__FILE__) . '/../_files/_compress/Compress/zipextracted.txt',
+            dirname(__FILE__) . '/../_files/_compress/Compress',
+            dirname(__FILE__) . '/../_files/_compress/zipextracted.txt',
+            dirname(__FILE__) . '/../_files/_compress',
+            dirname(__FILE__) . '/../_files/compressed.tar'
+        );
+
+        foreach($files as $file) {
+            if (file_exists($file)) {
+                if (is_dir($file)) {
+                    rmdir($file);
+                } else {
+                    unlink($file);
+                }
+            }
+        }
+    }
+
+    /**
+     * Basic usage
+     *
+     * @return void
+     */
+    public function testBasicUsage()
+    {
+        $filter  = new Zend_Filter_Compress_Tar(
+            array(
+                'archive'  => dirname(__FILE__) . '/../_files/compressed.tar',
+                'target'   => dirname(__FILE__) . '/../_files/zipextracted.txt'
+            )
+        );
+
+        $content = $filter->compress('compress me');
+        $this->assertEquals(dirname(__FILE__) . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '_files'
+                            . DIRECTORY_SEPARATOR . 'compressed.tar', $content);
+
+        $content = $filter->decompress($content);
+        $this->assertEquals(dirname(__FILE__) . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR, $content);
+        $content = file_get_contents(dirname(__FILE__) . '/../_files/zipextracted.txt');
+        $this->assertEquals('compress me', $content);
+    }
+
+    /**
+     * Setting Options
+     *
+     * @return void
+     */
+    public function testTarGetSetOptions()
+    {
+        $filter = new Zend_Filter_Compress_Tar();
+        $this->assertEquals(
+            array(
+                'archive' => null,
+                'target'  => '.',
+                'mode'    => null),
+            $filter->getOptions()
+        );
+
+        $this->assertEquals(null, $filter->getOptions('archive'));
+
+        $this->assertNull($filter->getOptions('nooption'));
+        $filter->setOptions(array('nooptions' => 'foo'));
+        $this->assertNull($filter->getOptions('nooption'));
+
+        $filter->setOptions(array('archive' => 'temp.txt'));
+        $this->assertEquals('temp.txt', $filter->getOptions('archive'));
+    }
+
+    /**
+     * Setting Archive
+     *
+     * @return void
+     */
+    public function testTarGetSetArchive()
+    {
+        $filter = new Zend_Filter_Compress_Tar();
+        $this->assertEquals(null, $filter->getArchive());
+        $filter->setArchive('Testfile.txt');
+        $this->assertEquals('Testfile.txt', $filter->getArchive());
+        $this->assertEquals('Testfile.txt', $filter->getOptions('archive'));
+    }
+
+    /**
+     * Setting Target
+     *
+     * @return void
+     */
+    public function testTarGetSetTarget()
+    {
+        $filter = new Zend_Filter_Compress_Tar();
+        $this->assertEquals('.', $filter->getTarget());
+        $filter->setTarget('Testfile.txt');
+        $this->assertEquals('Testfile.txt', $filter->getTarget());
+        $this->assertEquals('Testfile.txt', $filter->getOptions('target'));
+
+        try {
+            $filter->setTarget('/unknown/path/to/file.txt');
+            $this->fail('Exception expected');
+        } catch(Zend_Filter_Exception $e) {
+            $this->assertContains('does not exist', $e->getMessage());
+        }
+    }
+
+    /**
+     * Setting Archive
+     *
+     * @return void
+     */
+    public function testTarCompressToFile()
+    {
+        $filter  = new Zend_Filter_Compress_Tar(
+            array(
+                'archive'  => dirname(__FILE__) . '/../_files/compressed.tar',
+                'target'   => dirname(__FILE__) . '/../_files/zipextracted.txt'
+            )
+        );
+        file_put_contents(dirname(__FILE__) . '/../_files/zipextracted.txt', 'compress me');
+
+        $content = $filter->compress(dirname(__FILE__) . '/../_files/zipextracted.txt');
+        $this->assertEquals(dirname(__FILE__) . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '_files'
+                            . DIRECTORY_SEPARATOR . 'compressed.tar', $content);
+
+        $content = $filter->decompress($content);
+        $this->assertEquals(dirname(__FILE__) . DIRECTORY_SEPARATOR . '..'
+                            . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR, $content);
+        $content = file_get_contents(dirname(__FILE__) . '/../_files/zipextracted.txt');
+        $this->assertEquals('compress me', $content);
+    }
+
+    /**
+     * Compress directory to Filename
+     *
+     * @return void
+     */
+    public function testTarCompressDirectory()
+    {
+        $filter  = new Zend_Filter_Compress_Tar(
+            array(
+                'archive'  => dirname(__FILE__) . '/../_files/compressed.tar',
+                'target'   => dirname(__FILE__) . '/../_files/_compress'
+            )
+        );
+        $content = $filter->compress(dirname(__FILE__) . '/../_files/Compress');
+        $this->assertEquals(dirname(__FILE__) . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '_files'
+                            . DIRECTORY_SEPARATOR . 'compressed.tar', $content);
+    }
+
+    /**
+     * testing toString
+     *
+     * @return void
+     */
+    public function testTarToString()
+    {
+        $filter = new Zend_Filter_Compress_Tar();
+        $this->assertEquals('Tar', $filter->toString());
+    }
+}
+
+if (PHPUnit_MAIN_METHOD == 'Zend_Filter_Compress_TarTest::main') {
+    Zend_Filter_Compress_TarTest::main();
+}

+ 293 - 0
tests/Zend/Filter/Compress/ZipTest.php

@@ -0,0 +1,293 @@
+<?php
+/**
+ * Zend Framework
+ *
+ * LICENSE
+ *
+ * This source file is subject to the new BSD license that is bundled
+ * with this package in the file LICENSE.txt.
+ * It is also available through the world-wide-web at this URL:
+ * http://framework.zend.com/license/new-bsd
+ * If you did not receive a copy of the license and are unable to
+ * obtain it through the world-wide-web, please send an email
+ * to license@zend.com so we can send you a copy immediately.
+ *
+ * @category   Zend
+ * @package    Zend_Filter
+ * @subpackage UnitTests
+ * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ * @version    $Id: $
+ */
+
+if (!defined('PHPUnit_MAIN_METHOD')) {
+    define('PHPUnit_MAIN_METHOD', 'Zend_Filter_Compress_ZipTest::main');
+}
+
+/**
+ * Test helper
+ */
+require_once dirname(__FILE__) . '/../../../TestHelper.php';
+
+/**
+ * @see Zend_Filter_Compress_Zip
+ */
+require_once 'Zend/Filter/Compress/Zip.php';
+
+/**
+ * @category   Zend
+ * @package    Zend_Filter
+ * @subpackage UnitTests
+ * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ */
+class Zend_Filter_Compress_ZipTest extends PHPUnit_Framework_TestCase
+{
+    /**
+     * Runs this test suite
+     *
+     * @return void
+     */
+    public static function main()
+    {
+        $suite  = new PHPUnit_Framework_TestSuite('Zend_Filter_Compress_ZipTest');
+        $result = PHPUnit_TextUI_TestRunner::run($suite);
+    }
+
+    public function setUp()
+    {
+        if (!extension_loaded('zip')) {
+            $this->markTestSkipped('This adapter needs the zip extension');
+        }
+
+        $files = array(
+            dirname(__FILE__) . '/../_files/compressed.zip',
+            dirname(__FILE__) . '/../_files/zipextracted.txt',
+            dirname(__FILE__) . '/../_files/zip.tmp',
+            dirname(__FILE__) . '/../_files/_compress/Compress/First/Second/zipextracted.txt',
+            dirname(__FILE__) . '/../_files/_compress/Compress/First/Second',
+            dirname(__FILE__) . '/../_files/_compress/Compress/First/zipextracted.txt',
+            dirname(__FILE__) . '/../_files/_compress/Compress/First',
+            dirname(__FILE__) . '/../_files/_compress/Compress/zipextracted.txt',
+            dirname(__FILE__) . '/../_files/_compress/Compress',
+            dirname(__FILE__) . '/../_files/_compress/zipextracted.txt',
+            dirname(__FILE__) . '/../_files/_compress'
+        );
+
+        foreach($files as $file) {
+            if (file_exists($file)) {
+                if (is_dir($file)) {
+                    rmdir($file);
+                } else {
+                    unlink($file);
+                }
+            }
+        }
+    }
+
+    public function tearDown()
+    {
+        $files = array(
+            dirname(__FILE__) . '/../_files/compressed.zip',
+            dirname(__FILE__) . '/../_files/zipextracted.txt',
+            dirname(__FILE__) . '/../_files/zip.tmp',
+            dirname(__FILE__) . '/../_files/_compress/Compress/First/Second/zipextracted.txt',
+            dirname(__FILE__) . '/../_files/_compress/Compress/First/Second',
+            dirname(__FILE__) . '/../_files/_compress/Compress/First/zipextracted.txt',
+            dirname(__FILE__) . '/../_files/_compress/Compress/First',
+            dirname(__FILE__) . '/../_files/_compress/Compress/zipextracted.txt',
+            dirname(__FILE__) . '/../_files/_compress/Compress',
+            dirname(__FILE__) . '/../_files/_compress/zipextracted.txt',
+            dirname(__FILE__) . '/../_files/_compress'
+        );
+
+        foreach($files as $file) {
+            if (file_exists($file)) {
+                if (is_dir($file)) {
+                    rmdir($file);
+                } else {
+                    unlink($file);
+                }
+            }
+        }
+    }
+
+    /**
+     * Basic usage
+     *
+     * @return void
+     */
+    public function testBasicUsage()
+    {
+        $filter  = new Zend_Filter_Compress_Zip(
+            array(
+                'archive' => dirname(__FILE__) . '/../_files/compressed.zip',
+                'target'  => dirname(__FILE__) . '/../_files/zipextracted.txt'
+            )
+        );
+
+        $content = $filter->compress('compress me');
+        $this->assertEquals(dirname(__FILE__) . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '_files'
+                            . DIRECTORY_SEPARATOR . 'compressed.zip', $content);
+
+        $content = $filter->decompress($content);
+        $this->assertEquals(dirname(__FILE__) . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR, $content);
+        $content = file_get_contents(dirname(__FILE__) . '/../_files/zipextracted.txt');
+        $this->assertEquals('compress me', $content);
+    }
+
+    /**
+     * Setting Options
+     *
+     * @return void
+     */
+    public function testZipGetSetOptions()
+    {
+        $filter = new Zend_Filter_Compress_Zip();
+        $this->assertEquals(array('archive' => null, 'target' => '.'), $filter->getOptions());
+
+        $this->assertEquals(null, $filter->getOptions('archive'));
+
+        $this->assertNull($filter->getOptions('nooption'));
+        $filter->setOptions(array('nooption' => 'foo'));
+        $this->assertNull($filter->getOptions('nooption'));
+
+        $filter->setOptions(array('archive' => 'temp.txt'));
+        $this->assertEquals('temp.txt', $filter->getOptions('archive'));
+    }
+
+    /**
+     * Setting Archive
+     *
+     * @return void
+     */
+    public function testZipGetSetArchive()
+    {
+        $filter = new Zend_Filter_Compress_Zip();
+        $this->assertEquals(null, $filter->getArchive());
+        $filter->setArchive('Testfile.txt');
+        $this->assertEquals('Testfile.txt', $filter->getArchive());
+        $this->assertEquals('Testfile.txt', $filter->getOptions('archive'));
+    }
+
+    /**
+     * Setting Target
+     *
+     * @return void
+     */
+    public function testZipGetSetTarget()
+    {
+        $filter = new Zend_Filter_Compress_Zip();
+        $this->assertEquals('.', $filter->getTarget());
+        $filter->setTarget('Testfile.txt');
+        $this->assertEquals('Testfile.txt', $filter->getTarget());
+        $this->assertEquals('Testfile.txt', $filter->getOptions('target'));
+
+        try {
+            $filter->setTarget('/unknown/path/to/file.txt');
+            $this->fails('Exception expected');
+        } catch(Zend_Filter_Exception $e) {
+            $this->assertContains('does not exist', $e->getMessage());
+        }
+    }
+
+    /**
+     * Compress to Archive
+     *
+     * @return void
+     */
+    public function testZipCompressFile()
+    {
+        $filter  = new Zend_Filter_Compress_Zip(
+            array(
+                'archive' => dirname(__FILE__) . '/../_files/compressed.zip',
+                'target'  => dirname(__FILE__) . '/../_files/zipextracted.txt'
+            )
+        );
+        file_put_contents(dirname(__FILE__) . '/../_files/zipextracted.txt', 'compress me');
+
+        $content = $filter->compress(dirname(__FILE__) . '/../_files/zipextracted.txt');
+        $this->assertEquals(dirname(__FILE__) . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '_files'
+                            . DIRECTORY_SEPARATOR . 'compressed.zip', $content);
+
+        $content = $filter->decompress($content);
+        $this->assertEquals(dirname(__FILE__) . DIRECTORY_SEPARATOR . '..'
+                            . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR, $content);
+        $content = file_get_contents(dirname(__FILE__) . '/../_files/zipextracted.txt');
+        $this->assertEquals('compress me', $content);
+    }
+
+    /**
+     * Basic usage
+     *
+     * @return void
+     */
+    public function testCompressNonExistingTargetFile()
+    {
+        $filter  = new Zend_Filter_Compress_Zip(
+            array(
+                'archive' => dirname(__FILE__) . '/../_files/compressed.zip',
+                'target'  => dirname(__FILE__) . '/../_files'
+            )
+        );
+
+        $content = $filter->compress('compress me');
+        $this->assertEquals(dirname(__FILE__) . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '_files'
+                            . DIRECTORY_SEPARATOR . 'compressed.zip', $content);
+
+        $content = $filter->decompress($content);
+        $this->assertEquals(dirname(__FILE__) . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR, $content);
+        $content = file_get_contents(dirname(__FILE__) . '/../_files/zip.tmp');
+        $this->assertEquals('compress me', $content);
+    }
+
+    /**
+     * Compress directory to Archive
+     *
+     * @return void
+     */
+    public function testZipCompressDirectory()
+    {
+        $filter  = new Zend_Filter_Compress_Zip(
+            array(
+                'archive' => dirname(__FILE__) . '/../_files/compressed.zip',
+                'target'  => dirname(__FILE__) . '/../_files/_compress'
+            )
+        );
+        $content = $filter->compress(dirname(__FILE__) . '/../_files/Compress');
+        $this->assertEquals(dirname(__FILE__) . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '_files'
+                            . DIRECTORY_SEPARATOR . 'compressed.zip', $content);
+
+        mkdir(dirname(__FILE__) . DIRECTORY_SEPARATOR . '..'
+            . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . '_compress');
+        $content = $filter->decompress($content);
+        $this->assertEquals(dirname(__FILE__) . DIRECTORY_SEPARATOR . '..'
+                            . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . '_compress'
+                            . DIRECTORY_SEPARATOR, $content);
+
+        $base = dirname(__FILE__) . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '_files'
+              . DIRECTORY_SEPARATOR . '_compress' . DIRECTORY_SEPARATOR . 'Compress' . DIRECTORY_SEPARATOR;
+        $this->assertTrue(file_exists($base));
+        $this->assertTrue(file_exists($base . 'zipextracted.txt'));
+        $this->assertTrue(file_exists($base . 'First' . DIRECTORY_SEPARATOR . 'zipextracted.txt'));
+        $this->assertTrue(file_exists($base . 'First' . DIRECTORY_SEPARATOR .
+                          'Second' . DIRECTORY_SEPARATOR . 'zipextracted.txt'));
+        $content = file_get_contents(dirname(__FILE__) . '/../_files/Compress/zipextracted.txt');
+        $this->assertEquals('compress me', $content);
+    }
+
+    /**
+     * testing toString
+     *
+     * @return void
+     */
+    public function testZipToString()
+    {
+        $filter = new Zend_Filter_Compress_Zip();
+        $this->assertEquals('Zip', $filter->toString());
+    }
+}
+
+if (PHPUnit_MAIN_METHOD == 'Zend_Filter_Compress_ZipTest::main') {
+    Zend_Filter_Compress_ZipTest::main();
+}

+ 277 - 0
tests/Zend/Filter/CompressTest.php

@@ -0,0 +1,277 @@
+<?php
+/**
+ * Zend Framework
+ *
+ * LICENSE
+ *
+ * This source file is subject to the new BSD license that is bundled
+ * with this package in the file LICENSE.txt.
+ * It is also available through the world-wide-web at this URL:
+ * http://framework.zend.com/license/new-bsd
+ * If you did not receive a copy of the license and are unable to
+ * obtain it through the world-wide-web, please send an email
+ * to license@zend.com so we can send you a copy immediately.
+ *
+ * @category   Zend
+ * @package    Zend_Filter
+ * @subpackage UnitTests
+ * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ * @version    $Id: $
+ */
+
+if (!defined('PHPUnit_MAIN_METHOD')) {
+    define('PHPUnit_MAIN_METHOD', 'Zend_Filter_CompressTest::main');
+}
+
+/**
+ * Test helper
+ */
+require_once dirname(__FILE__) . '/../../TestHelper.php';
+
+/**
+ * @see Zend_Filter_Compress
+ */
+require_once 'Zend/Filter/Compress.php';
+
+/**
+ * @category   Zend
+ * @package    Zend_Filter
+ * @subpackage UnitTests
+ * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ */
+class Zend_Filter_CompressTest extends PHPUnit_Framework_TestCase
+{
+    /**
+     * Runs this test suite
+     *
+     * @return void
+     */
+    public static function main()
+    {
+        $suite  = new PHPUnit_Framework_TestSuite('Zend_Filter_CompressTest');
+        $result = PHPUnit_TextUI_TestRunner::run($suite);
+    }
+
+    public function setUp()
+    {
+        if (!extension_loaded('bz2')) {
+            $this->markTestSkipped('This filter is tested with the bz2 extension');
+        }
+    }
+
+    public function tearDown()
+    {
+        if (file_exists(dirname(__FILE__) . '/../_files/compressed.bz2')) {
+            unlink(dirname(__FILE__) . '/../_files/compressed.bz2');
+        }
+    }
+
+    /**
+     * Basic usage
+     *
+     * @return void
+     */
+    public function testBasicUsage()
+    {
+        $filter  = new Zend_Filter_Compress('bz2');
+
+        $text     = 'compress me';
+        $compressed = $filter->filter($text);
+        $this->assertNotEquals($text, $compressed);
+
+        $decompressed = $filter->decompress($compressed);
+        $this->assertEquals($text, $decompressed);
+    }
+
+    /**
+     * Setting Options
+     *
+     * @return void
+     */
+    public function testGetSetAdapterOptionsInConstructor()
+    {
+        $filter = new Zend_Filter_Compress(array(
+            'adapter' => 'bz2', 
+            'options' => array(
+                'blocksize' => 6,
+                'archive'   => 'test.txt',
+            )
+        ));
+
+        $this->assertEquals(
+            array('blocksize' => 6, 'archive' => 'test.txt'),
+            $filter->getAdapterOptions()
+        );
+
+        $adapter = $filter->getAdapter();
+        $this->assertEquals(6, $adapter->getBlocksize());
+        $this->assertEquals('test.txt', $adapter->getArchive());
+    }
+
+    /**
+     * Setting Options through constructor
+     *
+     * @return void
+     */
+    public function testGetSetAdapterOptions()
+    {
+        $filter = new Zend_Filter_Compress('bz2');
+        $filter->setAdapterOptions(array(
+            'blocksize' => 6,
+            'archive'   => 'test.txt',
+        ));
+        $this->assertEquals(
+            array('blocksize' => 6, 'archive'   => 'test.txt'),
+            $filter->getAdapterOptions()
+        );
+        $adapter = $filter->getAdapter();
+        $this->assertEquals(6, $adapter->getBlocksize());
+        $this->assertEquals('test.txt', $adapter->getArchive());
+    }
+
+    /**
+     * Setting Blocksize
+     *
+     * @return void
+     */
+    public function testGetSetBlocksize()
+    {
+        $filter = new Zend_Filter_Compress('bz2');
+        $this->assertEquals(4, $filter->getBlocksize());
+        $filter->setBlocksize(6);
+        $this->assertEquals(6, $filter->getOptions('blocksize'));
+
+        try {
+            $filter->setBlocksize(15);
+            $this->fail('Exception expected');
+        } catch(Zend_Filter_Exception $e) {
+            $this->assertContains('must be between', $e->getMessage());
+        }
+    }
+
+    /**
+     * Setting Archive
+     *
+     * @return void
+     */
+    public function testGetSetArchive()
+    {
+        $filter = new Zend_Filter_Compress('bz2');
+        $this->assertEquals(null, $filter->getArchive());
+        $filter->setArchive('Testfile.txt');
+        $this->assertEquals('Testfile.txt', $filter->getArchive());
+        $this->assertEquals('Testfile.txt', $filter->getOptions('archive'));
+    }
+
+    /**
+     * Setting Archive
+     *
+     * @return void
+     */
+    public function testCompressToFile()
+    {
+        $filter   = new Zend_Filter_Compress('bz2');
+        $archive = dirname(__FILE__) . '/../_files/compressed.bz2';
+        $filter->setArchive($archive);
+
+        $content = $filter->filter('compress me');
+        $this->assertTrue($content);
+
+        $filter2  = new Zend_Filter_Compress('bz2');
+        $content2 = $filter2->decompress($archive);
+        $this->assertEquals('compress me', $content2);
+
+        $filter3 = new Zend_Filter_Compress('bz2');
+        $filter3->setArchive($archive);
+        $content3 = $filter3->decompress(null);
+        $this->assertEquals('compress me', $content3);
+    }
+
+    /**
+     * testing toString
+     *
+     * @return void
+     */
+    public function testToString()
+    {
+        $filter = new Zend_Filter_Compress('bz2');
+        $this->assertEquals('Bz2', $filter->toString());
+    }
+
+    /**
+     * testing getAdapter
+     *
+     * @return void
+     */
+    public function testGetAdapter()
+    {
+        $filter = new Zend_Filter_Compress('bz2');
+        $adapter = $filter->getAdapter();
+        $this->assertTrue($adapter instanceof Zend_Filter_Compress_CompressInterface);
+        $this->assertEquals('Bz2', $filter->getAdapterName());
+    }
+
+    /**
+     * Setting Adapter
+     *
+     * @return void
+     */
+    public function testSetAdapter()
+    {
+        if (!extension_loaded('zlib')) {
+            $this->markTestSkipped('This filter is tested with the zlib extension');
+        }
+
+        $filter = new Zend_Filter_Compress();
+        $this->assertEquals('Gz', $filter->getAdapterName());
+
+        try {
+            $filter->setAdapter('Zend_Filter');
+            $adapter = $filter->getAdapter();
+            $this->fail('Invalid adapter should fail when retrieved');
+        } catch (Zend_Filter_Exception $e) {
+            $this->assertContains('does not implement', $e->getMessage());
+        }
+    }
+
+    /**
+     * Decompress archiv
+     *
+     * @return void
+     */
+    public function testDecompressArchive()
+    {
+        $filter   = new Zend_Filter_Compress('bz2');
+        $archive = dirname(__FILE__) . '/../_files/compressed.bz2';
+        $filter->setArchive($archive);
+
+        $content = $filter->filter('compress me');
+        $this->assertTrue($content);
+
+        $filter2  = new Zend_Filter_Compress('bz2');
+        $content2 = $filter2->decompress($archive);
+        $this->assertEquals('compress me', $content2);
+    }
+
+    /**
+     * Setting invalid method
+     *
+     * @return void
+     */
+    public function testInvalidMethod()
+    {
+        $filter = new Zend_Filter_Compress();
+        try {
+            $filter->invalidMethod();
+            $this->fail('Exception expected');
+        } catch (Zend_Exception $e) {
+            $this->assertContains('Unknown method', $e->getMessage());
+        }
+    }
+}
+
+if (PHPUnit_MAIN_METHOD == 'Zend_Filter_CompressTest::main') {
+    Zend_Filter_CompressTest::main();
+}

+ 134 - 0
tests/Zend/Filter/DecompressTest.php

@@ -0,0 +1,134 @@
+<?php
+/**
+ * Zend Framework
+ *
+ * LICENSE
+ *
+ * This source file is subject to the new BSD license that is bundled
+ * with this package in the file LICENSE.txt.
+ * It is also available through the world-wide-web at this URL:
+ * http://framework.zend.com/license/new-bsd
+ * If you did not receive a copy of the license and are unable to
+ * obtain it through the world-wide-web, please send an email
+ * to license@zend.com so we can send you a copy immediately.
+ *
+ * @category   Zend
+ * @package    Zend_Filter
+ * @subpackage UnitTests
+ * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ * @version    $Id: $
+ */
+
+if (!defined('PHPUnit_MAIN_METHOD')) {
+    define('PHPUnit_MAIN_METHOD', 'Zend_Filter_DecompressTest::main');
+}
+
+/**
+ * Test helper
+ */
+require_once dirname(__FILE__) . '/../../TestHelper.php';
+
+/**
+ * @see Zend_Filter_Decompress
+ */
+require_once 'Zend/Filter/Decompress.php';
+
+/**
+ * @category   Zend
+ * @package    Zend_Filter
+ * @subpackage UnitTests
+ * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ */
+class Zend_Filter_DecompressTest extends PHPUnit_Framework_TestCase
+{
+    /**
+     * Runs this test suite
+     *
+     * @return void
+     */
+    public static function main()
+    {
+        $suite  = new PHPUnit_Framework_TestSuite('Zend_Filter_DecompressTest');
+        $result = PHPUnit_TextUI_TestRunner::run($suite);
+    }
+
+    public function setUp()
+    {
+        if (!extension_loaded('bz2')) {
+            $this->markTestSkipped('This filter is tested with the bz2 extension');
+        }
+    }
+
+    public function tearDown()
+    {
+        if (file_exists(dirname(__FILE__) . '/../_files/compressed.bz2')) {
+            unlink(dirname(__FILE__) . '/../_files/compressed.bz2');
+        }
+    }
+
+    /**
+     * Basic usage
+     *
+     * @return void
+     */
+    public function testBasicUsage()
+    {
+        $filter  = new Zend_Filter_Decompress('bz2');
+
+        $text       = 'compress me';
+        $compressed = $filter->compress($text);
+        $this->assertNotEquals($text, $compressed);
+
+        $decompressed = $filter->filter($compressed);
+        $this->assertEquals($text, $decompressed);
+    }
+
+    /**
+     * Setting Archive
+     *
+     * @return void
+     */
+    public function testCompressToFile()
+    {
+        $filter   = new Zend_Filter_Decompress('bz2');
+        $archive = dirname(__FILE__) . '/../_files/compressed.bz2';
+        $filter->setArchive($archive);
+
+        $content = $filter->compress('compress me');
+        $this->assertTrue($content);
+
+        $filter2  = new Zend_Filter_Decompress('bz2');
+        $content2 = $filter2->filter($archive);
+        $this->assertEquals('compress me', $content2);
+
+        $filter3 = new Zend_Filter_Decompress('bz2');
+        $filter3->setArchive($archive);
+        $content3 = $filter3->filter(null);
+        $this->assertEquals('compress me', $content3);
+    }
+
+    /**
+     * Basic usage
+     *
+     * @return void
+     */
+    public function testDecompressArchive()
+    {
+        $filter   = new Zend_Filter_Decompress('bz2');
+        $archive = dirname(__FILE__) . '/../_files/compressed.bz2';
+        $filter->setArchive($archive);
+
+        $content = $filter->compress('compress me');
+        $this->assertTrue($content);
+
+        $filter2  = new Zend_Filter_Decompress('bz2');
+        $content2 = $filter2->filter($archive);
+        $this->assertEquals('compress me', $content2);
+    }
+}
+
+if (PHPUnit_MAIN_METHOD == 'Zend_Filter_DecompressTest::main') {
+    Zend_Filter_DecompressTest::main();
+}

+ 1 - 0
tests/Zend/Filter/_files/Compress/First/Second/zipextracted.txt

@@ -0,0 +1 @@
+compress me

+ 1 - 0
tests/Zend/Filter/_files/Compress/First/zipextracted.txt

@@ -0,0 +1 @@
+compress me

+ 1 - 0
tests/Zend/Filter/_files/Compress/zipextracted.txt

@@ -0,0 +1 @@
+compress me

BIN
tests/Zend/Filter/_files/compressed.rar