欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C语言基于窗体命令行打包,解包和浏览打包文件信息

程序员文章站 2022-11-06 21:05:55
#include#include#include#include#include#include#include using namespace std; int coun ......

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<windows.h>
#include<io.h>
#include<iostream>
#include<string>

using namespace std;
 int count=0;
 typedef struct note{
  char path[50];
  struct note *next;
 }note,*lnote;
 typedef struct node {
  char name[20];
  unsigned int size;
  struct node *next;   
 }node,*list;
 
 list creat(void);
 void add(list *head,char *tname,int tsize);
 void bwrite(char *fn);
 void file_in(char *path_r,char *path_w);
 void unzip(char *path_file,char *path_out);
 void lookthepack(char *path);
 lnote tcreat(void);
 void tadd(lnote *thead,char *path);
 void zip(char *ar1,char *ar2); 
 
 
 
 void lookthepack(char *path){
  int i,j;
  node p;
  file *fp;
   if((fp=fopen(path,"rb"))==null){
   printf("can't open %s\n",path);
    exit(1);
   }
   fseek(fp,0l,seek_set); 
   rewind(fp);
   fread(&j,sizeof(j),1,fp);
   printf("打包文件总数为:  %d\n",j);
   for(i=0;i<j;i++){
    fread(&p,sizeof(p),1,fp);
    printf("%s\n",p.name);
   }
 }
 
 
 
 void zip(char *ar1,char *ar2){
   char check;
   int l;
   list head=creat();
   lnote thead=tcreat();
     long handle;
   struct _finddata_t fileinfo;
    l=strlen(ar1);
   for(;l>0;l--){
    if(ar1[l-1]=='\\'){
     check=ar1[l];
     break;}
   }
   if(check!='*')
    strcat(ar1,"\\*.*");  
     
  if((handle=_findfirst(ar1,&fileinfo))==-1){
  /*  以下为路径不包括通配符情况*/
   printf("路径不存在!\n");
   exit(0);
 }
  
  else{
  do{
   if (!(strcmp(fileinfo.name, "..")&&strcmp(fileinfo.name,"."))){
    continue;
    }
  char path_a[40];
/* 以下旨在获取除去通配符后的文件路径 */
  int str_leng,t;
  strcpy(path_a,ar1);
  str_leng=strlen(path_a);
  for(t= str_leng+1;;t--)
   {if(path_a[t-1]=='\\'){
    path_a[t]='\0';
    break;}
   else
    path_a[t]='0';  
    
   }
   
  char tname[20];
  strcpy(tname,fileinfo.name);
   strcat(path_a,tname);  //获取绝对路径
    
   tadd(&thead,path_a);
   add(&head,fileinfo.name,fileinfo.size);     
    count++;  
   }while(_findnext(handle,&fileinfo)==0); 
  _findclose(handle);
  
  file *fp_stu,*fp_txt;
  if((fp_stu=fopen(ar2,"ab+"))==null){
   printf("can't open++++ %s\n",ar2);
   exit(1);
  }
  else{
   rewind(fp_stu);
   fwrite(&count,sizeof(count),1,fp_stu);  //记录文件总个数为int型,并放在打包文件头
   node *op;
   op=head->next;
   while(op!=null){
    fwrite(op,sizeof(*op),1,fp_stu); 
    op=op->next;
    }
    fclose(fp_stu); 
  } 
 
  
  note *pt;
  pt=thead->next;
  while(pt!=null){
   file_in(pt->path,ar2);     //文件读取及写入
   pt=pt->next;
  } 
 }
}  
 
 void unzip(char *path_file,char *path_out){
  file *fp1,*fp2,*fp3;
  char ch;
  int num,m=0;
  node p;
  
  if((fp1=fopen(path_file,"rb"))==null){
   printf("can't open %s\n",path_file);
    exit(1);
   }
  else if((fp2=fopen(path_file,"rb"))==null){
   printf("can't open %s\n",path_file);
    exit(1);
   } 
  else{
  
  fseek(fp1,0l,seek_set); 
  fseek(fp2,0l,seek_set);  
  rewind(fp1);
  rewind(fp1);
  fread(&num,sizeof(num),1,fp1);
  printf("the file-count is   :    %d\n",num);
  fseek(fp2,sizeof(int),seek_set);
  fseek(fp2,sizeof(p)*num,seek_cur);
  do { m++;
  fread(&p,sizeof(p),1,fp1);
  char fullpath_out[40];
  char name[20];
  int i=0;
  strcpy(name,p.name);
  strcpy(fullpath_out,path_out);
  strcat(fullpath_out,name);
  if((fp3=fopen(fullpath_out,"ab+"))==null){
   printf("can't open    %s   \n",fullpath_out);
    exit(1);
   }
  else{
   
   for(i=0;i<p.size;i+=(sizeof(char))){
   fread(&ch,sizeof(ch),1,fp2);
   fwrite(&ch,sizeof(ch),1,fp3);
   }
   }
  }while(m<num);
   fclose(fp1);
   fclose(fp2);
   fclose(fp3);
 
 }
  
}
  
 void file_in(char *path_r,char *path_w){
  file *fpr,*fpw;
  char ch;
  if((fpr=fopen(path_r,"rb"))==null){
   printf("can't open  文件路径  %s\n",path_r);
    exit(1);
   }
   
  if((fpw=fopen(path_w,"ab+"))==null){
   printf("can't open  打包指向路径 %s\n",path_w);
   exit(1);
   }
   fseek(fpw,0l,seek_end);    
   while ((ch=fgetc(fpr))!=eof)
    fputc(ch,fpw);
  
  
   fclose(fpr);
   fclose(fpw);  
 }
 
 lnote tcreat(void){
  lnote thead=(lnote)malloc(sizeof(note));
  thead->next=null;
  return thead;
 }
 void tadd(lnote *thead,char *path){
  note *s,*r;
  r=*thead;
  while(r->next!=null){
   r=r->next;  
  }
  s=(lnote)malloc(sizeof(note));
  strcpy(s->path,path);
  r->next=s;
  s->next=null;
 }
 
 list creat(void){
  list head=(list)malloc(sizeof(node));
  head->next=null;
  return head;
}
 void add(list *head,char *tname,int tsize){
  int i=0;
  node *s,*r;
  r=*head;
 
  while(r->next!=null){
   r=r->next;  
  } 
  s=(list)malloc(sizeof(node));
  strcpy(s->name,tname);
  s->size=tsize;
  r->next=s;
  s->next=null;
 }
 
 
 
