文件的分割和合并

来源:互联网 发布:淘宝店装修多少钱 编辑:程序博客网 时间:2024/04/28 15:01
segfile.c
  1. /*
  2.   Name: segfile(文件分割) 
  3.   Copyright: 天奇居 
  4.   Author: 加州旅客
  5.   Date: 30-04-08 23:15
  6.   Description:可以将较大的文件分割成数块,以便进行复制、传输。用法:segfile 单个文件大小 原文件路径 [分割文件保存路径]。 
  7. */
  8. #include<stdio.h>
  9. #include<sys/stat.h>
  10. #include<fcntl.h>
  11. #include<stdlib.h>
  12. #include<ctype.h>
  13. #include<string.h>
  14. #include<stdlib.h>
  15. #define READ_SIZE (1048576*2)
  16. unsigned long getfilesize(char *file);
  17. void copyfile(FILE *fpin,FILE *fpout,unsigned long fsize);
  18. int main(int argc,char **argv)
  19. {
  20. FILE *fp,*fpseg,*fplist,*fpcmd;
  21. int len,sharelen,i,j,n;
  22. char *s,savepath[512],*filename,filepath[512]="",lenstr[16],nowsegname[512],segfilename[512]="",seglist[512],mixcmd[512];
  23. unsigned long filesize,indivfile,lastfile,fileshare,nowsegsize;/*indivfile:单个文件大小;lastfile:最后一个文件的大小;fileshare:分割的文件份数*/
  24. if (argc<2)
  25.  {
  26.   printf("参数不够!/n用法:segfile 单个文件大小[K|M|G] 原文件路径 [分割文件保存路径]/n");
  27.   exit(-1);
  28.  }
  29. fp=fopen(argv[2],"r");
  30. if (fp==NULL)
  31. {
  32.  printf("找不到所要分割的文件,请检查你的输入是否有误或你是否有读取该文件的权限!/n");
  33.  exit(-1);
  34. }
  35. fclose(fp);
  36. filename=strrchr(argv[2],'//');
  37. if (filename==0)
  38.  {
  39.   filename=argv[2];
  40.  }
  41. else
  42.  {
  43.   filename++;
  44.   len=strlen(argv[2])-strlen(filename);
  45.   strncpy(filepath,argv[2],len);
  46.  }
  47. if (argc=3)
  48.  {
  49.   strcpy(savepath,filepath);
  50.  } 
  51. else
  52.  {
  53.   s=strcpy(savepath,argv[3]);
  54.   s=s+strlen(s)-1;
  55.   if(*s!='//')strcat(savepath,"//");
  56.  }
  57. filesize=getfilesize(argv[2]);
  58. indivfile=strtoul(argv[1],NULL,10);
  59. len=strlen(argv[1]);
  60. s=argv[1]+len-1;
  61. switch (*s)
  62.  {
  63.   case 'k':
  64.   case 'K':
  65.        indivfile*=1024;
  66.        break;
  67.   case 'm':
  68.   case 'M':
  69.        indivfile*=1048576;
  70.        break;
  71.   case 'g':
  72.   case 'G':
  73.        indivfile*=1073741824;
  74.        break;
  75.  }
  76. if (indivfile>filesize)
  77.  {
  78.   return -1;
  79.  }
  80. lastfile=filesize%indivfile;
  81. fileshare=filesize/indivfile;
  82. if (lastfile>0)
  83.  {
  84.   fileshare++;
  85.  }
  86. else
  87.  {
  88.   lastfile=indivfile;
  89.  }
  90. printf("待分割的文件:%s%s,大小是:%lu字节。/n分割文件保存路径:%s,分割文件个数是:%lu。/n现在开始分割…/n",filepath,filename,filesize,savepath,fileshare);
  91. sprintf(lenstr,"%lu",fileshare);
  92. sharelen=strlen(lenstr);
  93. fp=fopen(argv[2],"rb");
  94. strcpy(seglist,savepath);
  95. strcat(seglist,filename);
  96. strcat(seglist,".seglist");
  97. fplist=fopen(seglist,"w");
  98. strcpy(mixcmd,savepath);
  99. strcat(mixcmd,filename);
  100. strcat(mixcmd,".cmd");
  101. fpcmd=fopen(mixcmd,"w");
  102. fprintf(fpcmd,"copy /b ");
  103. for (i=1;i<=fileshare;i++)
  104.  {
  105.   sprintf(lenstr,"%d",i);
  106.   strcpy(segfilename,filename);
  107.   strcat(segfilename,".");
  108.   if (sharelen>strlen(lenstr))
  109.    {
  110.     for(j=1;j<=(sharelen-strlen(lenstr));j++)
  111.      {
  112.       strcat(segfilename,"0");
  113.      }                         
  114.    }
  115.   strcat(segfilename,lenstr);
  116.   strcat(segfilename,".seg");
  117.   fprintf(fplist,"%s/n",segfilename);
  118.   if (i<fileshare)
  119.    {
  120.     fprintf(fpcmd,"/"%s/"+",segfilename);
  121.    }
  122.   else
  123.    {
  124.     fprintf(fpcmd,"/"%s/"",segfilename);
  125.    }
  126.   strcpy(nowsegname,savepath);
  127.   strcat(nowsegname,segfilename);
  128.   if (i<fileshare)
  129.    {
  130.     nowsegsize=indivfile;
  131.    }
  132.   else
  133.    {
  134.     nowsegsize=lastfile;
  135.    } 
  136.   fpseg=fopen(nowsegname,"wb");
  137.   copyfile(fp,fpseg,nowsegsize);
  138.   fclose(fpseg);  
  139.  }
  140. fprintf(fpcmd," /"%s/"",filename);
  141. fclose(fplist);
  142. fclose(fp);
  143. printf("/n分割完成!!!/n分割文件列表为:%s,你可以用“mixfile.exe”读取此列表文件来合并分割的文件。/n生成的批处理文件为:%s,在windows下可以直接双击该文件来合并文件。/n注意:所有文件(包括列表文件和批处理文件)都必须放在同一文件夹下!否则程序会因为找不到文件而导致无法合并出完整源文件!/n",seglist,mixcmd);
  144. }
  145. unsigned long getfilesize(char *file)
  146. {
  147.   FILE *fp;
  148.   unsigned long curpos1,curpos2,length;
  149.   fp=fopen(file,"rb");
  150.   curpos1=ftell(fp);
  151.   fseek(fp,0L,SEEK_END);
  152.   curpos2=ftell(fp);
  153.   length=curpos2-curpos1;
  154.   fseek(fp,curpos1,SEEK_SET);
  155.   return length;
  156.   fclose(fp);     
  157. }
  158. void copyfile(FILE *fpin,FILE *fpout,unsigned long fsize)
  159. {
  160. void *buf;
  161. int i,num;
  162. unsigned long remain;
  163. num=fsize/READ_SIZE;
  164. remain=fsize%READ_SIZE;
  165. buf=malloc(READ_SIZE);
  166. for (i=0;i<num;i++)
  167.  {
  168.   fread(buf,READ_SIZE,1,fpin);
  169.   fwrite(buf,READ_SIZE,1,fpout);
  170.  }
  171. if(remain)
  172.  {
  173.   fread(buf,remain,1,fpin);
  174.   fwrite(buf,remain,1,fpout);
  175.  }
  176. free(buf);
  177. }
 
