文件的分割和合并
来源:互联网 发布:淘宝店装修多少钱 编辑:程序博客网 时间:2024/04/28 15:01
segfile.c
- /*
- Name: segfile(文件分割)
- Copyright: 天奇居
- Author: 加州旅客
- Date: 30-04-08 23:15
- Description:可以将较大的文件分割成数块,以便进行复制、传输。用法:segfile 单个文件大小 原文件路径 [分割文件保存路径]。
- */
- #include<stdio.h>
- #include<sys/stat.h>
- #include<fcntl.h>
- #include<stdlib.h>
- #include<ctype.h>
- #include<string.h>
- #include<stdlib.h>
- #define READ_SIZE (1048576*2)
- unsigned long getfilesize(char *file);
- void copyfile(FILE *fpin,FILE *fpout,unsigned long fsize);
- int main(int argc,char **argv)
- {
- FILE *fp,*fpseg,*fplist,*fpcmd;
- int len,sharelen,i,j,n;
- char *s,savepath[512],*filename,filepath[512]="",lenstr[16],nowsegname[512],segfilename[512]="",seglist[512],mixcmd[512];
- unsigned long filesize,indivfile,lastfile,fileshare,nowsegsize;/*indivfile:单个文件大小;lastfile:最后一个文件的大小;fileshare:分割的文件份数*/
- if (argc<2)
- {
- printf("参数不够!/n用法:segfile 单个文件大小[K|M|G] 原文件路径 [分割文件保存路径]/n");
- exit(-1);
- }
- fp=fopen(argv[2],"r");
- if (fp==NULL)
- {
- printf("找不到所要分割的文件,请检查你的输入是否有误或你是否有读取该文件的权限!/n");
- exit(-1);
- }
- fclose(fp);
- filename=strrchr(argv[2],'//');
- if (filename==0)
- {
- filename=argv[2];
- }
- else
- {
- filename++;
- len=strlen(argv[2])-strlen(filename);
- strncpy(filepath,argv[2],len);
- }
- if (argc=3)
- {
- strcpy(savepath,filepath);
- }
- else
- {
- s=strcpy(savepath,argv[3]);
- s=s+strlen(s)-1;
- if(*s!='//')strcat(savepath,"//");
- }
- filesize=getfilesize(argv[2]);
- indivfile=strtoul(argv[1],NULL,10);
- len=strlen(argv[1]);
- s=argv[1]+len-1;
- switch (*s)
- {
- case 'k':
- case 'K':
- indivfile*=1024;
- break;
- case 'm':
- case 'M':
- indivfile*=1048576;
- break;
- case 'g':
- case 'G':
- indivfile*=1073741824;
- break;
- }
- if (indivfile>filesize)
- {
- return -1;
- }
- lastfile=filesize%indivfile;
- fileshare=filesize/indivfile;
- if (lastfile>0)
- {
- fileshare++;
- }
- else
- {
- lastfile=indivfile;
- }
- printf("待分割的文件:%s%s,大小是:%lu字节。/n分割文件保存路径:%s,分割文件个数是:%lu。/n现在开始分割…/n",filepath,filename,filesize,savepath,fileshare);
- sprintf(lenstr,"%lu",fileshare);
- sharelen=strlen(lenstr);
- fp=fopen(argv[2],"rb");
- strcpy(seglist,savepath);
- strcat(seglist,filename);
- strcat(seglist,".seglist");
- fplist=fopen(seglist,"w");
- strcpy(mixcmd,savepath);
- strcat(mixcmd,filename);
- strcat(mixcmd,".cmd");
- fpcmd=fopen(mixcmd,"w");
- fprintf(fpcmd,"copy /b ");
- for (i=1;i<=fileshare;i++)
- {
- sprintf(lenstr,"%d",i);
- strcpy(segfilename,filename);
- strcat(segfilename,".");
- if (sharelen>strlen(lenstr))
- {
- for(j=1;j<=(sharelen-strlen(lenstr));j++)
- {
- strcat(segfilename,"0");
- }
- }
- strcat(segfilename,lenstr);
- strcat(segfilename,".seg");
- fprintf(fplist,"%s/n",segfilename);
- if (i<fileshare)
- {
- fprintf(fpcmd,"/"%s/"+",segfilename);
- }
- else
- {
- fprintf(fpcmd,"/"%s/"",segfilename);
- }
- strcpy(nowsegname,savepath);
- strcat(nowsegname,segfilename);
- if (i<fileshare)
- {
- nowsegsize=indivfile;
- }
- else
- {
- nowsegsize=lastfile;
- }
- fpseg=fopen(nowsegname,"wb");
- copyfile(fp,fpseg,nowsegsize);
- fclose(fpseg);
- }
- fprintf(fpcmd," /"%s/"",filename);
- fclose(fplist);
- fclose(fp);
- printf("/n分割完成!!!/n分割文件列表为:%s,你可以用“mixfile.exe”读取此列表文件来合并分割的文件。/n生成的批处理文件为:%s,在windows下可以直接双击该文件来合并文件。/n注意:所有文件(包括列表文件和批处理文件)都必须放在同一文件夹下!否则程序会因为找不到文件而导致无法合并出完整源文件!/n",seglist,mixcmd);
- }
- unsigned long getfilesize(char *file)
- {
- FILE *fp;
- unsigned long curpos1,curpos2,length;
- fp=fopen(file,"rb");
- curpos1=ftell(fp);
- fseek(fp,0L,SEEK_END);
- curpos2=ftell(fp);
- length=curpos2-curpos1;
- fseek(fp,curpos1,SEEK_SET);
- return length;
- fclose(fp);
- }
- void copyfile(FILE *fpin,FILE *fpout,unsigned long fsize)
- {
- void *buf;
- int i,num;
- unsigned long remain;
- num=fsize/READ_SIZE;
- remain=fsize%READ_SIZE;
- buf=malloc(READ_SIZE);
- for (i=0;i<num;i++)
- {
- fread(buf,READ_SIZE,1,fpin);
- fwrite(buf,READ_SIZE,1,fpout);
- }
- if(remain)
- {
- fread(buf,remain,1,fpin);
- fwrite(buf,remain,1,fpout);
- }
- free(buf);
- }
mixfile.c
- /*
- Name: mixfile(文件分割)
- Copyright: 天奇居
- Author: 加州旅客
- Date: 30-04-08 23:15
- Description:可将segfile分割的文件合并。用法:mixfile 列表文件 [输出文件名]。
- */
- #include<stdio.h>
- #include<string.h>
- #define READ_SIZE (1048576*2)
- unsigned long getfilesize(char *file);
- void copyfile(FILE *fpin,FILE *fpout,unsigned long fsize);
- int main(int argc,char **argv)
- {
- FILE *fpseg,*fpout,*fp;
- int len,i;
- char *filename,filepath[512]="",*s,pristinefile[512],c,ones[1],segfile[256]="",segfilepath[512];
- unsigned long segfilesize;
- if (argc<2)
- printf("用法:mixfile 列表文件 [输出文件名]/n");
- fp=fopen(argv[1],"r");
- if (fp==NULL)
- {
- printf("找不到分割文件列表,请检查你的输入是否有误或你是否有读取该文件的权限!/n");
- exit(-1);
- }
- fclose(fp);
- filename=strrchr(argv[1],'//');
- if (filename==0)
- {
- filename=argv[1];
- }
- else
- {
- filename++;
- len=strlen(argv[1])-strlen(filename);
- strncpy(filepath,argv[1],len);
- }
- if (argc<3)
- {
- strcpy(pristinefile,filepath);
- s=strrchr(filename,'.');
- if (s==0)
- {
- strcat(pristinefile,filename);
- strcat(pristinefile,".out");
- }
- else
- {
- len=strlen(filename)-strlen(s);
- strncat(pristinefile,filename,len);
- }
- }
- else
- {
- strcpy(pristinefile,argv[2]);
- }
- fpout=fopen(pristinefile,"wb");
- printf("现在开始将分割文件合并到%s……/n/n",pristinefile);
- fp=fopen(argv[1],"r");
- while((c=fgetc(fp))!=EOF)
- {
- if (c=='/n')
- {
- strcpy(segfilepath,filepath);
- strcat(segfilepath,segfile);
- segfilesize=getfilesize(segfilepath);
- fpseg=fopen(segfilepath,"rb");
- if (fp==NULL)
- {
- printf("无法打开分割文件%s!/n",segfilepath);
- exit(-1);
- }
- printf("正在读取%s,大小为%lu字节.../n",segfilepath,segfilesize);
- copyfile(fpseg,fpout,segfilesize);
- fclose(fpseg);
- strcpy(segfile,"");
- }
- else
- {
- sprintf(ones,"%c",c);
- strcat(segfile,ones);
- }
- }
- fclose(fp);
- fclose(fpout);
- printf("/n文件合并成功!/n");
- }
- unsigned long getfilesize(char *file)
- {
- FILE *fp;
- unsigned long curpos1,curpos2,length;
- fp=fopen(file,"rb");
- curpos1=ftell(fp);
- fseek(fp,0L,SEEK_END);
- curpos2=ftell(fp);
- length=curpos2-curpos1;
- fseek(fp,curpos1,SEEK_SET);
- return length;
- fclose(fp);
- }
- void copyfile(FILE *fpin,FILE *fpout,unsigned long fsize)
- {
- void *buf;
- int i,num;
- unsigned long remain;
- num=fsize/READ_SIZE;
- remain=fsize%READ_SIZE;
- buf=malloc(READ_SIZE);
- for (i=0;i<num;i++)
- {
- fread(buf,READ_SIZE,1,fpin);
- fwrite(buf,READ_SIZE,1,fpout);
- }
- if(remain)
- {
- fread(buf,remain,1,fpin);
- fwrite(buf,remain,1,fpout);
- }
- free(buf);
- }
- 文件的分割和合并
- 文件的分割和合并
- 文件分割和合并
- Java大文件的分割和合并
- Linux文件分割和合并
- Wireshark 文件分割和合并
- 黑马程序员_学习笔记文件的分割和合并
- java流操作对文件的分割和合并
- 黑马程序员_文件的分割和合并
- Linux split cat 分割和合并文件的指令
- linux下分割文件和合并文件
- 文件分割和合并多线程实现
- Php处理大文件-分割和合并
- PowerShell 分割和合并大文件
- 用iText分割和合并pdf文件
- java中的文件分割和合并
- 分割流和合并流的应用
- 文件的切割和合并
- 美国兰德公司对中国人的评价(值得思考)
- 最简单的延时程序
- IE6,IE7 与Firefox的JS,CSS不同 (不断更新中....)
- CodeGear RadStudio Delphi 2007 Delphi2009 最新破解补丁集
- My Love
- 文件的分割和合并
- Nebula3渲染层: Graphics
- Take Me To Your Heart
- 祝自己考研成功!
- 删除文件空行
- 只问耕耘,不问收获;但行好事,莫问前程。
- Encryption Review- Part 2 Public-key Cryptograph
- 床前明月光
- 21句