From 3a55ea02aa0a91bbb90a6db8dda620720d141eb1 Mon Sep 17 00:00:00 2001 From: Remi Collet Date: Wed, 6 May 2015 15:11:00 +0200 Subject: [PATCH] add ZipArchive::setCompressionName and ZipArchive::setCompressionIndex methods --- ext/zip/examples/set_compression.php | 21 +++++++ ext/zip/php_zip.c | 87 +++++++++++++++++++++++++++- ext/zip/php_zip.h | 2 +- ext/zip/tests/oo_setcompression.phpt | 72 +++++++++++++++++++++++ 4 files changed, 178 insertions(+), 4 deletions(-) create mode 100644 ext/zip/examples/set_compression.php create mode 100644 ext/zip/tests/oo_setcompression.phpt diff --git a/ext/zip/examples/set_compression.php b/ext/zip/examples/set_compression.php new file mode 100644 index 0000000000..1efd88c7f1 --- /dev/null +++ b/ext/zip/examples/set_compression.php @@ -0,0 +1,21 @@ +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(); diff --git a/ext/zip/php_zip.c b/ext/zip/php_zip.c index f8b6a88fd7..a547709875 100644 --- a/ext/zip/php_zip.c +++ b/ext/zip/php_zip.c @@ -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} }; /* }}} */ diff --git a/ext/zip/php_zip.h b/ext/zip/php_zip.h index e68c9fcec9..289b41ade2 100644 --- a/ext/zip/php_zip.h +++ b/ext/zip/php_zip.h @@ -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 index 0000000000..8a746a83d7 --- /dev/null +++ b/ext/zip/tests/oo_setcompression.phpt @@ -0,0 +1,72 @@ +--TEST-- +setCompressionName and setCompressionIndex methods +--SKIPIF-- + +--FILE-- +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-- + +--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 -- 2.40.0