⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mypacket.cpp

📁 windows下的压缩解压程序
💻 CPP
字号:
#include "stdafx.h"
#include "windows.h"
#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>

#define MAC_FILENAMELENOPATH 50

int judge[100];//0:文件,1:文件夹
char names[100][50];//文件或文件夹的名
int num=0;//文件和文件夹个数
long length[100];//文件的长度 
int dirlen=0;//根目录名长度(中间变量,不需存储)

void war(char* rootDir,char* filename);

void dewar(char* dir ,  char* file);//dir:新建路径名,file:.war文件路径名及文件名

void writefile(char* file_1,char* file_2,long count);//count:the length of file_1

void saveSource(char* filename);//save the arrays , judge ,names, length and the variable num

void load(char* filename);//load data to arrays 
 	


int main()
{
	cout<<"please tell me what do you want to do:"<<endl;
	cout<<"1 : war  2 :dewar"<<endl;
	cout<<"please input your demand:";
	char i; cin>>i; cout<<endl;
	cout<<"--------------------------------------------------------------------"<<endl;
	while(i !=EOF )
	{
		switch(i) {
		case '1':
	        char strPath_1[32],strPath_2[32];
	        ZeroMemory(strPath_1,32);
            ZeroMemory(strPath_2,32);	
	      
		    cout<<"input the directory"<<endl;
	        cin>>strPath_1;
	        dirlen=strlen(strPath_1);	 
	        cout<<"input the filename"<<endl;
	        cin>>strPath_2;
	        
		    num=0; ZeroMemory(judge,100); ZeroMemory(length,100); ZeroMemory(names,5000);
		    
	        war(strPath_1, strPath_2);
	        saveSource(strPath_2);
		    break;

		case '2':
            char strPath_3[32],strPath_4[32];
			ZeroMemory(strPath_3,32);
			ZeroMemory(strPath_4,32);

			cout<<"input the filename of *.war"<<endl;
			cin>>strPath_3;
			cout<<"input the directory of dewar"<<endl;
			cin>>strPath_4;

            num=0; ZeroMemory(judge,100); ZeroMemory(length,100); ZeroMemory(names,5000);


	        load(strPath_3);     
	 
	        dewar(strPath_4,strPath_3);	 	 
			break;

		case 'q':
			exit(1);
			break;

		default:
			cout << "please input correctly" << endl;
			break;
		}
		cout << "press q to quit"<<endl;
		cin >> i; cout << endl;
	}
	return 0;
}
  
