Create folder is there's a slash
authorRobert Pengelly <robertapengelly@hotmail.com>
Wed, 1 Oct 2025 18:44:28 +0000 (19:44 +0100)
committerRobert Pengelly <robertapengelly@hotmail.com>
Wed, 1 Oct 2025 18:44:28 +0000 (19:44 +0100)
unzip.c

diff --git a/unzip.c b/unzip.c
index 056bc88d0d2a2d692ddc0baa2305091b13a84000..b0a273646e63e2e88f2f71601c2235a279477ac8 100755 (executable)
--- a/unzip.c
+++ b/unzip.c
@@ -584,7 +584,7 @@ static void extract_zip (const char *path) {
     struct cfh cfh = { 0 };
     struct lfh lfh = { 0 };
     
-    char *temp;
+    char *temp, *p;
     int j;
     
     uint64_t src_used, dst_used;
@@ -691,20 +691,24 @@ static void extract_zip (const char *path) {
         
         }
         
-        if (cfh.ext_attrs & EXT_ATTR_DIR || cfh.name[cfh.name_len - 1] == '/') {
+        if (cfh.ext_attrs & EXT_ATTR_DIR || (p = strrchr (cfh.name, '/'))) {
         
+            if (p) { *p = '\0'; }
+            
             if (state->exdir) {
             
-                if (!(temp = malloc (strlen (state->exdir) + 1 + cfh.name_len + 1))) {
+                if (!(temp = malloc (strlen (state->exdir) + 1 + strlen (cfh.name) + 1))) {
                 
                     report_at (program_name, 0, REPORT_ERROR, "not enough free memory for name");
+                    
+                    if (p) { *p = '/'; }
                     break;
                 
                 }
                 
                 sprintf (temp, "%s%c%s", state->exdir, ch, cfh.name);
                 
-                if (!is_relative (temp + strlen (state->exdir) + 1, cfh.name_len)) {
+                if (!is_relative (temp + strlen (state->exdir) + 1, strlen (cfh.name))) {
                 
                     free (temp);
                     free (cfh.name);
@@ -719,22 +723,18 @@ static void extract_zip (const char *path) {
             
             } else {
             
-                if (!(temp = malloc (cfh.name_len + 1))) {
+                if (!(temp = malloc (strlen (cfh.name) + 1))) {
                 
                     report_at (program_name, 0, REPORT_ERROR, "not enough free memory for name");
-                    free (cfh.name);
-                    
-                    if (cfh.comment) { free (cfh.comment); }
-                    if (cfh.extra) { free (cfh.extra); }
                     
-                    memset (&cfh, 0, sizeof (cfh));
+                    if (p) { *p = '/'; }
                     break;
                 
                 }
                 
                 sprintf (temp, "%s", cfh.name);
                 
-                if (!is_relative (temp, cfh.name_len)) {
+                if (!is_relative (temp, strlen (cfh.name))) {
                 
                     free (temp);
                     free (cfh.name);
@@ -749,32 +749,21 @@ static void extract_zip (const char *path) {
             
             }
             
-            if (temp[strlen (temp) - 1] == '/') {
-                temp[strlen (temp) - 1] = '\0';
-            }
-            
 #if     defined (unix) || defined (__unix) || defined (__unix__) || defined (__APPLE__)
 
             if (!stat (temp, &sb)) {
             
-                if (S_ISDIR (sb.st_mode)) {
+                if (!S_ISDIR (sb.st_mode)) {
                 
-                    free (temp);
-                    free (cfh.name);
+                    report_at (program_name, 0, REPORT_ERROR, "%s exists but is not a directory", temp);
                     
-                    if (cfh.comment) { free (cfh.comment); }
-                    if (cfh.extra) { free (cfh.extra); }
-                    
-                    memset (&cfh, 0, sizeof (cfh));
-                    continue;
+                    free (temp);
+                    break;
                 
                 }
-                
-                report_at (program_name, 0, REPORT_ERROR, "%s exists but is not a directory", temp);
-                
-                free (temp);
-                break;
             
+            } else {
+                printf ("   creating: %s%c\n", temp, ch);
             }
 
 #elif   defined (_WIN32)
@@ -783,30 +772,21 @@ static void extract_zip (const char *path) {
             
             if (dwAttrib != INVALID_FILE_ATTRIBUTES) {
             
-                if (dwAttrib & FILE_ATTRIBUTE_DIRECTORY) {
+                if (!(dwAttrib & FILE_ATTRIBUTE_DIRECTORY)) {
                 
-                    free (temp);
-                    free (cfh.name);
-                    
-                    if (cfh.comment) { free (cfh.comment); }
-                    if (cfh.extra) { free (cfh.extra); }
+                    report_at (program_name, 0, REPORT_ERROR, "%s exists but is not a directory", temp);
                     
-                    memset (&cfh, 0, sizeof (cfh));
-                    continue;
+                    free (temp);
+                    break;
                 
+                } else {
+                    printf ("   creating: %s%c\n", temp, ch);
                 }
-                
-                report_at (program_name, 0, REPORT_ERROR, "%s exists but is not a directory", temp);
-                
-                free (temp);
-                break;
             
             }
 
 #endif
 
-            printf ("   creating: %s%c\n", temp, ch);
-            
             if (make_directory (temp)) {
             
                 report_at (program_name, 0, REPORT_ERROR, "failed to create %s", state->exdir);
@@ -816,8 +796,20 @@ static void extract_zip (const char *path) {
             
             }
             
+            if (p) { *p = '/'; }
             free (temp);
-            continue;
+            
+            if (cfh.ext_attrs & EXT_ATTR_DIR || cfh.name[cfh.name_len - 1] == '/') {
+            
+                free (cfh.name);
+                
+                if (cfh.comment) { free (cfh.comment); }
+                if (cfh.extra) { free (cfh.extra); }
+                
+                memset (&cfh, 0, sizeof (cfh));
+                continue;
+            
+            }
         
         }