Code sous la licence BSD (g la flemme de copier/coller la licence)
Edit: Pour la forme j'ai mis la licence :
/*
Copyright (c) 2003, Godzil aka TRAPIER Manoël
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
Neither the name of 986 Corporation nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#define usage() { fprintf(stderr, "usage : spliter s size file basename\n spliter u basename file\nwhere :\n\t\t s : split a file\n\t\t u : unsplit a file\n\t\t size : Size of splited file in bytes\n\t\t file : for s file to split, for u file where to save\n\t\t basename : The basename for splited files (ie : MySplittedfile)\t\t Don't put an extention !\n\n"); }
unsigned long GetFileSize(FILE *fp)
{
unsigned long inSize;
unsigned long pos;
pos = ftell(fp);
fseek(fp,0,SEEK_END);
inSize = ftell(fp);
fseek(fp,pos,SEEK_SET);
return inSize;
}
#define KILO (1024)
#define MEGA (1024 * 1024)
#define GIGA (1024 * 1024 * 1024)
char *SaySize(unsigned long Size)
{
static char buffer[100];
double size = Size;
memset(buffer,0,100);
if ( ( size / GIGA ) >= 1.0 )
{
sprintf(buffer,"%4.3f Go", size / GIGA );
}
else if ( ( size / MEGA ) >= 1.0 )
{
sprintf(buffer,"%4.3f Mo", size / MEGA );
}
else if ( ( size / KILO ) >= 1.0 )
{
sprintf(buffer,"%4.3f Ko", size / KILO );
}
else /* Octet */
{
sprintf(buffer,"%4.3f Octet(s)", size );
}
return buffer;
}
void unsplitter(char *basename, char *filename)
{
FILE *sfp;
FILE *dfp;
char buff[20];
char NameBuffer[1024];
int finish;
unsigned long fNum;
if( (dfp = fopen(filename,"wb")) == NULL)
{
fprintf(stderr,"Error opening %s !\n",filename);
exit(-1);
}
printf("Unsplit %s.xxxxx.splt to %s....\nThis may take time...\n",basename,filename);
fNum = 0;
sprintf(NameBuffer,"%s.%0.5d.splt",basename,fNum);
putchar('\n');
putchar('-');
if( (sfp = fopen(NameBuffer,"rb")) == NULL)
{
fprintf(stderr,"Error opening %s !\n",filename);
exit(-1);
}
finish = 0;
while(finish == 0)
{
while(fread(buff,1,1,sfp) != 0)
{
fwrite(buff,1,1,dfp);
}
putchar('-');
fNum++;
sprintf(NameBuffer,"%s.%0.5d.splt",basename,fNum);
if( (sfp = fopen(NameBuffer,"rb")) == NULL)
{
finish = 1;
}
}
fclose(dfp);
printf("Finished !\n");
}
void splitter(unsigned long size, char *filename, char *basename)
{
FILE *sfp;
FILE *dfp;
char buff[20];
char NameBuffer[1024];
unsigned long fSize;
unsigned long fNum;
unsigned long pos;
if( (sfp = fopen(filename,"rb")) == NULL)
{
fprintf(stderr,"Error opening %s !\n",filename);
exit(-1);
}
fSize = GetFileSize(sfp);
printf("%s is %s\n",filename,SaySize(fSize));
printf("Will split approximativly in %d file(s) of %s",fSize/size,SaySize(size));
pos = 0;
fNum = 0;
sprintf(NameBuffer,"%s.%0.5d.splt",basename,fNum);
putchar('\n');
putchar('-');
if( (dfp = fopen(NameBuffer,"wb")) == NULL)
{
fprintf(stderr,"Error opening %s !\n",filename);
exit(-1);
}
while(!feof(sfp))
{
if (pos >= size)
{
pos = 0;
fNum++;
sprintf(NameBuffer,"%s.%0.5d.splt",basename,fNum);
putchar('-');
fclose(dfp);
if( (dfp = fopen(NameBuffer,"wb")) == NULL)
{
fprintf(stderr,"Error opening %s !\n",filename);
exit(-1);
}
}
if ( fread(buff,1,1,sfp) != 0 )
fwrite(buff,1,1,dfp);
pos ++;
}
fclose(dfp);
fclose(sfp);
printf("Finished !\n");
}
int main(int argc, char *argv[])
{
if ( ( argc !=4 ) && ( argc !=5 ) )
{
usage();
exit(-1);
}
if ( argv[1][0] == 's' )
{ /* Split the file */
/*
exe s size file basename
*/
splitter(atoi(argv[2]),argv[3],argv[4]);
}
else if ( argv[1][0] == 'u' )
{ /* Unsplit the file */
/*
exe u basename file
*/
unsplitter(argv[2],argv[3]);
}
else
{
usage();
exit(-1);
}
}