void war(char* rootDir, char* filename)
{	 
	char fname[MAC_FILENAMELENOPATH];
	ZeroMemory(fname, MAC_FILENAMELENOPATH);

	WIN32_FIND_DATA fd;
	ZeroMemory(&fd, sizeof(WIN32_FIND_DATA));

	HANDLE hSearch;
	char filePathName[256];
	char tmpPath[256];
	char relaPathName[256];

	ZeroMemory(filePathName, 256);
	ZeroMemory(tmpPath, 256);
    ZeroMemory(relaPathName,256);

	strcpy(filePathName, rootDir);

	BOOL bSearchFinished = FALSE;

	if( filePathName[strlen(rootDir)-1] != '\\' )
	{
		strcat(filePathName, "\\");
		strcat(rootDir,"\\");
	}

	strcat(filePathName, "*");
     
	hSearch = FindFirstFile(filePathName, &fd);

	    
	//Is directory
	if( (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
		&& strcmp(fd.cFileName, ".") && strcmp(fd.cFileName, "..") )		
	{    
		strcpy(tmpPath, rootDir);
		strcat(tmpPath, fd.cFileName);
        num+=1;
		judge[num]=1;
		strcpy(relaPathName,"\\");
		strcat(relaPathName,fd.cFileName);
		strcpy(names[num],relaPathName);
		war(tmpPath,filename);//递归
	}
	//is file

	else	if( strcmp(fd.cFileName, ".") && strcmp(fd.cFileName, "..") )
				{  	
		            sprintf(fname, "%-50.50s", fd.cFileName);
					//strcat(strRet + strRet[strlen(strRet)] , fname);                     
		            strcpy(tmpPath, rootDir);
					strcat(tmpPath, fd.cFileName);	
					num+=1;
					judge[num]=0;
					strcpy(relaPathName,"\\");
					strcat(relaPathName,fd.cFileName);
					strcpy(names[num],relaPathName);
					length[num]=fd.nFileSizeLow;
					writefile(tmpPath,filename,fd.nFileSizeLow);					 
				}

	while( !bSearchFinished )
	{
		if( FindNextFile(hSearch, &fd) )
		{
			if( (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
				&& strcmp(fd.cFileName, ".") && strcmp(fd.cFileName, "..") )		
			{    
				strcpy(tmpPath, rootDir);
				strcat(tmpPath, fd.cFileName);
				num+=1;
				judge[num]=1;
			    for(int i=dirlen;i < strlen(tmpPath);i++)
				{
					names[num][i-dirlen]=tmpPath[i];
				}
				war(tmpPath,filename);
			}
	
			else	if( strcmp(fd.cFileName, ".") && strcmp(fd.cFileName, "..") )
			{                 
							sprintf(fname, "%-50.50s", fd.cFileName);
							//strcat(strRet + strRet[strlen(strRet)] , fname);                             
							strcpy(tmpPath, rootDir);							 
		                    strcat(tmpPath, fd.cFileName);	
							num+=1;
							judge[num]=0;
							for(int i=dirlen;i < strlen(tmpPath);i++)
							{
					            names[num][i-dirlen]=tmpPath[i];
							}
							length[num]=fd.nFileSizeLow;
					        writefile(tmpPath,filename,fd.nFileSizeLow);		 
							
						}
		}
		else
		{
			if( GetLastError() == ERROR_NO_MORE_FILES )			//Normal Finished
			{
				bSearchFinished = TRUE;
			}
			else
				bSearchFinished = TRUE;		//Terminate Search
		}
	}

	FindClose(hSearch);
	
}

void dewar(char* dir,char* file)
{   
	
	fstream File(file,ios::in|ios::out|ios::binary);	
	char path[64];
	ZeroMemory(path,64);

	for(int i=0; i< strlen(dir);i++)//建立新路径
	{   
        path[i]=dir[i];    
		if (dir[i]=='\\')
		{
			CreateDirectory(path,NULL);
		}
	}
	CreateDirectory(dir,NULL);
    
	char filename[64];
	ZeroMemory(filename,64);
	for(i=0;i <= num ;i++)//建立文件夹
	{
		if (judge[i]==1){
           strcpy(filename,dir);
		   strcat(filename,names[i]);   
		   ZeroMemory(path,64);
	       for(int flag=0; flag< strlen(filename);flag++)
		   {   
               path[flag]=filename[flag];    
		       if (filename[flag]=='\\')
			   {
			       CreateDirectory(path,NULL);
			   } 
		   }   
		   CreateDirectory(filename,NULL);
		}	 	 
	}
	long signal=0;
	for(i=1;i <= num;i++)//建立文件
	{   
		if (judge[i]==0){			
			strcpy(filename,dir);
			strcat(filename,names[i]);
			fstream newfile(filename,ios::in|ios::out|ios::binary);
			long count=length[i];
			char ch;
			File.seekg(signal);
			for(long flag=1;flag<=count;flag++)
			{
				File.get(ch);
				newfile.put(ch);
			}
			newfile.close();
			signal+=length[i];
		}
	}
	 
    File.close();
}


void writefile(char* file_1,char* file_2,long count)//count:the length of file_1
{    
	fstream File_1(file_1,ios::in | ios::out | ios::binary );
	if (!File_1){
		cerr<<"can not open File_1"<<endl;
		exit(1);
	}
	fstream File_2(file_2,ios::in | ios::out | ios::binary | ios::app );
	if (!File_2){
		cerr<<"can not open File_2"<<endl;
		exit(2);
	}
	char ch;
	
	while(count>0)
	{
		File_1.get(ch);		 
		File_2.put(ch);
		count-=1;
	}
     
	File_1.close();
	File_2.close();	 
} 

void load(char* filename)
{
	ifstream file(filename,ios::in | ios::binary);	
	char ch;  int t=-1;	
    file.seekg(t,ios::end);

	file>>ch;
	while(ch!='@')
	{
		file.seekg(t--,ios::end);		
		file>>ch;
	}

	long size; file>>size;
	file.close(); 

	ifstream file1(filename,ios::in | ios::binary);		
	file1.seekg(size,ios::beg);


    file1>>num;

	for(int i=1;i <= num;i++)
	{   
		file1>>judge[i];	

	}
	for(i=1; i <= num;i++)
	{
		file1>>length[i];	 

	}
	for(i=1; i <= num;i++)
	{
		for(int s=0;s < 50 ;s++)
		{
			file1>>names[i][s];			 

		}		 

	} 
    file1.close();
}
 
void saveSource(char* filename)
{
	fstream file(filename, ios::out | ios::binary | ios::app);
	file.seekp(0,ios::end);
	long fileSize=file.tellp();

	file<<' ';
	file<<num<<' ';
	for(int i=1;i <= num;i++)
	{
		file<<judge[i]<<' ';
	}
	for(i=1;i <= num;i++)
	{
		file<<length[i]<<' ';
	}
	for(i=1;i <= num;i++)
	{
		for(int s=0;s < 50;s++)
		{
			file<<names[i][s];
		}
	}
    file<<'@';
	file<<fileSize;
    file.close();
}

     

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -