]> git.lizzy.rs Git - zlib.git/blobdiff - contrib/minizip/miniunz.c
zlib 1.2.2
[zlib.git] / contrib / minizip / miniunz.c
index 938d4ef5a36dfa5de685fde7f7aaf092a10b1de8..a6b06a2082eefb01a1f4d0f557b6f8b783b709ea 100644 (file)
@@ -1,3 +1,11 @@
+/*
+   miniunz.c
+   Version 1.01b, May 30th, 2004
+
+   Copyright (C) 1998-2004 Gilles Vollant
+*/
+
+
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -27,7 +35,7 @@
   mini unzip, demo of unzip package
 
   usage :
-  Usage : miniunz [-exvlo] file.zip [file_to_extract]
+  Usage : miniunz [-exvlo] file.zip [file_to_extract] [-d extractdir]
 
   list the file in the zipfile, and print the content of FILE_ID.ZIP or README.TXT
     if it exists
@@ -140,13 +148,20 @@ int makedir (newdir)
 
 void do_banner()
 {
-    printf("MiniUnz 0.15, demo of zLib + Unz package written by Gilles Vollant\n");
+    printf("MiniUnz 1.01b, demo of zLib + Unz package written by Gilles Vollant\n");
     printf("more info at http://www.winimage.com/zLibDll/unzip.html\n\n");
 }
 
 void do_help()
 {
-    printf("Usage : miniunz [-exvlo] file.zip [file_to_extract]\n\n") ;
+    printf("Usage : miniunz [-e] [-x] [-v] [-l] [-o] [-p password] file.zip [file_to_extr.] [-d extractdir]\n\n" \
+           "  -e  Extract without pathname (junk paths)\n" \
+           "  -x  Extract with pathname\n" \
+           "  -v  list files\n" \
+           "  -l  list files\n" \
+           "  -d  directory to extract into\n" \
+           "  -o  overwrite files without prompting\n" \
+           "  -p  extract crypted file using password\n\n");
 }
 
 
@@ -168,6 +183,7 @@ int do_list(uf)
         unz_file_info file_info;
         uLong ratio=0;
         const char *string_method;
+        char charCrypt=' ';
         err = unzGetCurrentFileInfo(uf,&file_info,filename_inzip,sizeof(filename_inzip),NULL,0,NULL,0);
         if (err!=UNZ_OK)
         {
@@ -177,6 +193,10 @@ int do_list(uf)
         if (file_info.uncompressed_size>0)
             ratio = (file_info.compressed_size*100)/file_info.uncompressed_size;
 
+        /* display a '*' if the file is crypted */
+        if ((file_info.flag & 1) != 0)
+            charCrypt='*';
+
         if (file_info.compression_method==0)
             string_method="Stored";
         else
@@ -193,8 +213,10 @@ int do_list(uf)
         else
             string_method="Unkn. ";
 
-        printf("%7lu  %6s %7lu %3lu%%  %2.2lu-%2.2lu-%2.2lu  %2.2lu:%2.2lu  %8.8lx   %s\n",
-                file_info.uncompressed_size,string_method,file_info.compressed_size,
+        printf("%7lu  %6s%c%7lu %3lu%%  %2.2lu-%2.2lu-%2.2lu  %2.2lu:%2.2lu  %8.8lx   %s\n",
+                file_info.uncompressed_size,string_method,
+                charCrypt,
+                file_info.compressed_size,
                 ratio,
                 (uLong)file_info.tmu_date.tm_mon + 1,
                 (uLong)file_info.tmu_date.tm_mday,
@@ -291,8 +313,14 @@ int do_extract_currentfile(uf,popt_extract_without_path,popt_overwrite,password)
                 do
                 {
                     char answer[128];
-                    printf("The file %s exist. Overwrite ? [y]es, [n]o, [A]ll: ",write_filename);
-                    scanf("%1s",answer);
+                    int ret;
+
+                    printf("The file %s exists. Overwrite ? [y]es, [n]o, [A]ll: ",write_filename);
+                    ret = scanf("%1s",answer);
+                    if (ret != 1)
+                    {
+                       exit(EXIT_FAILURE);
+                    }
                     rep = answer[0] ;
                     if ((rep>='a') && (rep<='z'))
                         rep -= 0x20;
@@ -446,6 +474,8 @@ int main(argc,argv)
     int opt_do_extract=1;
     int opt_do_extract_withoutpath=0;
     int opt_overwrite=0;
+    int opt_extractdir=0;
+    const char *dirname=NULL;
     unzFile uf=NULL;
 
     do_banner();
@@ -475,6 +505,12 @@ int main(argc,argv)
                         opt_do_extract = opt_do_extract_withoutpath = 1;
                     if ((c=='o') || (c=='O'))
                         opt_overwrite=1;
+                    if ((c=='d') || (c=='D'))
+                    {
+                        opt_extractdir=1;
+                        dirname=argv[i+1];
+                    }
+
                     if (((c=='p') || (c=='P')) && (i+1<argc))
                     {
                         password=argv[i+1];
@@ -486,7 +522,7 @@ int main(argc,argv)
             {
                 if (zipfilename == NULL)
                     zipfilename = argv[i];
-                else if (filename_to_extract==NULL)
+                else if ((filename_to_extract==NULL) && (!opt_extractdir))
                         filename_to_extract = argv[i] ;
             }
         }
@@ -495,28 +531,28 @@ int main(argc,argv)
     if (zipfilename!=NULL)
     {
 
-        #ifdef USEWIN32IOAPI
+#        ifdef USEWIN32IOAPI
         zlib_filefunc_def ffunc;
-        #endif
+#        endif
 
         strncpy(filename_try, zipfilename,MAXFILENAME-1);
         /* strncpy doesnt append the trailing NULL, of the string is too long. */
         filename_try[ MAXFILENAME ] = '\0';
 
-        #ifdef USEWIN32IOAPI
+#        ifdef USEWIN32IOAPI
         fill_win32_filefunc(&ffunc);
         uf = unzOpen2(zipfilename,&ffunc);
-        #else
+#        else
         uf = unzOpen(zipfilename);
-        #endif
+#        endif
         if (uf==NULL)
         {
             strcat(filename_try,".zip");
-            #ifdef USEWIN32IOAPI
+#            ifdef USEWIN32IOAPI
             uf = unzOpen2(filename_try,&ffunc);
-            #else
+#            else
             uf = unzOpen(filename_try);
-            #endif
+#            endif
         }
     }
 
@@ -531,6 +567,12 @@ int main(argc,argv)
         return do_list(uf);
     else if (opt_do_extract==1)
     {
+        if (opt_extractdir && chdir(dirname))
+        {
+          printf("Error changing into %s, aborting\n", dirname);
+          exit(-1);
+        }
+
         if (filename_to_extract == NULL)
             return do_extract(uf,opt_do_extract_withoutpath,opt_overwrite,password);
         else