]> granicus.if.org Git - php/commitdiff
add ZipArchive::setCompressionName and ZipArchive::setCompressionIndex methods
authorRemi Collet <remi@fedoraproject.org>
Wed, 6 May 2015 13:11:00 +0000 (15:11 +0200)
committerRemi Collet <remi@php.net>
Wed, 6 May 2015 13:16:43 +0000 (15:16 +0200)
ext/zip/examples/set_compression.php [new file with mode: 0644]
ext/zip/php_zip.c
ext/zip/php_zip.h
ext/zip/tests/oo_setcompression.phpt [new file with mode: 0644]

diff --git a/ext/zip/examples/set_compression.php b/ext/zip/examples/set_compression.php
new file mode 100644 (file)
index 0000000..1efd88c
--- /dev/null
@@ -0,0 +1,21 @@
+<?php
+error_reporting(E_ALL);
+if (!extension_loaded('zip')) {
+    dl('zip.so');
+}
+
+$zip = new ZipArchive();
+$filename = "a.zip";
+
+if (!$zip->open($filename, ZIPARCHIVE::CREATE | ZipArchive::OVERWRITE)) {
+       exit("cannot open <$filename>\n");
+}
+
+$zip->addFromString("testfilephp.txt", "#1 This is a test string added as testfilephp.txt.\n");
+$zip->addFromString("testfilephp2.txt", "#2 This is a test string added as testfilephp2.txt.\n");
+$zip->addFile("too.php", "testfromfile.php");
+
+$zip->setCompressionName("testfilephp2.txt", ZipArchive::CM_STORE);
+$zip->setCompressionIndex(2, ZipArchive::CM_STORE);
+
+$zip->close();
index f8b6a88fd71b26ed26e9657901db6a9df98b5b35..a54770987594b8e3bff14802f188900ac4200381 100644 (file)
@@ -80,10 +80,10 @@ static int le_zip_entry;
 #define PHP_ZIP_SET_FILE_COMMENT(za, index, comment, comment_len) \
        if (comment_len == 0) { \
                /* Passing NULL remove the existing comment */ \
-               if (zip_set_file_comment(intern, index, NULL, 0) < 0) { \
+               if (zip_set_file_comment(za, index, NULL, 0) < 0) { \
                        RETURN_FALSE; \
                } \
-       } else if (zip_set_file_comment(intern, index, comment, comment_len) < 0) { \
+       } else if (zip_set_file_comment(za, index, comment, comment_len) < 0) { \
                RETURN_FALSE; \
        } \
        RETURN_TRUE;
@@ -1543,7 +1543,7 @@ static ZIPARCHIVE_METHOD(getStatusString)
        RETVAL_STRINGL(error_string, len);
 #else
        err = zip_get_error(intern);
-       RETVAL_STRING(zip_error_strerror(err), 1);
+       RETVAL_STRING(zip_error_strerror(err));
        zip_error_fini(err);
 #endif
 }
@@ -2275,6 +2275,73 @@ static ZIPARCHIVE_METHOD(getCommentIndex)
 }
 /* }}} */
 
+/* {{{ proto bool ZipArchive::setCompressionName(string name, int comp_method[, int comp_flags])
+Set the compression of a file in zip, using its name */
+static ZIPARCHIVE_METHOD(setCompressionName)
+ {
+       struct zip *intern;
+       zval *this = getThis();
+       size_t name_len;
+       char *name;
+       zip_int64_t idx;
+       zend_long comp_method, comp_flags = 0;
+
+       if (!this) {
+               RETURN_FALSE;
+       }
+
+       ZIP_FROM_OBJECT(intern, this);
+
+       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sl|l",
+                       &name, &name_len, &comp_method, &comp_flags) == FAILURE) {
+               return;
+       }
+
+       if (name_len < 1) {
+               php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Empty string as entry name");
+       }
+
+       idx = zip_name_locate(intern, name, 0);
+       if (idx < 0) {
+               RETURN_FALSE;
+       }
+
+       if (zip_set_file_compression(intern, (zip_uint64_t)idx,
+                       (zip_int32_t)comp_method, (zip_uint32_t)comp_flags) != 0) {
+               RETURN_FALSE;
+       }
+       RETURN_TRUE;
+}
+/* }}} */
+
+/* {{{ proto bool ZipArchive::setCompressionIndex(int index, int comp_method[, int comp_flags])
+Set the compression of a file in zip, using its index */
+static ZIPARCHIVE_METHOD(setCompressionIndex)
+{
+       struct zip *intern;
+       zval *this = getThis();
+       zend_long index;
+       zend_long comp_method, comp_flags = 0;
+
+       if (!this) {
+               RETURN_FALSE;
+       }
+
+       ZIP_FROM_OBJECT(intern, this);
+
+       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ll|l",
+                       &index, &comp_method, &comp_flags) == FAILURE) {
+               return;
+       }
+
+       if (zip_set_file_compression(intern, (zip_uint64_t)index,
+                       (zip_int32_t)comp_method, (zip_uint32_t)comp_flags) != 0) {
+               RETURN_FALSE;
+       }
+       RETURN_TRUE;
+}
+/* }}} */
+
 /* {{{ proto bool ZipArchive::deleteIndex(int index)
 Delete a file using its index */
 static ZIPARCHIVE_METHOD(deleteIndex)
