]> granicus.if.org Git - php/commitdiff
- MFB: #40228, ZipArchive::extractTo does create empty directories
authorPierre Joye <pajoye@php.net>
Mon, 29 Jan 2007 16:01:55 +0000 (16:01 +0000)
committerPierre Joye <pajoye@php.net>
Mon, 29 Jan 2007 16:01:55 +0000 (16:01 +0000)
 recursively

ext/zip/php_zip.c
ext/zip/tests/bug40228.phpt [new file with mode: 0644]
ext/zip/tests/bug40228.zip [new file with mode: 0644]

index e49b6118744133a777b8789ce5aed87c3311ed42..78f268a7fa183effd147fd5ace2a6a27590b7639 100644 (file)
@@ -101,23 +101,28 @@ static int php_zip_extract_file(struct zip * za, char *dest, char *file, int fil
 
        char *file_basename;
        size_t file_basename_len;
+       int is_dir_only = 0;
 
        if (file_len >= MAXPATHLEN || zip_stat(za, file, 0, &sb)) {
                return 0;
        }
 
-       memcpy(file_dirname, file, file_len);
-
-       dir_len = php_dirname(file_dirname, file_len);
-
-       if (dir_len > 0) {
-               len = spprintf(&file_dirname_fullpath, 0, "%s/%s", dest, file_dirname);
+       if (file_len > 1 && file[file_len - 1] == '/') {
+               len = spprintf(&file_dirname_fullpath, 0, "%s/%s", dest, file);
+               is_dir_only = 1;
        } else {
-               len = spprintf(&file_dirname_fullpath, 0, "%s", dest);
-       }
+               memcpy(file_dirname, file, file_len);
 
-       php_basename(file, file_len, NULL, 0, &file_basename, &file_basename_len TSRMLS_CC);
+               dir_len = php_dirname(file_dirname, file_len);
+
+               if (dir_len > 0) {
+                       len = spprintf(&file_dirname_fullpath, 0, "%s/%s", dest, file_dirname);
+               } else {
+                       len = spprintf(&file_dirname_fullpath, 0, "%s", dest);
+               }
 
+               php_basename(file, file_len, NULL, 0, &file_basename, (unsigned int *)&file_basename_len TSRMLS_CC);
+       }
        /* let see if the path already exists */
        if (php_stream_stat_path(file_dirname_fullpath, &ssb) < 0) {
                ret = php_stream_mkdir(file_dirname_fullpath, 0777,  PHP_STREAM_MKDIR_RECURSIVE, NULL);
@@ -131,7 +136,9 @@ static int php_zip_extract_file(struct zip * za, char *dest, char *file, int fil
        /* it is a standalone directory, job done */
        if (file[file_len - 1] == '/') {
                efree(file_dirname_fullpath);
-               efree(file_basename);
+               if (!is_dir_only) {
+                       efree(file_basename);
+               }
                return 1;
        }
 
diff --git a/ext/zip/tests/bug40228.phpt b/ext/zip/tests/bug40228.phpt
new file mode 100644 (file)
index 0000000..fec2963
--- /dev/null
@@ -0,0 +1,23 @@
+--TEST--
+Bug #40228 (extractTo does not create recursive empty path)
+--SKIPIF--
+<?php if (!extension_loaded("zip")) print "skip"; ?>
+--FILE--
+<?php
+$dest = dirname(__FILE__);
+$arc_name = $dest . "/bug40228.zip";
+$zip = new ZipArchive;
+$zip->open($arc_name, ZIPARCHIVE::CREATE);;
+$zip->extractTo($dest);
+if (is_dir($dest . '/test/empty')) {
+       echo "Ok\n";
+       rmdir($dest . '/test/empty');
+       rmdir($dest . '/test');
+} else {
+       echo "Failed.\n";
+}
+echo "Done\n";
+?>
+--EXPECT--
+Ok
+Done
diff --git a/ext/zip/tests/bug40228.zip b/ext/zip/tests/bug40228.zip
new file mode 100644 (file)
index 0000000..bbcd951
Binary files /dev/null and b/ext/zip/tests/bug40228.zip differ