Skip to content

Commit

Permalink
add ZipArchive::setCompressionName and ZipArchive::setCompressionInde…
Browse files Browse the repository at this point in the history
…x methods
  • Loading branch information
remicollet committed May 6, 2015
1 parent 3638ac2 commit 3a55ea0
Show file tree
Hide file tree
Showing 4 changed files with 178 additions and 4 deletions.
21 changes: 21 additions & 0 deletions ext/zip/examples/set_compression.php
Original file line number Diff line number Diff line change
@@ -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();
87 changes: 84 additions & 3 deletions ext/zip/php_zip.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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}
};
/* }}} */
Expand Down
2 changes: 1 addition & 1 deletion ext/zip/php_zip.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
72 changes: 72 additions & 0 deletions ext/zip/tests/oo_setcompression.phpt
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 3a55ea0

Please # to comment.