int main(int argc,char *argv[]){
 
  
 if(argc>=1){
  if(strcmp(argv[1],"help")==0){
     printf("此程序命令行参数如下:  \n");
     printf("命令总数为 %d    \n",argc+1); 
  for(int i=0;i<argc;i++)
      printf("%s     %d\n",argv[i],i);
     }
 }
 /*  以下对应于argv[1]为‘-l’时,表示显示压缩包内的文件 */ 
  
   
    char a[20],b[5];
 for(int i=0;i<2;i++){
  b[i]=argv[1][i];
 }
  b[2]='\0';
 
    if(strcmp(argv[1],"-l")==0){
     lookthepack(argv[2]);  
     
 } 
 
 /*  以下对应于argv[1]为‘-u’时,表示解压 */ 
    else if(strcmp(b,"-u")==0){
     printf("sasda\n");
     unzip(argv[2],argv[3]);
    }
  
 else { 
   zip(argv[1],argv[2]);   
 }
  return 0;
  /*事例命令如下*/
//e:\打包事例\test.exe e:\打包事例\thepack\*.* e:\打包事例\
//e:\打包事例\test.exe -l e:\打包事例\testqq.txt
//e:\打包事例\test.exe -u e:\打包事例\testqq.txt e:\打包事例\zxcv\

 
}
 
 
还没有做递归和文件夹,也就是说仅仅对文件夹内文件适用。这个程序还差一点压缩,,之后有空再补上。