Skip to content

Commit 3a55ea0

Browse files
committed
add ZipArchive::setCompressionName and ZipArchive::setCompressionIndex methods
1 parent 3638ac2 commit 3a55ea0

File tree

4 files changed

+178
-4
lines changed

4 files changed

+178
-4
lines changed

ext/zip/examples/set_compression.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
error_reporting(E_ALL);
3+
if (!extension_loaded('zip')) {
4+
dl('zip.so');
5+
}
6+
7+
$zip = new ZipArchive();
8+
$filename = "a.zip";
9+
10+
if (!$zip->open($filename, ZIPARCHIVE::CREATE | ZipArchive::OVERWRITE)) {
11+
exit("cannot open <$filename>\n");
12+
}
13+
14+
$zip->addFromString("testfilephp.txt", "#1 This is a test string added as testfilephp.txt.\n");
15+
$zip->addFromString("testfilephp2.txt", "#2 This is a test string added as testfilephp2.txt.\n");
16+
$zip->addFile("too.php", "testfromfile.php");
17+
18+
$zip->setCompressionName("testfilephp2.txt", ZipArchive::CM_STORE);
19+
$zip->setCompressionIndex(2, ZipArchive::CM_STORE);
20+
21+
$zip->close();

ext/zip/php_zip.c

Lines changed: 84 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,10 @@ static int le_zip_entry;
8080
#define PHP_ZIP_SET_FILE_COMMENT(za, index, comment, comment_len) \
8181
if (comment_len == 0) { \
8282
/* Passing NULL remove the existing comment */ \
83-
if (zip_set_file_comment(intern, index, NULL, 0) < 0) { \
83+
if (zip_set_file_comment(za, index, NULL, 0) < 0) { \
8484
RETURN_FALSE; \
8585
} \
86-
} else if (zip_set_file_comment(intern, index, comment, comment_len) < 0) { \
86+
} else if (zip_set_file_comment(za, index, comment, comment_len) < 0) { \
8787
RETURN_FALSE; \
8888
} \
8989
RETURN_TRUE;
@@ -1543,7 +1543,7 @@ static ZIPARCHIVE_METHOD(getStatusString)
15431543
RETVAL_STRINGL(error_string, len);
15441544
#else
15451545
err = zip_get_error(intern);
1546-
RETVAL_STRING(zip_error_strerror(err), 1);
1546+
RETVAL_STRING(zip_error_strerror(err));
15471547
zip_error_fini(err);
15481548
#endif
15491549
}
@@ -2275,6 +2275,73 @@ static ZIPARCHIVE_METHOD(getCommentIndex)
22752275
}
22762276
/* }}} */
22772277

2278+
/* {{{ proto bool ZipArchive::setCompressionName(string name, int comp_method[, int comp_flags])
2279+
Set the compression of a file in zip, using its name */
2280+
static ZIPARCHIVE_METHOD(setCompressionName)
2281+
{
2282+
struct zip *intern;
2283+
zval *this = getThis();
2284+
size_t name_len;
2285+
char *name;
2286+
zip_int64_t idx;
2287+
zend_long comp_method, comp_flags = 0;
2288+
2289+
if (!this) {
2290+
RETURN_FALSE;
2291+
}
2292+
2293+
ZIP_FROM_OBJECT(intern, this);
2294+
2295+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sl|l",
2296+
&name, &name_len, &comp_method, &comp_flags) == FAILURE) {
2297+
return;
2298+
}
2299+
2300+
if (name_len < 1) {
2301+
php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Empty string as entry name");
2302+
}
2303+
2304+
idx = zip_name_locate(intern, name, 0);
2305+
if (idx < 0) {
2306+
RETURN_FALSE;
2307+
}
2308+
2309+
if (zip_set_file_compression(intern, (zip_uint64_t)idx,
2310+
(zip_int32_t)comp_method, (zip_uint32_t)comp_flags) != 0) {
2311+
RETURN_FALSE;
2312+
}
2313+
RETURN_TRUE;
2314+
}
2315+
/* }}} */
2316+
2317+
/* {{{ proto bool ZipArchive::setCompressionIndex(int index, int comp_method[, int comp_flags])
2318+
Set the compression of a file in zip, using its index */
2319+
static ZIPARCHIVE_METHOD(setCompressionIndex)
2320+
{
2321+
struct zip *intern;
2322+
zval *this = getThis();
2323+
zend_long index;
2324+
zend_long comp_method, comp_flags = 0;
2325+
2326+
if (!this) {
2327+
RETURN_FALSE;
2328+
}
2329+
2330+
ZIP_FROM_OBJECT(intern, this);
2331+
2332+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ll|l",
2333+
&index, &comp_method, &comp_flags) == FAILURE) {
2334+
return;
2335+
}
2336+
2337+
if (zip_set_file_compression(intern, (zip_uint64_t)index,
2338+
(zip_int32_t)comp_method, (zip_uint32_t)comp_flags) != 0) {
2339+
RETURN_FALSE;
2340+
}
2341+
RETURN_TRUE;
2342+
}
2343+
/* }}} */
2344+
22782345
/* {{{ proto bool ZipArchive::deleteIndex(int index)
22792346
Delete a file using its index */
22802347
static ZIPARCHIVE_METHOD(deleteIndex)
@@ -2870,6 +2937,18 @@ ZEND_END_ARG_INFO()
28702937
#endif /* ifdef ZIP_OPSYS_DEFAULT */
28712938
/* }}} */
28722939

