File Splitter & Merger … Updated Version 1.1

Hello ,

I finished writing two programs , One will split a file into a specified number of pieces .Gonna study some file compression algorithm .

I later thought its a waste of time writing another file compression alogol so used gzip itself . And if i have to change to new algo i made the usage of gzip modular so that any new algol can be integerated as a seperate module . 🙂

SPLITTER

#include<stdio.h>
#include<unistd.h>
#include<string.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<fcntl.h>

int fd=0,err=0,file_size=0;
int block_numbers=0;
char buffer[1024];
char tno[5];

//Argument 1 is File to split
//Argument 2 is block sizes in mb

int main(int argc,char *argv[])
{

int i,j,k,tfd,fileno=1,byte_count=0,nob=0,mb=0,err=0;
struct stat buf;
char dest[25]=”../dumps/dest”;
char filename[25],*arg[20];

arg[0]=(char *)malloc(5);  // gzip

arg[1]=(char *)malloc(5);  // file name

arg[2]=(char *)malloc(5);

arg[0]=”gzip”;

arg[2]=NULL;

for(i=0;i<1024;i++){
buffer[i]=”;
if(i<25){
dest[i]=filename[i]=”;
if(i<5){
tno[i]=”;
}
}
}

if(argc > 2){

mb=atoi(argv[2]);

strcpy(dest,argv[1]);
strcat(dest,”.abe”);

if( mkdir(dest,0777) >=0 ){

strcat(dest,”/dest”);
strcpy(filename,dest);
//strcpy(dest,argv[2]);
}
else{
perror(“”);

printf(“\nDirectory Creation Failure . Program Exitting \n”);
//strcpy(dest,”../dumps/dest”);
//strcpy(filename,dest);
exit(0);
}

fd=open(argv[1],O_RDONLY);
if(fd==-1){
perror(“Main File:”);
exit(0);
}
err=fstat(fd,&buf);
if(err==-1){
perror(“First stat:”);
}

printf(“\nThe size of the file is : %d”,buf.st_size);
file_size=buf.st_size;
block_numbers=file_size/(1024*1024*mb);//Last Param is number of MS’s

block_numbers++;

printf(“\nThere will be  %d “,block_numbers);
printf(“block/blocks \n”);
k=block_numbers;

strcpy(filename,dest);
itoa(fileno,&tno);
strcat(filename,tno);

tfd=open(filename,O_RDWR | O_CREAT , 0666);
if(tfd<0){
perror(“Temp File:”);
}

while((nob = read(fd,&buffer,1024))>0 ){
//write to file
err=write(tfd,&buffer,nob);
if(err<0){
perror(“Tfile Write :”);
}

byte_count+=nob;

//flush buffer
flush_buf();

//open new file
if(byte_count==1024 * 1024 * mb){

byte_count=0;
close(tfd);

//Zip the closed file

strcpy(arg[1],filename);

if(!fork()){

execvp(“gzip”,arg);
}
else{

fileno++;
flush_buf();

itoa(fileno,&tno);
for(i=0;i<25;i++){
filename[i]=”;
}
strcpy(filename,dest);
strcat(filename,tno);
tfd=open(filename,O_RDWR | O_CREAT , 0666);
if(tfd<0){
perror(“Tfile Creation “);
}

}//Else of Fork
}
nob=0;
}

strcpy(arg[1],filename);

if(!fork()){

execvp(“gzip”,arg);

}

}
else{
write(1,”Enter all the parammters . Program quitting”,20);
exit(0);
}
return 0;
}

// Convert the integer to string

void itoa(int a ,char *p)
{
int i,j,k;
char s[5];
s[0]=s[1]=s[2]=s[3]=s[4]=”;

k=a;i=0;

while(a>0){
j=a%10;
a=a/10;
s[i]=j+48;
i++;
}

// Reverse it
i=0;
j=0;
while(s[i]!=”){
i++;
}

for(k=0;k<i;k++,i–){
p[k]=s[i-1];
}

}

void flush_buf()
{
int i=0;

for(i=0;i<1024;i++){
buffer[i]=”;
}
}

MERGER

#include<stdio.h>
#include<fcntl.h>
#include<dirent.h>

char buffer[1024];

//Argument 1 Directory path relative to the excutable

int main(int argc,char *argv[])
{
int fd,mfd,err,i,j,nob=0,file_count=0,status;

char source[30],final[30],f_name[30],fi[5],*arg[5];

DIR *dp=NULL;

struct dirent *bf=NULL;

arg[0]=(char *)malloc(10);  //GZIP

arg[1]=(char *)malloc(10);  //OPTIONS FOR UNZIPPING

arg[2]=(char *)malloc(30);  //FILE NAME

arg[3]=(char *)malloc(10);  //NULL

arg[0]=”gzip”;

arg[1]=”-d”;

arg[3]=NULL;

for(i=0;i<30;i++){

f_name[i]=final[i]=source[i]=”;

if(i<5){

fi[i]=”;

}
}

if(argc<2){

printf(“\nInsufficient parameters : Program Quitting\n”);
exit(0);

}

strcpy(source,argv[1]);

for(i=29;i>0;i–){

if(source[i]==’.’){

break;

}

}

for(j=0;j<i;j++){

final[j]=source[j];

}

strcat(final,”merged”);

dp=opendir(source);
if(dp==NULL){

perror(“Directory “);

}

mfd=open(final,O_WRONLY | O_CREAT , 0666);

if(mfd<0){
perror(“Main File”);
}

flush_buf();

while( ( bf=readdir(dp) ) > 0 ){

if(strcmp(bf->d_name,”.”) && strcmp(bf->d_name,”..”)){

file_count++;

strcpy(arg[2],”/home/unni/dinesh/test/”);

strcat(arg[2],source);

strcat(arg[2],”/”);

strcat(arg[2],bf->d_name);

if(!fork()){

execvp(“gzip”,arg);
perror(“”);
}
else{
waitpid(-1,&status,0);

}

}
}

for(i=0;i<file_count;i++){

strcpy(f_name,source);

strcat(f_name,”/dest”);

itoa(i+1,&fi);

strcat(f_name,fi);

//printf(“\n\n %s \n\n “,f_name);

fd=open(f_name,O_RDONLY);
if(fd<0){
perror(“Temp File”);
}

while(nob=read(fd,&buffer,1024)){

err=write(mfd,&buffer,nob);
if(err<0){
perror(“Write “);
exit(0);
}
flush_buf();
}

close(fd);
sleep(1);

}

close(mfd);
return 0;
}

void itoa(int a ,char *p)
{
int i,j,k;
char s[5];
s[0]=s[1]=s[2]=s[3]=s[4]=”;

k=a;i=0;

while(a>0){
j=a%10;
a=a/10;
s[i]=j+48;
i++;
}

// Reverse it
i=0;
j=0;
while(s[i]!=”){
i++;
}

for(k=0;k<i;k++,i–){
p[k]=s[i-1];
}

}

void flush_buf()
{
int i=0;

for(i=0;i<1024;i++){
buffer[i]=”;
}
}

Good bye

Advertisements

~ by dineshabe on December 17, 2007.

One Response to “File Splitter & Merger … Updated Version 1.1”

  1. This was my Diploma project in VB

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: