home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c019 / 3.ddi / DXZIP / DXZIP.C next >
Encoding:
C/C++ Source or Header  |  1989-09-30  |  6.2 KB  |  215 lines

  1. /* --------------------------------------------------------------------
  2.    DXZIP - This routine does for PKZIP and ZIP files what DXARC did for
  3.            ARC files: deletes files on the main directory already in
  4.            the ZIP file, either by name only or if size, date, and time
  5.            matches.
  6.  
  7.    Compiler: Turbo C 1.5. Will compile under Turbo C 2.0 or under 
  8.              MicroSoft C 5.0 or later or QuickC 2.0 or later.
  9.  
  10.    Contributions are welcome...send to:
  11.    Edward V. Dong
  12.    12205 Alexandria Place
  13.    Chino, CA 91710
  14.    Comments may be directed to E.DONG on Diamond Bar BBS at 714-861-1549
  15.    (1200/2400/9600), "E.DONG" on GENIE, or 71641,2371 on CompuServ.
  16.    PKZIP is copyrighted by Phil Katz.
  17.    This routine is copyright (c) 1989 by Edward V. Dong, All Rights Reserved.
  18.    -------------------------------------------------------------------- */
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <mem.h>
  22. #include <dos.h>
  23. #include <dir.h>
  24.  
  25. typedef    long    signature_type;
  26.  
  27. signature_type    local_file_header_signature    = 0x04034b50;
  28. signature_type    central_file_header_signature    = 0x02014b50;
  29. signature_type    end_central_dir_signature    = 0x06054b50;
  30.  
  31. int    name = 0;        /* default must be complete match */
  32. int    prompt = 0;        /* default is no prompt */
  33. int    FilesKilled = 0;    /* initialize to zero files killed */
  34. int    NoOfFiles = 0;        /* initialize to zero files scanned */
  35.  
  36. typedef struct xyx
  37. {    int    version_needed_to_extract;
  38.     int    general_purpose_bit_flag;
  39.     int    compression_method;
  40.     int    last_mod_file_time;
  41.     int    last_mod_file_date;
  42.     long    crc32;
  43.     long    compressed_size;
  44.     long    uncompressed_size;
  45.     int    filename_length;
  46.     int    extra_field_length;
  47. } local_file_header;
  48.  
  49. typedef struct xyxx
  50. {    int    version_made_by;
  51.     int    version_needed_to_extract;
  52.     int    general_purpose_bit_flag;
  53.     int    compression_method;
  54.     int    last_mod_file_time;
  55.     int    last_mod_file_date;
  56.     long    crc32;
  57.     long    compressed_size;
  58.     long    uncompressed_size;
  59.     int    filename_length;
  60.     int    extra_field_length;
  61.     int    file_comment_length;
  62.     int    disk_number_start;
  63.     int    internal_file_attributes;
  64.     long    external_file_attributes;
  65.     long    relative_offset_local_header;
  66. } central_directory_file_header;
  67.  
  68. typedef struct xyx2
  69. {    int    number_this_disk;
  70.     int    number_disk_with_start_central_directory;
  71.     int    total_entries_central_dir_on_this_disk;
  72.     int    total_entries_central_dir;
  73.     long    size_central_directory;
  74.     long    offset_start_central_directory;
  75.     int    zipfile_comment_length;
  76. } end_central_dir_record;
  77.  
  78. FILE*    zipfile;
  79.  
  80. int killfile(char *filename,int date,int time,long filelen)
  81. {                    /* kill main files tagged in ZIP */
  82. struct  ffblk   ffblk;
  83. int Kill;
  84.     Kill = 0;            /* don't kill file */
  85.     NoOfFiles++;            /* increment number of files scanned */
  86.         if (!findfirst(filename,&ffblk,FA_RDONLY|FA_HIDDEN|FA_SYSTEM|FA_ARCH))
  87.     {    if (    (date==ffblk.ff_fdate) &&
  88.             (time==ffblk.ff_ftime) &&
  89.             (filelen==ffblk.ff_fsize))
  90.                     Kill = 1;    /* match found */
  91.         if (prompt)
  92.         {    printf(" ?");
  93.             if (toupper(getch())!='Y') return;
  94.             Kill = 1;        /* kill file */
  95.         }
  96.         if (name) Kill = 1;        /* kill on name match */
  97.  
  98.         if (Kill)
  99.         {    if (!unlink(filename))
  100.             {    printf("\t: Killed\n");
  101.                 FilesKilled++;
  102.             }
  103.             else printf("\t: Can't Kill\n");
  104.         }
  105.     }
  106.     else printf(": Not Found\n");
  107. }
  108.  
  109. void process_local_file_header(void)
  110. {
  111. local_file_header    header;
  112. char    filename[240], extra[240];
  113. int    mon, day, year, hrs, min;
  114.  
  115. /* read local file header to get filename */
  116.     fread(&header,sizeof(local_file_header),1,zipfile);
  117.        setmem(filename,sizeof(filename),0);
  118.        fread(filename,header.filename_length,1,zipfile);    /* get filename */
  119.        fread(extra,header.extra_field_length,1,zipfile);
  120.        fseek(zipfile,header.compressed_size,1);    /* skip to next one */
  121.  
  122. /* list file name, date, time, size */
  123.     printf("%s",filename);
  124.     killfile(filename,header.last_mod_file_date,
  125.         header.last_mod_file_time,header.uncompressed_size);
  126. }
  127.  
  128. void process_central_file_header(void)
  129. {
  130. central_directory_file_header    header;
  131. char    filename[240], extra[240], comment[240];
  132.  
  133. /* open ZIP file & print optional comment if any */
  134.     fread(&header,sizeof(central_directory_file_header),1,zipfile);
  135.     fread(filename,header.filename_length,1,zipfile);
  136.     fread(extra,header.extra_field_length,1,zipfile);
  137.     setmem(comment,sizeof(comment),0);
  138.     fread(comment,header.file_comment_length,1,zipfile);
  139.     if (comment[0]) printf("%s\n",comment);
  140. }
  141.  
  142. void process_end_central_dir(void)
  143. {
  144. end_central_dir_record    record;
  145. int    count;
  146.  
  147.     fread(&record,sizeof(end_central_dir_record),1,zipfile);
  148.     count = record.zipfile_comment_length;
  149.     while (count)            /* bypass comments */
  150.     {    fgetc(zipfile);
  151.         count--;
  152.     }
  153. }
  154.  
  155. void process_headers(void)
  156. {
  157. long    sig;
  158.  
  159.     while (1)
  160.     {    if (!fread(&sig,sizeof(long),1,zipfile))
  161.         {    printf("\nZIP file is truncated: aborting...");
  162.             exit(1);
  163.         }
  164.         if (sig == local_file_header_signature)
  165.             process_local_file_header();
  166.         else if (sig == central_file_header_signature)
  167.             process_central_file_header();
  168.         else if (sig == end_central_dir_signature)
  169.         {    process_end_central_dir();
  170.             printf("SUMMARY: %d (of %d) files killed\n",
  171.                 FilesKilled,NoOfFiles);
  172.             return;
  173.         }
  174.         else
  175.         {    printf("\nZIP file has errors...aborting!\n");
  176.             exit(1);
  177.         }
  178.     }
  179. }
  180.  
  181. main(int argc,char *argv[])
  182. {
  183. char    filename[240];
  184. int    count;
  185.     argc--;argv++;        /* skip zeroth argument */
  186.     printf("DXZIP 1.0 (c) 1989 by E.Dong (on GENIE) (CIS 71641,2371)\n");
  187.     if (argc)
  188.     {    strcpy(filename,argv[0]);
  189.         argc--;
  190.         if (argc)
  191.         {    count = strlen(strupr(argv[1]));
  192.             if (strncmp(argv[1],"NAME",count)==0)
  193.                 name = 1;
  194.             else if (strncmp(argv[1],"PROMPT",count)==0)
  195.                 prompt = 1;
  196.         }
  197.         if (strchr(filename,'.')==NULL)    /* no extension given */
  198.             strcat(filename,".ZIP");/* add default extension */
  199.         zipfile = fopen(filename,"rb");    /* open file if possible */
  200.         if (zipfile == NULL)        /* error on opening file */
  201.         {    printf("Unable to open file %s\n",filename);
  202.             exit(1);
  203.         }
  204.         else process_headers();
  205.         fclose(zipfile);
  206.     }
  207.     else
  208.     {    printf("Deletes file(s) in current directory found in ZIP file\n");
  209.         printf("Syntax: DXZIP zipfile [name|prompt]\n");
  210.         printf("        'zipfile' is required\n");
  211.         printf("        'name' deletes file on name match\n");
  212.         printf("        'prompt' asks if to delete file\n");
  213.     }
  214. }
  215.