mixfile.c
  1. /*
  2.   Name: mixfile(文件分割) 
  3.   Copyright: 天奇居 
  4.   Author: 加州旅客 
  5.   Date: 30-04-08 23:15
  6.   Description:可将segfile分割的文件合并。用法:mixfile 列表文件 [输出文件名]。 
  7. */
  8. #include<stdio.h>
  9. #include<string.h>
  10. #define READ_SIZE (1048576*2)
  11. unsigned long getfilesize(char *file);
  12. void copyfile(FILE *fpin,FILE *fpout,unsigned long fsize);
  13. int main(int argc,char **argv)
  14. {
  15.  FILE *fpseg,*fpout,*fp;
  16.  int len,i; 
  17.  char *filename,filepath[512]="",*s,pristinefile[512],c,ones[1],segfile[256]="",segfilepath[512];
  18.  unsigned long segfilesize;
  19.  if (argc<2)
  20.   printf("用法:mixfile 列表文件 [输出文件名]/n");
  21.  fp=fopen(argv[1],"r");
  22.  if (fp==NULL)
  23.   {
  24.    printf("找不到分割文件列表,请检查你的输入是否有误或你是否有读取该文件的权限!/n");
  25.    exit(-1);
  26.   }
  27.  fclose(fp);
  28.  filename=strrchr(argv[1],'//');
  29.  if (filename==0)
  30.   {
  31.    filename=argv[1];
  32.   }
  33.  else
  34.   {
  35.    filename++;
  36.    len=strlen(argv[1])-strlen(filename);
  37.    strncpy(filepath,argv[1],len);
  38.   }
  39.  if (argc<3)
  40.   {
  41.    strcpy(pristinefile,filepath);
  42.    s=strrchr(filename,'.');
  43.    if (s==0)
  44.     {
  45.      strcat(pristinefile,filename);
  46.      strcat(pristinefile,".out");
  47.     }
  48.    else
  49.     {
  50.      len=strlen(filename)-strlen(s);
  51.      strncat(pristinefile,filename,len);
  52.     }
  53.   }
  54.  else
  55.   {
  56.    strcpy(pristinefile,argv[2]);
  57.   }
  58.  fpout=fopen(pristinefile,"wb");
  59.  printf("现在开始将分割文件合并到%s……/n/n",pristinefile);
  60.  fp=fopen(argv[1],"r");
  61.  while((c=fgetc(fp))!=EOF)
  62.   {
  63.    if (c=='/n')
  64.    {
  65.     strcpy(segfilepath,filepath);
  66.     strcat(segfilepath,segfile);
  67.     segfilesize=getfilesize(segfilepath);
  68.     fpseg=fopen(segfilepath,"rb");
  69.     if (fp==NULL)
  70.      {
  71.       printf("无法打开分割文件%s!/n",segfilepath);
  72.       exit(-1);
  73.      }
  74.     printf("正在读取%s,大小为%lu字节.../n",segfilepath,segfilesize);
  75.     copyfile(fpseg,fpout,segfilesize);
  76.     fclose(fpseg);
  77.     strcpy(segfile,"");
  78.    }
  79.    else
  80.    {
  81.     sprintf(ones,"%c",c);
  82.     strcat(segfile,ones);
  83.    }
  84.   }
  85.  fclose(fp);
  86.  fclose(fpout);
  87.  printf("/n文件合并成功!/n");
  88. }
  89. unsigned long getfilesize(char *file)
  90. {
  91.   FILE *fp;
  92.   unsigned long curpos1,curpos2,length;
  93.   fp=fopen(file,"rb");
  94.   curpos1=ftell(fp);
  95.   fseek(fp,0L,SEEK_END);
  96.   curpos2=ftell(fp);
  97.   length=curpos2-curpos1;
  98.   fseek(fp,curpos1,SEEK_SET);
  99.   return length;
  100.   fclose(fp);     
  101. }
  102. void copyfile(FILE *fpin,FILE *fpout,unsigned long fsize)
  103. {
  104. void *buf;
  105. int i,num;
  106. unsigned long remain;
  107. num=fsize/READ_SIZE;
  108. remain=fsize%READ_SIZE;
  109. buf=malloc(READ_SIZE);
  110. for (i=0;i<num;i++)
  111.  {
  112.   fread(buf,READ_SIZE,1,fpin);
  113.   fwrite(buf,READ_SIZE,1,fpout);
  114.  }
  115. if(remain)
  116.  {
  117.   fread(buf,remain,1,fpin);
  118.   fwrite(buf,remain,1,fpout);
  119.  }
  120. free(buf);
  121. }
 
原创粉丝点击