2940+
ZEND_BEGIN_ARG_INFO_EX(arginfo_ziparchive_setcompname, 0, 0, 2)
2941+
ZEND_ARG_INFO(0, name)
2942+
ZEND_ARG_INFO(0, method)
2943+
ZEND_ARG_INFO(0, compflags)
2944+
ZEND_END_ARG_INFO()
2945+
2946+
ZEND_BEGIN_ARG_INFO_EX(arginfo_ziparchive_setcompindex, 0, 0, 2)
2947+
ZEND_ARG_INFO(0, index)
2948+
ZEND_ARG_INFO(0, method)
2949+
ZEND_ARG_INFO(0, compflags)
2950+
ZEND_END_ARG_INFO()
2951+
28732952
/* {{{ ze_zip_object_class_functions */
28742953
static const zend_function_entry zip_class_functions[] = {
28752954
ZIPARCHIVE_ME(open, arginfo_ziparchive_open, ZEND_ACC_PUBLIC)
@@ -2907,6 +2986,8 @@ static const zend_function_entry zip_class_functions[] = {
29072986
ZIPARCHIVE_ME(setExternalAttributesIndex, arginfo_ziparchive_setextattrindex, ZEND_ACC_PUBLIC)
29082987
ZIPARCHIVE_ME(getExternalAttributesName, arginfo_ziparchive_getextattrname, ZEND_ACC_PUBLIC)
29092988
ZIPARCHIVE_ME(getExternalAttributesIndex, arginfo_ziparchive_getextattrindex, ZEND_ACC_PUBLIC)
2989+
ZIPARCHIVE_ME(setCompressionName, arginfo_ziparchive_setcompname, ZEND_ACC_PUBLIC)
2990+
ZIPARCHIVE_ME(setCompressionIndex, arginfo_ziparchive_setcompindex, ZEND_ACC_PUBLIC)
29102991
{NULL, NULL, NULL}
29112992
};
29122993
/* }}} */

ext/zip/php_zip.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ extern zend_module_entry zip_module_entry;
3838
#define ZIP_OVERWRITE ZIP_TRUNCATE
3939
#endif
4040

41-
#define PHP_ZIP_VERSION "1.12.5"
41+
#define PHP_ZIP_VERSION "1.12.6dev"
4242

4343
#ifndef Z_SET_REFCOUNT_P
4444
# if PHP_MAJOR_VERSION < 6 && (PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION < 3)

ext/zip/tests/oo_setcompression.phpt

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
--TEST--
2+
setCompressionName and setCompressionIndex methods
3+
--SKIPIF--
4+
<?php
5+
/* $Id$ */
6+
if (!extension_loaded('zip')) die('skip');
7+
?>
8+
--FILE--
9+
<?php
10+
$tmpfile = dirname(__FILE__) . '/__tmp_oo_set_compression.zip';
11+
12+
if (file_exists($tmpfile)) {
13+
unlink($tmpfile);
14+
}
15+
16+
// generate the ZIP file
17+
$zip = new ZipArchive;
18+
if ($zip->open($tmpfile, ZipArchive::CREATE) !== TRUE) {
19+
exit('failed');
20+
}
21+
$txt = file_get_contents(__FILE__);
22+
$zip->addFromString('entry1.txt', $txt);
23+
$zip->addFromString('entry2.txt', $txt);
24+
$zip->addFromString('dir/entry3.txt', $txt);
25+
$zip->addFromString('entry4.txt', $txt);
26+
$zip->addFromString('entry5.txt', $txt);
27+
$zip->addFromString('entry6.txt', $txt);
28+
$zip->addFromString('entry7.txt', $txt);
29+
30+
var_dump($zip->setCompressionName('entry2.txt', ZipArchive::CM_DEFAULT));
31+
var_dump($zip->setCompressionName('dir/entry3.txt', ZipArchive::CM_STORE));
32+
var_dump($zip->setCompressionName('entry4.txt', ZipArchive::CM_DEFLATE));
33+
34+
var_dump($zip->setCompressionIndex(4, ZipArchive::CM_STORE));
35+
var_dump($zip->setCompressionIndex(5, ZipArchive::CM_DEFLATE));
36+
var_dump($zip->setCompressionIndex(6, ZipArchive::CM_DEFAULT));
37+
38+
if (!$zip->close()) {
39+
exit('failed');
40+
}
41+
42+
43+
// check the ZIP file
44+
$zip = zip_open($tmpfile);
45+
if (!is_resource($zip)) {
46+
exit('failed');
47+
}
48+
49+
while ($e = zip_read($zip)) {
50+
echo zip_entry_name($e) . ': ' . zip_entry_compressionmethod($e) . "\n";
51+
}
52+
zip_close($zip);
53+
?>
54+
--CLEAN--
55+
<?php
56+
$tmpfile = dirname(__FILE__) . '/__tmp_oo_set_compression.zip';
57+
unlink($tmpfile);
58+
?>
59+
--EXPECT--
60+
bool(true)
61+
bool(true)
62+
bool(true)
63+
bool(true)
64+
bool(true)
65+
bool(true)
66+
entry1.txt: deflated
67+
entry2.txt: deflated
68+
dir/entry3.txt: stored
69+
entry4.txt: deflated
70+
entry5.txt: stored
71+
entry6.txt: deflated
72+
entry7.txt: deflated

0 commit comments

Comments
 (0)