#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;
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
}
}
/* }}} */
+/* {{{ 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)
#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)
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}
};
/* }}} */
--- /dev/null
+--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