#include<iostream>
#include<windows.h>
#include<string.h>
#include<fstream.h>
void compact(char *a,char *b,HANDLE h);
char cpath[100];// 要压缩的原路经
void main()
{
char *tpath,path[50];
HANDLE hfind0,hfind1;
WIN32_FIND_DATA filedata0,filedata1;
cout<<"the source directory \n";
char *route,ch;
long i=0,j=0,b=0;//记述文件长度
cin>>path;
cout<<"the position of the file\"my.war\" \n";
cin>>cpath;
tpath=new char[50];
strcpy(tpath,path);
route=new char[50];
strcpy(route,path);
ofstream tfile0(cpath,ios::binary|ios::out|ios::app);
hfind0=FindFirstFile(path,&filedata0);//输入要压缩的路径的时候不能输错
tfile0<<path<<' ';
strcat(path,"\\*.*");
hfind1=FindFirstFile(path,&filedata1);
FindNextFile(hfind1,&filedata1);
b=FindNextFile(hfind1,&filedata1);
if(b!=0)
{
//cout<<filedata1.cFileName<<"filedata1\n";
tfile0<<"nfile ";
tfile0.close();
strcat(tpath,"\\\\");
strcat(tpath,filedata1.cFileName);
compact(tpath,route,hfind1);
}
else
{
ifstream tfile1(route,ios::binary);
if(tfile1.fail())
tfile0<<"nfile ";
else
{
tfile0<<"ifile ";
while(!tfile1.eof())
{
tfile1.get(ch);
i=i+1;
}
tfile0<<i-1<<' ';
tfile1.close();
ifstream tfile1(route,ios::binary);
for(j=0;j<i-1;j++)
{
tfile1.get(ch);
tfile0.put(ch);
//cout<<ch;
}
tfile0<<' ';
tfile0.close();
tfile1.close();
i=0;
}
}
cout<<"\n"<<"look up the "<<cpath<<"\n" ;
}
void compact(char *a,char *b,HANDLE h)
{
char *c,*d,ch,*t,*f;
long i=0,j=0,e=0;
HANDLE hfind3;
WIN32_FIND_DATA filedata3;
ofstream tfile0(cpath,ios::binary|ios::out|ios::app);
tfile0<<a<<' ';
d=new char[50];
f=new char[50];
strcpy(f,a);
strcpy(d,a);
strcat(d,"\\\\*.*");
hfind3=FindFirstFile(d,&filedata3);
FindNextFile(hfind3,&filedata3);
e=FindNextFile(hfind3,&filedata3);
if(e!=0)
{
tfile0<<"nfile ";
tfile0.close();
d=strcat(f,"\\\\");
strcat(d,filedata3.cFileName);
compact(d,a,hfind3);
}
else
{
ifstream tfile1(a,ios::binary);
if(tfile1.fail())
tfile0<<"nfile ";
else
{
tfile0<<"ifile ";
while(!tfile1.eof())
{
tfile1.get(ch);
i=i+1;
}
tfile0<<i-1<<' ';
tfile1.close();
ifstream tfile1(a,ios::binary);
for(j=0;j<i-1;j++)
{
tfile1.get(ch);
tfile0.put(ch);
//cout<<ch;
}
tfile0<<' ';
tfile0.close();
tfile1.close();
i=0;
}
}
while(FindNextFile(h,&filedata3)==1)
{
t=new char[50];
strcpy(t,b);
//cout<<filedata3.cFileName<<"filedata3,120\n";
e=0;
ofstream tfile0(cpath,ios::binary|ios::out|ios::app);
c=new char[50];
strcat(t,"\\\\");
strcat(t,filedata3.cFileName);
tfile0<<t<<' ';
strcpy(c,t);
strcat(c,"\\\\*.*");
hfind3=FindFirstFile(c,&filedata3);
FindNextFile(hfind3,&filedata3);
e=FindNextFile(hfind3,&filedata3);
//cout<<e;
if(e!=0)
{
tfile0<<"nfile ";
tfile0.close();
strcpy(c,t);
strcat(c,"\\\\");
strcat(c,filedata3.cFileName);
compact(c,t,hfind3);
}
else
{
ifstream tfile1(t,ios::binary);
if(tfile1.fail())
{
// cout<<"error\n";
tfile0<<"nfile ";
}
else
{
tfile0<<"ifile ";
while(!tfile1.eof())
{
tfile1.get(ch);
i=i+1;
}
tfile0<<i-1<<' ';
tfile1.close();
//cout<<b<<"158\n";
ifstream tfile1(t,ios::binary);
for(j=0;j<i-1;j++)
{
tfile1.get(ch);
tfile0.put(ch);
//cout<<ch;
}
tfile0<<' ';
tfile0.close();
tfile1.close();
i=0;
}
}
}
}