⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unzip.c

📁 这是个解压缩软件UNZIP的C源程序代码
💻 C
📖 第 1 页 / 共 2 页
字号:

/*
 * This is a component of the ProDoor System.
 * Do not distribute modified versions without my permission.
 * Do not remove or alter this notice or any other copyright notice.
 * If you use this in your own program you must distribute source code.
 * Do not use any of this in a commercial product.
 *
 */

/*
 * UnZip - A simple zipfile extract utility
 *
 */ 

#define version  "UnZip:  Zipfile Extract v1.1?of 03-06-89;  (C) 1989 S.H.Smith"

typedef unsigned char byte;
typedef long longint;
typedef unsigned word;
typedef char boolean;
#define STRSIZ  256

#include <stdio.h>
#include <io.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/stat.h>



/* ----------------------------------------------------------- */ 
/*
 * Zipfile layout declarations
 *
 */ 

   typedef longint       signature_type;


   #define local_file_header_signature  0x04034b50L


   typedef struct local_file_header { 
      word         version_needed_to_extract; 
      word         general_purpose_bit_flag; 
      word         compression_method; 
      word         last_mod_file_time; 
      word         last_mod_file_date; 
      longint      crc32; 
      longint      compressed_size; 
      longint      uncompressed_size; 
      word         filename_length; 
      word         extra_field_length; 
   } local_file_header; 


   #define central_file_header_signature  0x02014b50L


   typedef struct central_directory_file_header { 
      word         version_made_by; 
      word         version_needed_to_extract; 
      word         general_purpose_bit_flag; 
      word         compression_method; 
      word         last_mod_file_time; 
      word         last_mod_file_date; 
      longint      crc32; 
      longint      compressed_size; 
      longint      uncompressed_size; 
      word         filename_length; 
      word         extra_field_length; 
      word         file_comment_length; 
      word         disk_number_start; 
      word         internal_file_attributes; 
      longint      external_file_attributes; 
      longint      relative_offset_local_header; 
   } central_directory_file_header; 


   #define end_central_dir_signature  0x06054b50L


   typedef struct end_central_dir_record { 
      word         number_this_disk; 
      word         number_disk_with_start_central_directory; 
      word         total_entries_central_dir_on_this_disk; 
      word         total_entries_central_dir; 
      longint      size_central_directory; 
      longint      offset_start_central_directory; 
      word         zipfile_comment_length; 
   } end_central_dir_record; 



/* ----------------------------------------------------------- */ 
/*
 * input file variables
 *
 */ 


   #define  uinbufsize    512L   /* input buffer size */
   byte     inbuf[uinbufsize];

   boolean  zipeof;
   longint  csize;
   longint  cusize;
   int      cmethod;
   int      inpos;
   int      incnt;
   int      pc;
   int      pcbits;
   int      pcbitv;
   
   int      zipfd;
   char     zipfn[STRSIZ];
   local_file_header lrec;




/* ----------------------------------------------------------- */ 
/*
 * output stream variables
 *
 */ 


   byte     outbuf[4096];   /* for rle look-back */
   longint  outpos;         /* absolute position in outfile */
   int      outcnt;

   int      outfd;
   char     filename[STRSIZ];
   char     extra[STRSIZ];



/* ----------------------------------------------------------- */ 
/*
 * shrink/reduce working storage
 *
 */ 


   int      factor;
   byte     followers[256][64];
   byte     Slen[256];
   int      ExState;
   int      C;
   int      V;
   int      Len;

   #define max_bits      13
   #define init_bits     9
   #define hsize         8192
   #define first_ent     257
   #define clear         256

   typedef int  hsize_array_integer[hsize+1];
   typedef byte hsize_array_byte[hsize+1];

   hsize_array_integer prefix_of;
   hsize_array_byte    suffix_of;
   hsize_array_byte    stack;

   int      cbits;
   int      maxcode;
   int      free_ent;
   int      maxcodemax;
   int      offset;
   int      sizex;


/* ------------------------------------------------------------- */ 

void         skip_csize(void)
{ 
   lseek(zipfd,csize,SEEK_CUR);
   zipeof = 1;
   csize = 0L; 
   incnt = 0; 
} 


/* ------------------------------------------------------------- */ 

void         ReadByte(int *       x)
{ 
   if (incnt == 0) 
   { 
      if (csize == 0L) 
      { 
         zipeof = 1;
         return;
      } 

      inpos = sizeof(inbuf);
      if (inpos > csize) 
         inpos = (int)csize;
      incnt = read(zipfd,inbuf,inpos);

      inpos = 1; 
      csize -= incnt; 
   } 

   *x = inbuf[inpos-1]; 
   inpos++; 
   incnt--; 
} 


/* ------------------------------------------------------------- */ 