@@ -2870,6 +2937,18 @@ ZEND_END_ARG_INFO()
 #endif /* ifdef ZIP_OPSYS_DEFAULT */
 /* }}} */
 
+ZEND_BEGIN_ARG_INFO_EX(arginfo_ziparchive_setcompname, 0, 0, 2)
+       ZEND_ARG_INFO(0, name)
+       ZEND_ARG_INFO(0, method)
+       ZEND_ARG_INFO(0, compflags)
+ZEND_END_ARG_INFO()
+
+ZEND_BEGIN_ARG_INFO_EX(arginfo_ziparchive_setcompindex, 0, 0, 2)
+       ZEND_ARG_INFO(0, index)
+       ZEND_ARG_INFO(0, method)
+       ZEND_ARG_INFO(0, compflags)
+ZEND_END_ARG_INFO()
+
 /* {{{ ze_zip_object_class_functions */
 static const zend_function_entry zip_class_functions[] = {
        ZIPARCHIVE_ME(open,                                     arginfo_ziparchive_open, ZEND_ACC_PUBLIC)
@@ -2907,6 +2986,8 @@ static const zend_function_entry zip_class_functions[] = {
        ZIPARCHIVE_ME(setExternalAttributesIndex,       arginfo_ziparchive_setextattrindex, ZEND_ACC_PUBLIC)
        ZIPARCHIVE_ME(getExternalAttributesName,        arginfo_ziparchive_getextattrname, ZEND_ACC_PUBLIC)
        ZIPARCHIVE_ME(getExternalAttributesIndex,       arginfo_ziparchive_getextattrindex, ZEND_ACC_PUBLIC)
+       ZIPARCHIVE_ME(setCompressionName,               arginfo_ziparchive_setcompname, ZEND_ACC_PUBLIC)
+       ZIPARCHIVE_ME(setCompressionIndex,              arginfo_ziparchive_setcompindex, ZEND_ACC_PUBLIC)
        {NULL, NULL, NULL}
 };
 /* }}} */
index e68c9fcec90f692339fc95846cdd69b1ec553c34..289b41ade2a4a6dce27e3e7aba59d3da92a1fa30 100644 (file)
@@ -38,7 +38,7 @@ extern zend_module_entry zip_module_entry;
 #define ZIP_OVERWRITE ZIP_TRUNCATE
 #endif
 
-#define PHP_ZIP_VERSION "1.12.5"
+#define PHP_ZIP_VERSION "1.12.6dev"
 
 #ifndef  Z_SET_REFCOUNT_P
 # if PHP_MAJOR_VERSION < 6 && (PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION < 3)
diff --git a/ext/zip/tests/oo_setcompression.phpt b/ext/zip/tests/oo_setcompression.phpt
new file mode 100644 (file)
index 0000000..8a746a8
--- /dev/null
@@ -0,0 +1,72 @@
+--TEST--
+setCompressionName and setCompressionIndex methods
+--SKIPIF--
+<?php
+/* $Id$ */
+if (!extension_loaded('zip')) die('skip');
+?>
+--FILE--
+<?php
+$tmpfile = dirname(__FILE__) . '/__tmp_oo_set_compression.zip';
+
+if (file_exists($tmpfile)) {
+       unlink($tmpfile);
+}
+
+// generate the ZIP file
+$zip = new ZipArchive;
+if ($zip->open($tmpfile, ZipArchive::CREATE) !== TRUE) {
+       exit('failed');
+}
+$txt = file_get_contents(__FILE__);
+$zip->addFromString('entry1.txt', $txt);
+$zip->addFromString('entry2.txt', $txt);
+$zip->addFromString('dir/entry3.txt', $txt);
+$zip->addFromString('entry4.txt', $txt);
+$zip->addFromString('entry5.txt', $txt);
+$zip->addFromString('entry6.txt', $txt);
+$zip->addFromString('entry7.txt', $txt);
+
+var_dump($zip->setCompressionName('entry2.txt', ZipArchive::CM_DEFAULT));
+var_dump($zip->setCompressionName('dir/entry3.txt', ZipArchive::CM_STORE));
+var_dump($zip->setCompressionName('entry4.txt', ZipArchive::CM_DEFLATE));
+
+var_dump($zip->setCompressionIndex(4, ZipArchive::CM_STORE));
+var_dump($zip->setCompressionIndex(5, ZipArchive::CM_DEFLATE));
+var_dump($zip->setCompressionIndex(6, ZipArchive::CM_DEFAULT));
+
+if (!$zip->close()) {
+       exit('failed');
+}
+
+
+// check the ZIP file
+$zip = zip_open($tmpfile);
+if (!is_resource($zip)) {
+       exit('failed');
+}
+
+while ($e = zip_read($zip)) {
+       echo zip_entry_name($e) . ': ' . zip_entry_compressionmethod($e) . "\n";
+}
+zip_close($zip);
+?>
+--CLEAN--
+<?php
+$tmpfile = dirname(__FILE__) . '/__tmp_oo_set_compression.zip';
+unlink($tmpfile);
+?>
+--EXPECT--
+bool(true)
+bool(true)
+bool(true)
+bool(true)
+bool(true)
+bool(true)
+entry1.txt: deflated
+entry2.txt: deflated
+dir/entry3.txt: stored
+entry4.txt: deflated
+entry5.txt: stored
+entry6.txt: deflated
+entry7.txt: deflated