void         ReadBits(int      bits,
                      int *    x)
     /* read the specified number of bits */ 
{ 
   int      bit;
   int      bitv;

   *x = 0;
   bitv = 1;

   for (bit = 0; bit <= bits-1; bit++)
   { 

      if (pcbits > 0) 
      { 
         pcbits--; 
         pcbitv = pcbitv << 1; 
      } 
      else 

      { 
         ReadByte(&pc); 
         pcbits = 7; 
         pcbitv = 1; 
      } 

      if ((pc & pcbitv) != 0) 
         *x = *x | bitv; 

      bitv = (int) (bitv << 1);
   } 

} 


/* ---------------------------------------------------------- */ 

void         get_string(int      len,
                        char *   s)
{ 
   read(zipfd,s,len);
   s[len] = 0;
} 


/* ------------------------------------------------------------- */ 

void         OutByte(int      c)
   /* output each character from archive to screen */ 
{ 
   outbuf[outcnt /* outpos % sizeof(outbuf) */] = c;
   outpos++; 
   outcnt++;
 
   if (outcnt == sizeof(outbuf)) 
   { 
      write(outfd,outbuf,outcnt);
      outcnt = 0; 
      printf("."); 
   } 
} 


/* ----------------------------------------------------------- */
   
int         reduce_L(int         x)
   { 
      switch (factor) {
         case 1:   return x & 0x7f; 
         case 2:   return x & 0x3f; 
         case 3:   return x & 0x1f; 
         case 4:   return x & 0x0f; 
      } 
    return 0; /* error */
   } 

   
int         reduce_F(int         x)
   { 
      switch (factor) {
         case 1:   if (x == 127) return 2;  else return 3;
         case 2:   if (x == 63) return 2;   else return 3;
         case 3:   if (x == 31) return 2;   else return 3;
         case 4:   if (x == 15) return 2;   else return 3;
      } 
    return 0; /* error */
   } 

   
int         reduce_D(int         x,
                     int         y)
   { 
      switch (factor) {
         case 1:   return ((x >> 7) & 0x01) * 256 + y + 1; 
         case 2:   return ((x >> 6) & 0x03) * 256 + y + 1; 
         case 3:   return ((x >> 5) & 0x07) * 256 + y + 1; 
         case 4:   return ((x >> 4) & 0x0f) * 256 + y + 1; 
      } 
    return 0; /* error */
   } 


int         reduce_B(int         x)
        /* number of bits needed to encode the specified number */ 
   { 
      switch (x - 1) {
         
         case 0:   
         case 1:   return 1; 
         
         case 2:   
         case 3:   return 2; 
         
         case 4:
         case 5:   
         case 6:   
         case 7:   return 3; 
         
         case 8:   
         case 9:   
         case 10:   
         case 11:   
         case 12:   
         case 13:   
         case 14:   
         case 15:   return 4; 
        
         case 16:
         case 17:
         case 18:
         case 19:
         case 20:
         case 21:
         case 22:
         case 23:
         case 24:
         case 25:
         case 26:
         case 27:
         case 28:
         case 29:
         case 30:
         case 31:   return 5;
        
         case 32:
         case 33:
         case 34:
         case 35:
         case 36:
         case 37:
         case 38:
         case 39:
         case 40:
         case 41:
         case 42:
         case 43:
         case 44:
         case 45:
         case 46:
         case 47:
         case 48:
         case 49:
         case 50:
         case 51:
         case 52:
         case 53:
         case 54:
         case 55:
         case 56:
         case 57:
         case 58:
         case 59:
         case 60:
         case 61:
         case 62:
         case 63:   return 6;

         case 64:
         case 65:
         case 66:
         case 67:
         case 68:
         case 69:
         case 70:
         case 71:
         case 72:
         case 73:
         case 74:
         case 75:
         case 76:
         case 77:
         case 78:
         case 79:
         case 80:
         case 81:
         case 82:
         case 83:
         case 84:
         case 85:
         case 86:
         case 87:
         case 88:
         case 89:
         case 90:
         case 91:
         case 92:
         case 93:
         case 94:
         case 95:
         case 96:
         case 97:
         case 98:
         case 99:
         case 100:
         case 101:
         case 102:
         case 103:
         case 104:
         case 105:
         case 106:
         case 107:
         case 108:
         case 109:
         case 110:
         case 111:
         case 112:
         case 113:
         case 114:
         case 115:
         case 116:
         case 117:
         case 118:
         case 119:
         case 120:
         case 121:
         case 122:
         case 123:
         case 124:
         case 125:
         case 126:
         case 127:   return 7;
      
      default:       return 8;
      } 
   } 



/* ----------------------------------------------------------- */

void         Expand(int      c)
   { 
      #define DLE           144
   
      switch (ExState) {
           
           case 0:
               if (c != DLE)
                   OutByte(c);
               else 
                   ExState = 1; 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -