MFC文件操作大全(二)

28.打开对话框

  1. CFileDialog mFileDlg(TRUE,NULL,NULL,  
  2. OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT|OFN_ALLOWMULTISELECT,  
  3. All Files (*.*)|*.*||,AfxGetMainWnd());  
  4. CString str(” ”,10000);  
  5. mFileDlg.m_ofn.lpstrFile=str.GetBuffer(10000);  
  6. str.ReleaseBuffer();  
  7. POSITION mPos=mFileDlg.GetStartPosition();  
  8. CString pathName(” ”,128);  
  9. CFileStatus status;  
  10. while(mPos!=NULL)  
  11. {  
  12.   pathName=mFileDlg.GetNextPathName(mPos);  
  13.   CFile::GetStatus( pathName, status );  
  14. }  
CFileDialog mFileDlg(TRUE,NULL,NULL,
OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT|OFN_ALLOWMULTISELECT,
All Files (*.*)|*.*||,AfxGetMainWnd());
CString str(" ",10000);
mFileDlg.m_ofn.lpstrFile=str.GetBuffer(10000);
str.ReleaseBuffer();
POSITION mPos=mFileDlg.GetStartPosition();
CString pathName(" ",128);
CFileStatus status;
while(mPos!=NULL)
{
  pathName=mFileDlg.GetNextPathName(mPos);
  CFile::GetStatus( pathName, status );
}

29.文件分割

  1. CFile m_File;  
  2. CString m_Filename,m_FileTitle,m_FilePath;  
  3. m_FileName=%%1;  
  4. char pBuf[4096];  
  5. if(m_File.Open(m_FileName,CFile::modeRead | CFile::shareDenyWrite))  
  6. {  
  7. m_FileName=m_File.GetPathName();  
  8. m_FileTitle=m_File.GetFileTitle();  
  9. DWORD FileLength=m_File.GetLength();  
  10. DWORD PartLength=FileLength/2+FileLength%2;  
  11. int nCount=1;  
  12. CString strName;  
  13. CFile wrFile;  
  14. DWORD ReadBytes;  
  15. while(true)  
  16. {  
  17. ReadBytes=m_File.Read(pBuf,PartLength);  
  18. strName.Format(”%s%d”,m_FIleTitle,nCount);  
  19. wrFile.Open(strName,CFile::modeWrite | CFile::modeCreate);  
  20. wrFile.Write(pBuf,ReadBytes);  
  21. wrFile.Close();  
  22. if(ReadBytes<PartLength)  
  23. break;  
  24. nCount++;  
  25. }  
  26. m_File.Close();  
  27. }  
  28. else  
  29. AfxMessageBox(”不能打开文件”);  
CFile m_File;
CString m_Filename,m_FileTitle,m_FilePath;
m_FileName=%%1;
char pBuf[4096];
if(m_File.Open(m_FileName,CFile::modeRead | CFile::shareDenyWrite))
{
m_FileName=m_File.GetPathName();
m_FileTitle=m_File.GetFileTitle();
DWORD FileLength=m_File.GetLength();
DWORD PartLength=FileLength/2+FileLength%2;
int nCount=1;
CString strName;
CFile wrFile;
DWORD ReadBytes;
while(true)
{
ReadBytes=m_File.Read(pBuf,PartLength);
strName.Format("%s%d",m_FIleTitle,nCount);
wrFile.Open(strName,CFile::modeWrite | CFile::modeCreate);
wrFile.Write(pBuf,ReadBytes);
wrFile.Close();
if(ReadBytes<PartLength)
break;
nCount++;
}
m_File.Close();
}
else
AfxMessageBox("不能打开文件");

30.文件合并

  1. //#include <string>  
  2. using std::string;  
  3. string s(%%1);  
  4. char sep=‘/’;  
  5. #ifdef _WIN32  
  6. sep=’\\’;  
  7. #endif  
  8. size_t sz=s.rfind(sep,s.length());  
  9. if(sz!=string::npos)  
  10. {  
  11. CFile Out;  
  12. CString strFilename(s.substr(i+1,s.length()-i));  
  13. if(Out.Open(%%2+“\\”+strFilename,CFile::modeWrite|CFile::modeCreate)){  
  14. for(int i=1;i<=2;i++)  
  15. {  
  16. String Filename=%%%2+”\\”+strFilename+atoi(i);  
  17. CFile In;  
  18. if(In.Open(Filename,CFile::modeRead)){  
  19. char cbBuffer[4096];  
  20. int nFilesize=In.GetLength();  
  21. while(nFilesize>0){  
  22. int nSize=sizeof(cbBuffer);  
  23. if(nSize>nFilesize)  
  24. nSize=nFilesize;  
  25. try{  
  26. In.Read(cbBuffer,nSize);  
  27. }  
  28. catch(CFileException *e){  
  29. char *lpMsgBuf;  
  30. if(FormatMessage(  
  31. FORMAT_MESSAGE_ALLOCATE_BUFFER |  
  32. FORMAT_MESSAGE_FROM_SYSTEM,  
  33. NULL,e->m_lOsError,  
  34. MAKELANGID(LANG_NEUTRAL,  
  35. SUBLANG_DEFAULT),  
  36. (LPSTR)&lpMsgBuf,0,NULL)>0){  
  37. AfxMessageBox(lpMsgBuf);  
  38. LocalFree(lpMsgBuf);  
  39. }  
  40. e->Delete();  
  41. return;  
  42. }  
  43. try{  
  44. Out.Write(cbBuffer,nSize);  
  45. }  
  46. catch(CFileException *e){  
  47. char *lpMsgBuf;  
  48. if(FormatMessage(  
  49. FORMAT_MESSAGE_ALLOCATE_BUFFER |  
  50. FORMAT_MESSAGE_FROM_SYSTEM,  
  51. NULL,e->m_lOsError,  
  52. MAKELANGID(LANG_NEUTRAL,  
  53. SUBLANG_DEFAULT),  
  54. (LPSTR)&lpMsgBuf,0,NULL)>0){  
  55. AfxMessageBox(lpMsgBuf);  
  56. LocalFree(lpMsgBuf);  
  57. }  
  58. e->Delete();  
  59. return;  
  60. }  
  61. nFilesize=nSize;  
  62. }  
  63. }  
  64. else  
  65. AfxMessageBox(”不能打开”+Filename);  
  66. }  
  67. }  
  68. else  
  69. AfxMessageBox(”不能创建输出文件”);  
  70. }  
//#include <string>
using std::string;
string s(%%1);
char sep='/';




#ifdef _WIN32 sep='\\'; #endif size_t sz=s.rfind(sep,s.length()); if(sz!=string::npos) { CFile Out; CString strFilename(s.substr(i+1,s.length()-i)); if(Out.Open(%%2+"\\"+strFilename,CFile::modeWrite|CFile::modeCreate)){ for(int i=1;i<=2;i++) { String Filename=%%%2+"\\"+strFilename+atoi(i); CFile In; if(In.Open(Filename,CFile::modeRead)){ char cbBuffer[4096]; int nFilesize=In.GetLength(); while(nFilesize>0){ int nSize=sizeof(cbBuffer); if(nSize>nFilesize) nSize=nFilesize; try{ In.Read(cbBuffer,nSize); } catch(CFileException *e){ char *lpMsgBuf; if(FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL,e->m_lOsError, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&lpMsgBuf,0,NULL)>0){ AfxMessageBox(lpMsgBuf); LocalFree(lpMsgBuf); } e->Delete(); return; } try{ Out.Write(cbBuffer,nSize); } catch(CFileException *e){ char *lpMsgBuf; if(FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL,e->m_lOsError, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&lpMsgBuf,0,NULL)>0){ AfxMessageBox(lpMsgBuf); LocalFree(lpMsgBuf); } e->Delete(); return; } nFilesize=nSize; } } else AfxMessageBox("不能打开"+Filename); } } else AfxMessageBox("不能创建输出文件"); }

31.文件简单加密

  1. //#include <string>  
  2. using std::string;  
  3. string s(%%1);  
  4. char sep=‘/’;  
  5. #ifdef _WIN32  
  6. sep=’\\’;  
  7. #endif  
  8. size_t sz=s.rfind(sep,s.length());  
  9. if(sz!=string::npos)  
  10. {  
  11. CFile Out,In;  
  12. int nFIlesize;  
  13. char *lpMsgBuf;  
  14. CString strFilename(s.substr(i+1,s.length()-i));  
  15. if(!in.Open(%%1,CFile::modeRead)){  
  16. //不能打开输入文件  
  17. return;  
  18. }  
  19. if(!Out.Open(%%2+“\\enc_”+strFilename,CFile::modeWrite | CFile::modeCreate)){  
  20. //不能打开输出文件  
  21. return;  
  22. }  
  23. nFilesize=In.GetLength();  
  24. lpBuffer=new char[nFilesize];  
  25. if(lpBuffer==NULL){  
  26. //不能分配复制缓存  
  27. return;  
  28. }  
  29. CFileStatus rStatus;  
  30. In.GetStatus(%%1,rStatus);  
  31. try{  
  32. In.Read(cbBuffer,nFilesize);  
  33. }  
  34. catch(CFileException *e){  
  35. char *lpMsgBuf;  
  36. if(FormatMessage(  
  37. FORMAT_MESSAGE_ALLOCATE_BUFFER |  
  38. FORMAT_MESSAGE_FROM_SYSTEM,  
  39. NULL,e->m_lOsError,  
  40. MAKELANGID(LANG_NEUTRAL,  
  41. SUBLANG_DEFAULT),  
  42. (LPSTR)&lpMsgBuf,0,NULL)>0){  
  43. AfxMessageBox(lpMsgBuf);  
  44. LocalFree(lpMsgBuf);  
  45. }  
  46. e->Delete();  
  47. return;  
  48. }  
  49. for(int i=0;i<nFilesize;i++)  
  50. {  
  51. int ibt=lpBuffer[i];  
  52. ibt+=100;  
  53. ibt%=256;  
  54. bpBuffer[i]=(char)ibt;  
  55. }  
  56. try{  
  57. Out.Write(cbBuffer,nFilesize);  
  58. }  
  59. catch(CFileException *e){  
  60. char *lpMsgBuf;  
  61. if(FormatMessage(  
  62. FORMAT_MESSAGE_ALLOCATE_BUFFER |  
  63. FORMAT_MESSAGE_FROM_SYSTEM,  
  64. NULL,e->m_lOsError,  
  65. MAKELANGID(LANG_NEUTRAL,  
  66. SUBLANG_DEFAULT),  
  67. (LPSTR)&lpMsgBuf,0,NULL)>0){  
  68. AfxMessageBox(lpMsgBuf);  
  69. LocalFree(lpMsgBuf);  
  70. }  
  71. e->Delete();  
  72. return;  
  73. }  
  74. Out.Close();  
  75. //In.Close();  
  76. CFile::SetStatus(%%2+”\\enc_”+strFilename,rStatus);  
  77. delete[] lpBuffer;  
  78. }  
//#include <string>
using std::string;
string s(%%1);
char sep='/';




#ifdef _WIN32 sep='\\'; #endif size_t sz=s.rfind(sep,s.length()); if(sz!=string::npos) { CFile Out,In; int nFIlesize; char *lpMsgBuf; CString strFilename(s.substr(i+1,s.length()-i)); if(!in.Open(%%1,CFile::modeRead)){ //不能打开输入文件 return; } if(!Out.Open(%%2+"\\enc_"+strFilename,CFile::modeWrite | CFile::modeCreate)){ //不能打开输出文件 return; } nFilesize=In.GetLength(); lpBuffer=new char[nFilesize]; if(lpBuffer==NULL){ //不能分配复制缓存 return; } CFileStatus rStatus; In.GetStatus(%%1,rStatus); try{ In.Read(cbBuffer,nFilesize); } catch(CFileException *e){ char *lpMsgBuf; if(FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL,e->m_lOsError, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&lpMsgBuf,0,NULL)>0){ AfxMessageBox(lpMsgBuf); LocalFree(lpMsgBuf); } e->Delete(); return; } for(int i=0;i<nFilesize;i++) { int ibt=lpBuffer[i]; ibt+=100; ibt%=256; bpBuffer[i]=(char)ibt; } try{ Out.Write(cbBuffer,nFilesize); } catch(CFileException *e){ char *lpMsgBuf; if(FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL,e->m_lOsError, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&lpMsgBuf,0,NULL)>0){ AfxMessageBox(lpMsgBuf); LocalFree(lpMsgBuf); } e->Delete(); return; } Out.Close(); //In.Close(); CFile::SetStatus(%%2+"\\enc_"+strFilename,rStatus); delete[] lpBuffer; } 

32.文件简单解密

  1. //#include <string>  
  2. using std::string;  
  3. string s(%%1);  
  4. char sep=‘/’;  
  5. #ifdef _WIN32  
  6. sep=’\\’;  
  7. #endif  
  8. size_t sz=s.rfind(sep,s.length());  
  9. if(sz!=string::npos)  
  10. {  
  11. CFile Out,In;  
  12. int nFIlesize;  
  13. char *lpMsgBuf;  
  14. CString strFilename(s.substr(i+1,s.length()-i));  
  15. if(!in.Open(%%2+“\\enc_”+strFilename,CFile::modeRead)){  
  16. //不能打开输入文件  
  17. return;  
  18. }  
  19. if(!Out.Open(%%1,CFile::modeWrite | CFile::modeCreate)){  
  20. //不能打开输出文件  
  21. return;  
  22. }  
  23. nFilesize=In.GetLength();  
  24. lpBuffer=new char[nFilesize];  
  25. if(lpBuffer==NULL){  
  26. //不能分配复制缓存  
  27. return;  
  28. }  
  29. CFileStatus rStatus;  
  30. In.GetStatus(%%2+”\\enc_”+strFilename,rStatus);  
  31. try{  
  32. In.Read(cbBuffer,nFilesize);  
  33. }  
  34. catch(CFileException *e){  
  35. char *lpMsgBuf;  
  36. if(FormatMessage(  
  37. FORMAT_MESSAGE_ALLOCATE_BUFFER |  
  38. FORMAT_MESSAGE_FROM_SYSTEM,  
  39. NULL,e->m_lOsError,  
  40. MAKELANGID(LANG_NEUTRAL,  
  41. SUBLANG_DEFAULT),  
  42. (LPSTR)&lpMsgBuf,0,NULL)>0){  
  43. AfxMessageBox(lpMsgBuf);  
  44. LocalFree(lpMsgBuf);  
  45. }  
  46. e->Delete();  
  47. return;  
  48. }  
  49. for(int i=0;i<nFilesize;i++)  
  50. {  
  51. int ibt=lpBuffer[i];  
  52. ibt-=100;ibt+=256;  
  53. ibt%=256;  
  54. bpBuffer[i]=(char)ibt;  
  55. }  
  56. try{  
  57. Out.Write(cbBuffer,nFilesize);  
  58. }  
  59. catch(CFileException *e){  
  60. char *lpMsgBuf;  
  61. if(FormatMessage(  
  62. FORMAT_MESSAGE_ALLOCATE_BUFFER |  
  63. FORMAT_MESSAGE_FROM_SYSTEM,  
  64. NULL,e->m_lOsError,  
  65. MAKELANGID(LANG_NEUTRAL,  
  66. SUBLANG_DEFAULT),  
  67. (LPSTR)&lpMsgBuf,0,NULL)>0){  
  68. AfxMessageBox(lpMsgBuf);  
  69. LocalFree(lpMsgBuf);  
  70. }  
  71. e->Delete();  
  72. return;  
  73. }  
  74. Out.Close();  
  75. //In.Close();  
  76. CFile::SetStatus(%%1,rStatus);  
  77. delete[] lpBuffer;  
  78. }  
//#include <string>
using std::string;
string s(%%1);
char sep='/';




#ifdef _WIN32 sep='\\'; #endif size_t sz=s.rfind(sep,s.length()); if(sz!=string::npos) { CFile Out,In; int nFIlesize; char *lpMsgBuf; CString strFilename(s.substr(i+1,s.length()-i)); if(!in.Open(%%2+"\\enc_"+strFilename,CFile::modeRead)){ //不能打开输入文件 return; } if(!Out.Open(%%1,CFile::modeWrite | CFile::modeCreate)){ //不能打开输出文件 return; } nFilesize=In.GetLength(); lpBuffer=new char[nFilesize]; if(lpBuffer==NULL){ //不能分配复制缓存 return; } CFileStatus rStatus; In.GetStatus(%%2+"\\enc_"+strFilename,rStatus); try{ In.Read(cbBuffer,nFilesize); } catch(CFileException *e){ char *lpMsgBuf; if(FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL,e->m_lOsError, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&lpMsgBuf,0,NULL)>0){ AfxMessageBox(lpMsgBuf); LocalFree(lpMsgBuf); } e->Delete(); return; } for(int i=0;i<nFilesize;i++) { int ibt=lpBuffer[i]; ibt-=100;ibt+=256; ibt%=256; bpBuffer[i]=(char)ibt; } try{ Out.Write(cbBuffer,nFilesize); } catch(CFileException *e){ char *lpMsgBuf; if(FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL,e->m_lOsError, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&lpMsgBuf,0,NULL)>0){ AfxMessageBox(lpMsgBuf); LocalFree(lpMsgBuf); } e->Delete(); return; } Out.Close(); //In.Close(); CFile::SetStatus(%%1,rStatus); delete[] lpBuffer; }

33.读取ini文件属性

  1. CString UGetProfileString(CString lpszSection,CString lpszEntry,CString def_str )  
  2. {  
  3.     CStdioFile inifile(”Setup.ini”,CFile::modeRead);  
  4.     path = inifile.GetFilePath();  
  5.     inifile.Close();  
  6.     char key[600];  
  7.     DWORD bytes = GetPrivateProfileString(lpszSection,lpszEntry,def_str,key,590,path);  
  8.     if(bytes < 600)  
  9.       key[bytes] = ’\0’;  
  10.     sIp = key;  
  11.     return sIp;  
  12. }  
CString UGetProfileString(CString lpszSection,CString lpszEntry,CString def_str )
{
    CStdioFile inifile("Setup.ini",CFile::modeRead);
    path = inifile.GetFilePath();
    inifile.Close();
    char key[600];
    DWORD bytes = GetPrivateProfileString(lpszSection,lpszEntry,def_str,key,590,path);
    if(bytes < 600)
      key[bytes] = '\0';
    sIp = key;
    return sIp;
}

34.合并一个文件下所有的文件

  1. CString Directory=%%1+“\\”+%%3;  
  2. CFileFind FFile;  
  3. CFile Out;  
  4. if(Out.Open(%%2,CFile::modeWrite|CFile::modeCreate)){  
  5. BOOL bFound=FFile.FindFile(Directory);  
  6. while(bFound)  
  7. {  
  8. bFound=FFile.FileNextFile();  
  9. if(!FFile.IsDirectory() && !FFile.IsDots())  
  10. {  
  11. CString Filename=FFile.GetFileName();  
  12. CFile In;  
  13. if(In.Open(Filename,CFile::modeRead)){  
  14. char cbBuffer[4096];  
  15. int nFIlesize=In.GetLength();  
  16. while(nFIlesize>0){  
  17. {  
  18. int nSize=sizeof(cbBuffer);  
  19. if(nSize>nFilesize)  
  20. nSize=nFilesize;  
  21. try {  
  22. In.Read(cbBuffer,nSize);  
  23. }  
  24. catch(CFileException *e){  
  25. char *lpMsgBuf;  
  26. if(FormatMessage(  
  27. FORMAT_MESSAGE_ALLOCATE_BUFFER |  
  28. FORMAT_MESSAGE_FROM_SYSTEM,  
  29. NULL,e->m_lOsError,  
  30. MAKELANGID(LANG_NEUTRAL,  
  31. SUBLANG_DEFAULT),  
  32. (LPSTR)&lpMsgBuf,0,NULL)>0){  
  33. AfxMessageBox(lpMsgBuf);  
  34. LocalFree(lpMsgBuf);  
  35. }  
  36. e->Delete();  
  37. return;  
  38. }  
  39. try {  
  40. Out.Write(cbBuffer,nSize);  
  41. }  
  42. catch(CFileException *e){  
  43. char *lpMsgBuf;  
  44. if(FormatMessage(  
  45. FORMAT_MESSAGE_ALLOCATE_BUFFER |  
  46. FORMAT_MESSAGE_FROM_SYSTEM,  
  47. NULL,e->m_lOsError,  
  48. MAKELANGID(LANG_NEUTRAL,  
  49. SUBLANG_DEFAULT),  
  50. (LPSTR)&lpMsgBuf,0,NULL)>0){  
  51. AfxMessageBox(lpMsgBuf);  
  52. LocalFree(lpMsgBuf);  
  53. }  
  54. e->Delete();  
  55. return;  
  56. }  
  57. nFilesize=nSize;  
  58. }  
  59. }  
  60. else  
  61. AfxMessageBox(”不能打开”+Filename);  
  62. }  
  63. }  
  64. }  
  65. else  
  66. AfxMessageBox(”不能创建输出文件”);  
CString Directory=%%1+"\\"+%%3;
CFileFind FFile;
CFile Out;
if(Out.Open(%%2,CFile::modeWrite|CFile::modeCreate)){
BOOL bFound=FFile.FindFile(Directory);
while(bFound)
{
bFound=FFile.FileNextFile();
if(!FFile.IsDirectory() && !FFile.IsDots())
{
CString Filename=FFile.GetFileName();
CFile In;
if(In.Open(Filename,CFile::modeRead)){
char cbBuffer[4096];
int nFIlesize=In.GetLength();
while(nFIlesize>0){
{
int nSize=sizeof(cbBuffer);
if(nSize>nFilesize)
nSize=nFilesize;
try {
In.Read(cbBuffer,nSize);
}
catch(CFileException *e){
char *lpMsgBuf;
if(FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM,
NULL,e->m_lOsError,
MAKELANGID(LANG_NEUTRAL,
SUBLANG_DEFAULT),
(LPSTR)&lpMsgBuf,0,NULL)>0){
AfxMessageBox(lpMsgBuf);
LocalFree(lpMsgBuf);
}
e->Delete();
return;
}
try {
Out.Write(cbBuffer,nSize);
}
catch(CFileException *e){
char *lpMsgBuf;
if(FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM,
NULL,e->m_lOsError,
MAKELANGID(LANG_NEUTRAL,
SUBLANG_DEFAULT),
(LPSTR)&lpMsgBuf,0,NULL)>0){
AfxMessageBox(lpMsgBuf);
LocalFree(lpMsgBuf);
}
e->Delete();
return;
}
nFilesize=nSize;
}
}
else
AfxMessageBox("不能打开"+Filename);
}
}
}
else
AfxMessageBox("不能创建输出文件");

35.写入ini文件属性

  1. int UGetProfileInt(CString lpszSection,CString lpszEntry,int defaultint )  
  2. {  
  3.     CStdioFile inifile(”Setup.INI”,CFile::modeRead);  
  4. CString path;  
  5.     path = inifile.GetFilePath();  
  6.     inifile.Close();  
  7.     int bytes = 0;  
  8. bytes = GetPrivateProfileInt(lpszSection,lpszEntry,defaultint,path);  
  9.     return bytes;  
  10. }  
int UGetProfileInt(CString lpszSection,CString lpszEntry,int defaultint )
{
    CStdioFile inifile("Setup.INI",CFile::modeRead);
CString path;
    path = inifile.GetFilePath();
    inifile.Close();
    int bytes = 0;
bytes = GetPrivateProfileInt(lpszSection,lpszEntry,defaultint,path);
    return bytes;
}

36.获得当前路径

  1. TCHAR szDir[MAX_PATH];  
  2. GetCurrentDirectory(szDir,MAX_PATH];  
  3. CString %%1(szDir);  
TCHAR szDir[MAX_PATH];
GetCurrentDirectory(szDir,MAX_PATH];
CString %%1(szDir);

37.读取XML数据库

  1. //#include <string>  
  2. //using namespace std;  
  3. char sRead[5192];  
  4. CFile mFile(_T(%%1),CFile::modeRead);  
  5. mFile.Read(sRead,5192);  
  6. if(sRead!=null)  
  7. {  
  8. string tmp;  
  9. while(sRead!=null)  
  10. {  
  11. tmp.append(sRead);  
  12. mFile.Read(sRead,5192);  
  13. }  
  14. //%%2=”Logs” //%%4=”ID” //%%6=”Content”  
  15. //%%3=”Log” //%%5=”Time”  
  16. //%%7 code %%8 time %%9 content  
  17. string target(%%7),globalTag(”<”+%%2+“>”);  
  18. string propTag1(”<”+%%5+“>”,endTag1(“</”+%%5+“>”);  
  19. string propTag2(”<”+%%6+“>”,endTag1(“</”+%%6+“>”);  
  20. int offset=tmp.find_first_of(globalTag);  
  21. while(offset)  
  22. {  
  23. offset=tmp.find_first_of(globalTag);  
  24. string description;  
  25. tmp.copy(description.begin(),tmp.find_first_of(”\”“,offset+1)-offset);  
  26. if(target.compare(description)==0)  
  27. {  
  28. string prop,prop2;  
  29. offset=tmp.find_first_of(propTag1,offset)+strlen(%%5)+2;  
  30. tmp.copy(prop.begin(),tmp.find_first_of(endTag1,offset)-offset,offset);  
  31. offset=tmp.find_first_of(propTag2,offset)+strlen(%%6)+2;  
  32. tmp.copy(prop2.begin(),tmp.find_first_of(endTag2,offset)-offset,offset);  
  33. CString %%8(prop),%%9(prop2);  
  34. %%10  
  35. return 0;  
  36. }  
  37. }  
  38. }  
  39. else  
  40. return -1;  
//#include <string>
//using namespace std;
char sRead[5192];
CFile mFile(_T(%%1),CFile::modeRead);
mFile.Read(sRead,5192);
if(sRead!=null)
{
string tmp;
while(sRead!=null)
{
tmp.append(sRead);
mFile.Read(sRead,5192);
}
//%%2="Logs" //%%4="ID" //%%6="Content"
//%%3="Log" //%%5="Time"
//%%7 code %%8 time %%9 content
string target(%%7),globalTag("<"+%%2+">");
string propTag1("<"+%%5+">",endTag1("</"+%%5+">");
string propTag2("<"+%%6+">",endTag1("</"+%%6+">");
int offset=tmp.find_first_of(globalTag);
while(offset)
{
offset=tmp.find_first_of(globalTag);
string description;
tmp.copy(description.begin(),tmp.find_first_of("\"",offset+1)-offset);
if(target.compare(description)==0)
{
string prop,prop2;
offset=tmp.find_first_of(propTag1,offset)+strlen(%%5)+2;
tmp.copy(prop.begin(),tmp.find_first_of(endTag1,offset)-offset,offset);
offset=tmp.find_first_of(propTag2,offset)+strlen(%%6)+2;
tmp.copy(prop2.begin(),tmp.find_first_of(endTag2,offset)-offset,offset);
CString %%8(prop),%%9(prop2);
%%10
return 0;
}
}
}
else
return -1;

38.写入XML数据库

  1. //#include <string>  
  2. //using namespace std;  
  3. char sRead[5192];  
  4. string description;  
  5. CFile mFile(_T(%%1),CFile::modeRead);  
  6. mFile.Read(sRead,5192);  
  7. int no;  
  8. if(sRead!=null)  
  9. {  
  10. string tmp;  
  11. while(sRead!=null)  
  12. {  
  13. tmp.append(sRead);  
  14. mFile.Read(sRead,5192);  
  15. }  
  16. //%%2=”Logs” //%%4=”ID” //%%6=”Content”  
  17. //%%3=”Log” //%%5=”Time”  
  18. //%%7 code %%8 time %%9 content  
  19. int offset=tmp.find_last_of(“<”+%%3+“ ”+%%4)+strlen(%%3)+strlen(%%4)+4;  
  20. tmp.copy(description.begin(),tmp.find_last_of(”\”><”+%%5)-offset,offset);  
  21. bo=atoi(description.c_str())+1;  
  22. mFile.Close();  
  23. tmp.insert(tmp.find_last_of(”</”+%%2+“>”),“<”+%%3+“ ”+%%4+  
  24. ”=\”“+itoa(no)+“\”><”+%%5+“>”+%%8+“</”+%%5+“><”+%%6+“>”+  
  25. %%9+”</”+%%6+“>”);  
  26. CFile file(_T(%%1),CFile::modeWrite);  
  27. file.Write(tmp.c_str()):  
  28. file.Flush();  
  29. file.Close();  
  30. }  
  31. else  
  32. {  
  33. CFile file(_T(%%1),CFile::modeWrite|CFile::modeCreate);  
  34. file.Write(”<?xml version=\”1.0\” encoding=\”gb2312\”?><”+  
  35. %%2+”><”+%%3+“ ”+%%4+“=\”0\”><”+%%5+“>”+%%8+“</”+%%5+“><”+  
  36. %%6+”>”+%%9+“</”+%%6+“></”+%%3+“></”+%%2+“>”);  
  37. file.Flush();  
  38. file.Close();  
  39. }  
//#include <string>
//using namespace std;
char sRead[5192];
string description;
CFile mFile(_T(%%1),CFile::modeRead);
mFile.Read(sRead,5192);
int no;
if(sRead!=null)
{
string tmp;
while(sRead!=null)
{
tmp.append(sRead);
mFile.Read(sRead,5192);
}
//%%2="Logs" //%%4="ID" //%%6="Content"
//%%3="Log" //%%5="Time"
//%%7 code %%8 time %%9 content
int offset=tmp.find_last_of("<"+%%3+" "+%%4)+strlen(%%3)+strlen(%%4)+4;
tmp.copy(description.begin(),tmp.find_last_of("\"><"+%%5)-offset,offset);
bo=atoi(description.c_str())+1;
mFile.Close();
tmp.insert(tmp.find_last_of("</"+%%2+">"),"<"+%%3+" "+%%4+
"=\""+itoa(no)+"\"><"+%%5+">"+%%8+"</"+%%5+"><"+%%6+">"+
%%9+"</"+%%6+">");
CFile file(_T(%%1),CFile::modeWrite);
file.Write(tmp.c_str()):
file.Flush();
file.Close();
}
else
{
CFile file(_T(%%1),CFile::modeWrite|CFile::modeCreate);
file.Write("<?xml version=\"1.0\" encoding=\"gb2312\"?><"+
%%2+"><"+%%3+" "+%%4+"=\"0\"><"+%%5+">"+%%8+"</"+%%5+"><"+
%%6+">"+%%9+"</"+%%6+"></"+%%3+"></"+%%2+">");
file.Flush();
file.Close();
}

39.ZIP压缩文件

  1. //www.zlib.net  
  2. /* 
  3. #ifdef _DEBUG 
  4. #pragma comment(lib,”zlibd.lib”) 
  5. #else 
  6. #pragma comment(lib,”zlib.lib”) 
  7. #endif 
  8. #include “zlib.h” 
  9. #include “zconf.h” 
  10. */  
  11.  HANDLE hFile, hFileToWrite;  
  12.  CString strFilePath;  
  13.  m_ctrEdit.GetWindowText(strFilePath);  
  14.    
  15.  //打开要进行压缩的文件  
  16.  hFile = CreateFile(strFilePath, // file name  
  17.   GENERIC_READ, // open for reading  
  18.   FILE_SHARE_READ, // share for reading  
  19.   NULL, // no security  
  20.   OPEN_EXISTING, // existing file only  
  21.   FILE_ATTRIBUTE_NORMAL, // normal file  
  22.   NULL); // no attr. template  
  23.    
  24.  if (hFile == INVALID_HANDLE_VALUE)  
  25.  {  
  26.   AfxMessageBox(”Could not open file to read”); // process   
  27.   
  28.   
  29. error  
  30.   return;  
  31.  }  
  32.    
  33.  HANDLE hMapFile, hMapFileToWrite;  
  34.    
  35.  //创建一个文件映射  
  36.  hMapFile = CreateFileMapping(hFile, // Current file handle.  
  37.   NULL, // Default security.  
  38.   PAGE_READONLY, // Read/write permission.  
  39.   0, // Max. object size.  
  40.   0, // Size of hFile.  
  41.   ”ZipTestMappingObjectForRead”); // Name of mapping object.  
  42.    
  43.  if (hMapFile == NULL)  
  44.  {  
  45.   AfxMessageBox(”Could not create file mapping object”);  
  46.   return;  
  47.  }  
  48.    
  49.  LPVOID lpMapAddress, lpMapAddressToWrite;  
  50.    
  51.  //创建一个文件映射的视图用来作为source  
  52.  lpMapAddress = MapViewOfFile(hMapFile, // Handle to mapping object.  
  53.   FILE_MAP_READ, // Read/write permission  
  54.   0, // Max. object size.  
  55.   0, // Size of hFile.  
  56.   0); // Map entire file.  
  57.    
  58.  if (lpMapAddress == NULL)  
  59.  {  
  60.   AfxMessageBox(”Could not map view of file”);  
  61.   return;  
  62.  }  
  63.   
  64. //  
  65.  DWORD dwFileLength,dwFileLengthToWrite;  
  66.  dwFileLength = GetFileSize(hFile, NULL);  
  67.  m_dwSourceFileLength = dwFileLength;  
  68.  //因为压缩函数的输出缓冲必须比输入大0.1% + 12 然后一个DWORD用来保存压缩前的大小,  
  69.  // 解压缩的时候用,当然还可以保存更多的信息,这里用不到  
  70.  dwFileLengthToWrite = (double)dwFileLength*1.001 + 12 + sizeof(DWORD);   
  71.  //以下是创建一个文件,用来保存压缩后的文件  
  72.  hFileToWrite = CreateFile(”demoFile.rar”// demoFile.rar  
  73.   GENERIC_WRITE|GENERIC_READ, // open for writing  
  74.   0, // do not share  
  75.   NULL, // no security  
  76.   CREATE_ALWAYS, // overwrite existing  
  77.   FILE_ATTRIBUTE_NORMAL , // normal file  
  78.   NULL); // no attr. template   
  79.  if (hFileToWrite == INVALID_HANDLE_VALUE)  
  80.  {  
  81.   AfxMessageBox(”Could not open file to write”); // process error  
  82.   return;  
  83.  }  
  84.    
  85.  hMapFileToWrite = CreateFileMapping(hFileToWrite, // Current file handle.  
  86.   NULL, // Default security.  
  87.   PAGE_READWRITE, // Read/write permission.  
  88.   0, // Max. object size.  
  89.   dwFileLengthToWrite, // Size of hFile.  
  90.   ”ZipTestMappingObjectForWrite”); // Name of mapping object.   
  91.  if (hMapFileToWrite == NULL)  
  92.  {  
  93.   AfxMessageBox(”Could not create file mapping object for write”);  
  94.   return;  
  95.  }  
  96.    
  97.  lpMapAddressToWrite = MapViewOfFile(hMapFileToWrite, // Handle to mapping object.  
  98.   FILE_MAP_WRITE, // Read/write permission  
  99.   0, // Max. object size.  
  100.   0, // Size of hFile.  
  101.   0); // Map entire file.  
  102.    
  103.  if (lpMapAddressToWrite == NULL)  
  104.  {  
  105.   AfxMessageBox(”Could not map view of file”);  
  106.   return;  
  107.  }  
  108.    
  109.  //这里是将压缩前的大小保存在文件的第一个DWORD里面  
  110.  LPVOID pBuf = lpMapAddressToWrite;  
  111.  (*(DWORD*)pBuf) = dwFileLength;  
  112.  pBuf = (DWORD*)pBuf + 1;  
  113.   
  114. //   
  115.  //这里就是最重要的,zlib里面提供的一个方法,将源缓存的数据压缩至目的缓存  
  116.  //原形如下:  
  117.  //int compress (Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen);  
  118.  //参数destLen返回实际压缩后的文件大小。  
  119.  compress((Bytef*)pBuf,&dwFileLengthToWrite, (Bytef*)lpMapAddress, dwFileLength);  
  120. //  
  121.    
  122.  UnmapViewOfFile(lpMapAddress);  
  123.  CloseHandle(hMapFile);  
  124.  CloseHandle(hFile);  
  125.    
  126.  UnmapViewOfFile(lpMapAddressToWrite);  
  127.  CloseHandle(hMapFileToWrite);  
  128.  //这里将文件大小重新设置一下  
  129.  SetFilePointer(hFileToWrite,dwFileLengthToWrite + sizeof(DWORD) ,NULL,FILE_BEGIN);  
  130.  SetEndOfFile(hFileToWrite);  
  131.  CloseHandle(hFileToWrite);  
//www.zlib.net
/*




#ifdef _DEBUG #pragma comment(lib,"zlibd.lib") #else #pragma comment(lib,"zlib.lib") #endif #include "zlib.h" #include "zconf.h" */ HANDLE hFile, hFileToWrite; CString strFilePath; m_ctrEdit.GetWindowText(strFilePath); //打开要进行压缩的文件 hFile = CreateFile(strFilePath, // file name GENERIC_READ, // open for reading FILE_SHARE_READ, // share for reading NULL, // no security OPEN_EXISTING, // existing file only FILE_ATTRIBUTE_NORMAL, // normal file NULL); // no attr. template if (hFile == INVALID_HANDLE_VALUE) { AfxMessageBox("Could not open file to read"); // process error return; } HANDLE hMapFile, hMapFileToWrite; //创建一个文件映射 hMapFile = CreateFileMapping(hFile, // Current file handle. NULL, // Default security. PAGE_READONLY, // Read/write permission. 0, // Max. object size. 0, // Size of hFile. "ZipTestMappingObjectForRead"); // Name of mapping object. if (hMapFile == NULL) { AfxMessageBox("Could not create file mapping object"); return; } LPVOID lpMapAddress, lpMapAddressToWrite; //创建一个文件映射的视图用来作为source lpMapAddress = MapViewOfFile(hMapFile, // Handle to mapping object. FILE_MAP_READ, // Read/write permission 0, // Max. object size. 0, // Size of hFile. 0); // Map entire file. if (lpMapAddress == NULL) { AfxMessageBox("Could not map view of file"); return; } // DWORD dwFileLength,dwFileLengthToWrite; dwFileLength = GetFileSize(hFile, NULL); m_dwSourceFileLength = dwFileLength; //因为压缩函数的输出缓冲必须比输入大0.1% + 12 然后一个DWORD用来保存压缩前的大小, // 解压缩的时候用,当然还可以保存更多的信息,这里用不到 dwFileLengthToWrite = (double)dwFileLength*1.001 + 12 + sizeof(DWORD); //以下是创建一个文件,用来保存压缩后的文件 hFileToWrite = CreateFile("demoFile.rar", // demoFile.rar GENERIC_WRITE|GENERIC_READ, // open for writing 0, // do not share NULL, // no security CREATE_ALWAYS, // overwrite existing FILE_ATTRIBUTE_NORMAL , // normal file NULL); // no attr. template if (hFileToWrite == INVALID_HANDLE_VALUE) { AfxMessageBox("Could not open file to write"); // process error return; } hMapFileToWrite = CreateFileMapping(hFileToWrite, // Current file handle. NULL, // Default security. PAGE_READWRITE, // Read/write permission. 0, // Max. object size. dwFileLengthToWrite, // Size of hFile. "ZipTestMappingObjectForWrite"); // Name of mapping object. if (hMapFileToWrite == NULL) { AfxMessageBox("Could not create file mapping object for write"); return; } lpMapAddressToWrite = MapViewOfFile(hMapFileToWrite, // Handle to mapping object. FILE_MAP_WRITE, // Read/write permission 0, // Max. object size. 0, // Size of hFile. 0); // Map entire file. if (lpMapAddressToWrite == NULL) { AfxMessageBox("Could not map view of file"); return; } //这里是将压缩前的大小保存在文件的第一个DWORD里面 LPVOID pBuf = lpMapAddressToWrite; (*(DWORD*)pBuf) = dwFileLength; pBuf = (DWORD*)pBuf + 1; // //这里就是最重要的,zlib里面提供的一个方法,将源缓存的数据压缩至目的缓存 //原形如下: //int compress (Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen); //参数destLen返回实际压缩后的文件大小。 compress((Bytef*)pBuf,&dwFileLengthToWrite, (Bytef*)lpMapAddress, dwFileLength); // UnmapViewOfFile(lpMapAddress); CloseHandle(hMapFile); CloseHandle(hFile); UnmapViewOfFile(lpMapAddressToWrite); CloseHandle(hMapFileToWrite); //这里将文件大小重新设置一下 SetFilePointer(hFileToWrite,dwFileLengthToWrite + sizeof(DWORD) ,NULL,FILE_BEGIN); SetEndOfFile(hFileToWrite); CloseHandle(hFileToWrite);

40.ZIP解压缩

  1. //www.zlib.net  
  2. /* 
  3. #ifdef _DEBUG 
  4. #pragma comment(lib,”zlibd.lib”) 
  5. #else 
  6. #pragma comment(lib,”zlib.lib”) 
  7. #endif 
  8. #include “zlib.h” 
  9. #include “zconf.h” 
  10. */  
  11.  HANDLE hFile, hFileToWrite;  
  12.  CString strFilePath;  
  13.  m_ctrEdit.GetWindowText(strFilePath);  
  14.    
  15.  //打开要进行解压缩的文件  
  16.  hFile = CreateFile(strFilePath, // file name  
  17.   GENERIC_READ, // open for reading  
  18.   FILE_SHARE_READ, // share for reading  
  19.   NULL, // no security  
  20.   OPEN_EXISTING, // existing file only  
  21.   FILE_ATTRIBUTE_NORMAL, // normal file  
  22.   NULL); // no attr. template  
  23.    
  24.  if (hFile == INVALID_HANDLE_VALUE)  
  25.  {  
  26.   AfxMessageBox(”Could not open file to read”); // processerror  
  27.   return;  
  28.  }  
  29.    
  30.  HANDLE hMapFile, hMapFileToWrite;   
  31.  //创建一个文件映射  
  32.  hMapFile = CreateFileMapping(hFile, // Current file handle.  
  33.   NULL, // Default security.  
  34.   PAGE_READONLY, // Read/write permission.  
  35.   0, // Max. object size.  
  36.   0, // Size of hFile.  
  37.   ”ZipTestMappingObjectForRead”); // Name of mapping object.  
  38.    
  39.  if (hMapFile == NULL)  
  40.  {  
  41.   AfxMessageBox(”Could not create file mapping object”);  
  42.   return;  
  43.  }  
  44.    
  45.  LPVOID lpMapAddress, lpMapAddressToWrite;  
  46.    
  47.  //创建一个文件映射的视图用来作为source  
  48.  lpMapAddress = MapViewOfFile(hMapFile, // Handle to mapping object.  
  49.   FILE_MAP_READ, // Read/write permission  
  50.   0, // Max. object size.  
  51.   0, // Size of hFile.  
  52.   0); // Map entire file.  
  53.    
  54.  if (lpMapAddress == NULL)  
  55.  {  
  56.   AfxMessageBox(”Could not map view of file”);  
  57.   return;  
  58.  }  
  59.   
  60. /  
  61.  DWORD dwFileLength,dwFileLengthToWrite;  
  62.  dwFileLength = GetFileSize(hFile, NULL) - sizeof(DWORD);  
  63.  //因为压缩函数的输出缓冲必须比输入大0.1% + 12 然后一个DWORD用来保存压缩前的大小,  
  64.  // 解压缩的时候用,当然还可以保存更多的信息,这里用不到  
  65. // dwFileLengthToWrite = (double)dwFileLength*1.001 + 12 + sizeof(DWORD);  
  66.  dwFileLengthToWrite = (*(DWORD*)lpMapAddress);  
  67.   
  68.   
  69.  LPVOID pSourceBuf = lpMapAddress;  
  70.  pSourceBuf = (DWORD*)pSourceBuf + 1;  
  71.    
  72.  //以下是创建一个文件,用来保存压缩后的文件  
  73.  hFileToWrite = CreateFile(”demoFile.pdf”// create demo.gz  
  74.   GENERIC_WRITE|GENERIC_READ, // open for writing  
  75.   0, // do not share  
  76.   NULL, // no security  
  77.   CREATE_ALWAYS, // overwrite existing  
  78.   FILE_ATTRIBUTE_NORMAL , // normal file  
  79.   NULL); // no attr. template  
  80.    
  81.  if (hFileToWrite == INVALID_HANDLE_VALUE)  
  82.  {  
  83.   AfxMessageBox(”Could not open file to write”); // process error  
  84.   return;  
  85.  }  
  86.    
  87.  hMapFileToWrite = CreateFileMapping(hFileToWrite, // Current file handle.  
  88.   NULL, // Default security.  
  89.   PAGE_READWRITE, // Read/write permission.  
  90.   0, // Max. object size.  
  91.   dwFileLengthToWrite, // Size of hFile.  
  92.   ”ZipTestMappingObjectForWrite”); // Name of mapping object.  
  93.    
  94.  if (hMapFileToWrite == NULL)  
  95.  {  
  96.   AfxMessageBox(”Could not create file mapping object for write”);  
  97.   return;  
  98.  }  
  99.    
  100.  lpMapAddressToWrite = MapViewOfFile(hMapFileToWrite, // Handle to mapping object.  
  101.   FILE_MAP_WRITE, // Read/write permission  
  102.   0, // Max. object size.  
  103.   0, // Size of hFile.  
  104.   0); // Map entire file.  
  105.    
  106.  if (lpMapAddressToWrite == NULL)  
  107.  {  
  108.   AfxMessageBox(”Could not map view of file”);  
  109.   return;  
  110.  }  
  111.    
  112.  //这里是将压缩前的大小保存在文件的第一个DWORD里面  
  113.  LPVOID pBuf = lpMapAddressToWrite;  
  114.   
  115. //   
  116.  //这里就是最重要的,zlib里面提供的一个方法,将源缓存的数据压缩至目的缓存  
  117.  //原形如下:  
  118.  //int compress (Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen);  
  119.  //参数destLen返回实际压缩后的文件大小。  
  120.  uncompress((Bytef*)pBuf,&dwFileLengthToWrite, (Bytef*)pSourceBuf, dwFileLength);  
  121. //  
  122.    
  123.  UnmapViewOfFile(lpMapAddress);  
  124.  CloseHandle(hMapFile);  
  125.  CloseHandle(hFile);  
  126.    
  127.  UnmapViewOfFile(lpMapAddressToWrite);  
  128.  CloseHandle(hMapFileToWrite);  
  129.  //这里将文件大小重新设置一下  
  130.  SetFilePointer(hFileToWrite,dwFileLengthToWrite ,NULL,FILE_BEGIN);  
  131.  SetEndOfFile(hFileToWrite);  
  132.  CloseHandle(hFileToWrite);  
//www.zlib.net
/*




#ifdef _DEBUG #pragma comment(lib,"zlibd.lib") #else #pragma comment(lib,"zlib.lib") #endif #include "zlib.h" #include "zconf.h" */ HANDLE hFile, hFileToWrite; CString strFilePath; m_ctrEdit.GetWindowText(strFilePath); //打开要进行解压缩的文件 hFile = CreateFile(strFilePath, // file name GENERIC_READ, // open for reading FILE_SHARE_READ, // share for reading NULL, // no security OPEN_EXISTING, // existing file only FILE_ATTRIBUTE_NORMAL, // normal file NULL); // no attr. template if (hFile == INVALID_HANDLE_VALUE) { AfxMessageBox("Could not open file to read"); // processerror return; } HANDLE hMapFile, hMapFileToWrite; //创建一个文件映射 hMapFile = CreateFileMapping(hFile, // Current file handle. NULL, // Default security. PAGE_READONLY, // Read/write permission. 0, // Max. object size. 0, // Size of hFile. "ZipTestMappingObjectForRead"); // Name of mapping object. if (hMapFile == NULL) { AfxMessageBox("Could not create file mapping object"); return; } LPVOID lpMapAddress, lpMapAddressToWrite; //创建一个文件映射的视图用来作为source lpMapAddress = MapViewOfFile(hMapFile, // Handle to mapping object. FILE_MAP_READ, // Read/write permission 0, // Max. object size. 0, // Size of hFile. 0); // Map entire file. if (lpMapAddress == NULL) { AfxMessageBox("Could not map view of file"); return; } / DWORD dwFileLength,dwFileLengthToWrite; dwFileLength = GetFileSize(hFile, NULL) - sizeof(DWORD); //因为压缩函数的输出缓冲必须比输入大0.1% + 12 然后一个DWORD用来保存压缩前的大小, // 解压缩的时候用,当然还可以保存更多的信息,这里用不到 // dwFileLengthToWrite = (double)dwFileLength*1.001 + 12 + sizeof(DWORD); dwFileLengthToWrite = (*(DWORD*)lpMapAddress); LPVOID pSourceBuf = lpMapAddress; pSourceBuf = (DWORD*)pSourceBuf + 1; //以下是创建一个文件,用来保存压缩后的文件 hFileToWrite = CreateFile("demoFile.pdf", // create demo.gz GENERIC_WRITE|GENERIC_READ, // open for writing 0, // do not share NULL, // no security CREATE_ALWAYS, // overwrite existing FILE_ATTRIBUTE_NORMAL , // normal file NULL); // no attr. template if (hFileToWrite == INVALID_HANDLE_VALUE) { AfxMessageBox("Could not open file to write"); // process error return; } hMapFileToWrite = CreateFileMapping(hFileToWrite, // Current file handle. NULL, // Default security. PAGE_READWRITE, // Read/write permission. 0, // Max. object size. dwFileLengthToWrite, // Size of hFile. "ZipTestMappingObjectForWrite"); // Name of mapping object. if (hMapFileToWrite == NULL) { AfxMessageBox("Could not create file mapping object for write"); return; } lpMapAddressToWrite = MapViewOfFile(hMapFileToWrite, // Handle to mapping object. FILE_MAP_WRITE, // Read/write permission 0, // Max. object size. 0, // Size of hFile. 0); // Map entire file. if (lpMapAddressToWrite == NULL) { AfxMessageBox("Could not map view of file"); return; } //这里是将压缩前的大小保存在文件的第一个DWORD里面 LPVOID pBuf = lpMapAddressToWrite; // //这里就是最重要的,zlib里面提供的一个方法,将源缓存的数据压缩至目的缓存 //原形如下: //int compress (Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen); //参数destLen返回实际压缩后的文件大小。 uncompress((Bytef*)pBuf,&dwFileLengthToWrite, (Bytef*)pSourceBuf, dwFileLength); // UnmapViewOfFile(lpMapAddress); CloseHandle(hMapFile); CloseHandle(hFile); UnmapViewOfFile(lpMapAddressToWrite); CloseHandle(hMapFileToWrite); //这里将文件大小重新设置一下 SetFilePointer(hFileToWrite,dwFileLengthToWrite ,NULL,FILE_BEGIN); SetEndOfFile(hFileToWrite); CloseHandle(hFileToWrite);

41.ZIP压缩文件夹

  1. //www.zlib.net  
  2. /* 
  3. #include <stdio.h> 
  4. #include <string.h> 
  5. #include <assert.h> 
  6. #include <dos.h> 
  7. #include <direct.h> 
  8. #include <zlib.h> 
  9.  
  10. #if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(__CYGWIN__) 
  11. #  include <fcntl.h> 
  12. #  include <io.h> 
  13. #  define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY) 
  14. #else 
  15. #  define SET_BINARY_MODE(file) 
  16. #endif 
  17.  
  18. #define CHUNK 16384 
  19. //#define USE_TAG 
  20. #ifdef USE_TAG 
  21. #define COMPRESS_FILE_TAG_HEAD ”<<<” 
  22. #define COMPRESS_FILE_TAG_TAIL ”>>>” 
  23. #define COMPRESS_FILE_TAG_END_LEN 3    // must be strlen(COMPRESS_FILE_TAG_HEAD) = strlen(COMPRESS_FILE_TAG_TAIL) 
  24. #else 
  25. #define COMPRESS_FILE_TAG_HEAD ”“ 
  26. #define COMPRESS_FILE_TAG_TAIL ”“ 
  27. #define COMPRESS_FILE_TAG_END_LEN 0    // must be strlen(COMPRESS_FILE_TAG_HEAD) = strlen(COMPRESS_FILE_TAG_TAIL) 
  28. #endif 
  29. */  
  30. /**//**//**//* Compress from file source to file dest until EOF on source. 
  31.    def() returns Z_OK on success, Z_MEM_ERROR if memory could not be 
  32.    allocated for processing, Z_STREAM_ERROR if an invalid compression 
  33.    level is supplied, Z_VERSION_ERROR if the version of zlib.h and the 
  34.    version of the library linked do not match, or Z_ERRNO if there is 
  35.    an error reading or writing the files. */  
  36. static int def(FILE *source, FILE *dest, int level)  
  37. {  
  38.     int ret, flush;  
  39.     unsigned have;  
  40.     z_stream strm;  
  41.     unsigned char in[CHUNK];  
  42.     unsigned char out[CHUNK];  
  43.     /**//**//**//* allocate deflate state */  
  44.     strm.zalloc = Z_NULL;  
  45.     strm.zfree = Z_NULL;  
  46.     strm.opaque = Z_NULL;  
  47.     ret = deflateInit(&strm, level);  
  48.     if (ret != Z_OK)  
  49.         return ret;  
  50.     /**//**//**//* compress until end of file */  
  51.     do {  
  52.         strm.avail_in = fread(in, 1, CHUNK, source);  
  53.         if (ferror(source)) {  
  54.             (void)deflateEnd(&strm);  
  55.             return Z_ERRNO;  
  56.         }  
  57.         flush = feof(source) ? Z_FINISH : Z_NO_FLUSH;  
  58.         strm.next_in = in;  
  59.         /**//**//**//* run deflate() on input until output buffer not full, finish 
  60.            compression if all of source has been read in */  
  61.         do {  
  62.             strm.avail_out = CHUNK;  
  63.             strm.next_out = out;  
  64.             ret = deflate(&strm, flush);    /**//**//**//* no bad return value */  
  65.             assert(ret != Z_STREAM_ERROR);  /**//**//**//* state not clobbered */  
  66.             have = CHUNK - strm.avail_out;  
  67.             if (fwrite(out, 1, have, dest) != have || ferror(dest)) {  
  68.                 (void)deflateEnd(&strm);  
  69.                 return Z_ERRNO;  
  70.             }  
  71.         } while (strm.avail_out == 0);  
  72.         assert(strm.avail_in == 0);     /**//**//**//* all input will be used */  
  73.         /**//**//**//* done when last data in file processed */  
  74.     } while (flush != Z_FINISH);  
  75.     assert(ret == Z_STREAM_END);        /**//**//**//* stream will be complete */  
  76.     /**//**//**//* clean up and return */  
  77.     (void)deflateEnd(&strm);  
  78.     return Z_OK;  
  79. }  
  80.   
  81. /**//**//**//* Decompress from file source to file dest until stream ends or EOF. 
  82.    inf() returns Z_OK on success, Z_MEM_ERROR if memory could not be 
  83.    allocated for processing, Z_DATA_ERROR if the deflate data is 
  84.    invalid or incomplete, Z_VERSION_ERROR if the version of zlib.h and 
  85.    the version of the library linked do not match, or Z_ERRNO if there 
  86.    is an error reading or writing the files. */  
  87. static int inf(FILE *source, FILE *dest)  
  88. {  
  89.     int ret;  
  90.     unsigned have;  
  91.     z_stream strm;  
  92.     unsigned char in[CHUNK];  
  93.     unsigned char out[CHUNK];  
  94.   
  95.     /**//**//**//* allocate inflate state */  
  96.     strm.zalloc = Z_NULL;  
  97.     strm.zfree = Z_NULL;  
  98.     strm.opaque = Z_NULL;  
  99.     strm.avail_in = 0;  
  100.     strm.next_in = Z_NULL;  
  101.     ret = inflateInit(&strm);  
  102.     if (ret != Z_OK)  
  103.         return ret;  
  104.     /**//**//**//* decompress until deflate stream ends or end of file */  
  105.     do {  
  106.         strm.avail_in = fread(in, 1, CHUNK, source);  
  107.         if (ferror(source)) {  
  108.             (void)inflateEnd(&strm);  
  109.             return Z_ERRNO;  
  110.         }  
  111.         if (strm.avail_in == 0)  
  112.             break;  
  113.         strm.next_in = in;  
  114.   
  115.         /**//**//**//* run inflate() on input until output buffer not full */  
  116.         do {  
  117.             strm.avail_out = CHUNK;  
  118.             strm.next_out = out;  
  119.             ret = inflate(&strm, Z_NO_FLUSH);  
  120.             assert(ret != Z_STREAM_ERROR);  /**//**//**//* state not clobbered */  
  121.             switch (ret) {  
  122.             case Z_NEED_DICT:  
  123.                 ret = Z_DATA_ERROR;     /**//**//**//* and fall through */  
  124.             case Z_DATA_ERROR:  
  125.             case Z_MEM_ERROR:  
  126.                 (void)inflateEnd(&strm);  
  127.                 return ret;  
  128.             }  
  129.             have = CHUNK - strm.avail_out;  
  130.             if (fwrite(out, 1, have, dest) != have || ferror(dest)) {  
  131.                 (void)inflateEnd(&strm);  
  132.                 return Z_ERRNO;  
  133.             }  
  134.         } while (strm.avail_out == 0);  
  135.   
  136.         /**//**//**//* done when inflate() says it’s done */  
  137.     } while (ret != Z_STREAM_END);  
  138.   
  139.     /**//**//**//* clean up and return */  
  140.     (void)inflateEnd(&strm);  
  141.     return ret == Z_STREAM_END ? Z_OK : Z_DATA_ERROR;  
  142. }  
  143.   
  144. /**//**//**//* report a zlib or i/o error */  
  145. static void zerr(int ret)  
  146. {  
  147.     fputs(”zpipe: ”, stderr);  
  148.     switch (ret) {  
  149.     case Z_ERRNO:  
  150.         if (ferror(stdin))  
  151.             fputs(”error reading stdin ”, stderr);  
  152.         if (ferror(stdout))  
  153.             fputs(”error writing stdout ”, stderr);  
  154.         break;  
  155.     case Z_STREAM_ERROR:  
  156.         fputs(”invalid compression level ”, stderr);  
  157.         break;  
  158.     case Z_DATA_ERROR:  
  159.         fputs(”invalid or incomplete deflate data ”, stderr);  
  160.         break;  
  161.     case Z_MEM_ERROR:  
  162.         fputs(”out of memory ”, stderr);  
  163.         break;  
  164.     case Z_VERSION_ERROR:  
  165.         fputs(”zlib version mismatch! ”, stderr);  
  166.     }  
  167. }  
  168. // 以上就是zpipe.c的几个主要函数:def()、inf()和zerr(),def()是压缩函数,主要使用了zlib的deflate()接口;inf()是压缩函数,主要使用了zlib的inflate()接口;zerr()是错误打印函数。  
  169.   
  170. static int write_zfile_file_header(const char *file,FILE *zfile)  
  171. {  
  172.     int len;  
  173.     len = strlen(file);  
  174.     if (fwrite(COMPRESS_FILE_TAG_HEAD, 1, COMPRESS_FILE_TAG_END_LEN, zfile) != COMPRESS_FILE_TAG_END_LEN || ferror(zfile))   
  175.     {  
  176.         fprintf(stderr,”When writing file or dir header to zfile: write error. ”);  
  177.         return 1;  
  178.     }  
  179.     if (fwrite(file, 1, len, zfile) != len|| ferror(zfile))   
  180.     {  
  181.         fprintf(stderr,”When writing file or dir header to zfile: write error. ”);  
  182.         return 1;  
  183.     }  
  184.     if (fwrite(COMPRESS_FILE_TAG_TAIL, 1, COMPRESS_FILE_TAG_END_LEN, zfile) != COMPRESS_FILE_TAG_END_LEN || ferror(zfile))   
  185.     {  
  186.         fprintf(stderr,”When writing file or dir header to zfile: write error. ”);  
  187.         return 1;  
  188.     }  
  189.     return 0;  
  190. }  
  191. /**//* compress or decompress from stdin to stdout */  
  192. static int compress_dir(char *file_in,FILE *fd_out)  
  193. {  
  194.     FILE *fd_in;  
  195.     struct _finddata_t find_data;  
  196.     char file[128];  
  197.     long lf;  
  198.     int ret;  
  199.     write_zfile_file_header(file_in,fd_out);  
  200.     sprintf(file,”%s%s”,file_in,“/*”);  
  201.     if((lf = _findfirst(file,&find_data))==-1l)    // LOOKOUT: not eleven, but one and lowercase ‘L’  
  202.     {  
  203.         fprintf(stdout,”file not found. ”);  
  204.     }  
  205.     else  
  206.     {  
  207.          do   
  208.          {  
  209.              if(!strcmp(find_data.name,“.”) || !strcmp(find_data.name,“..”))  
  210.                  continue;  
  211.             fprintf(stdout,”%s”,find_data.name);  
  212.             sprintf(file,”%s%s%s”,file_in,“/”,find_data.name);  
  213.             if(find_data.attrib & _A_SUBDIR)  
  214.             {  
  215.                 fprintf(stdout,” —directory— ”);  
  216.                 ret = compress_dir(file,fd_out);  
  217.             }  
  218.             else  
  219.             {  
  220.                 write_zfile_file_header(file,fd_out);  
  221.                 if(access(file, 2) != 0)    //W_OK=2  
  222.                 {  
  223.                     int attrib;  
  224.                       
  225.                     attrib = _chmod(file,0);  
  226.                     _chmod(file,1,attrib & ~_A_RDONLY);  
  227.                     fprintf(stderr,”When writing file:  No privilege to write file %s. ”,file);  
  228.                     return -1;  
  229.                 }  
  230.                 fd_in = fopen(file,”rb+”);  
  231.                    SET_BINARY_MODE(fd_in);  
  232.                 ret = def(fd_in, fd_out, Z_DEFAULT_COMPRESSION);  
  233.                 if (ret != Z_OK)  
  234.                     zerr(ret);  
  235.                 else  
  236.                     fprintf(stdout,” zip over ”);  
  237.                 fclose(fd_in);  
  238.             }  
  239.         }while( _findnext(lf, &find_data ) == 0 );  
  240.     }  
  241.     return 0;  
  242. }  
  243. int main(int argc, char **argv)  
  244. {  
  245.     struct _finddata_t find_data;  
  246.     FILE *fd_in;  
  247.     FILE *fd_out;  
  248.     const char *file_dir;  
  249.     char file_out[100];  
  250.     int ret;  
  251.       
  252.      if (argc == 2)   
  253.     {  
  254.         file_dir = argv[1];  
  255.         if(_findfirst(file_dir,&find_data)==-1l)    // LOOKOUT: not eleven, but one and lowercase ‘L’  
  256.         {  
  257.             fprintf(stderr,”File or dir %s not found. ”,file_dir);  
  258.             return 1;  
  259.         }  
  260.         if(find_data.attrib & _A_SUBDIR)  
  261.         {  
  262.             sprintf(file_out,”%s%s”,file_dir,“.z”);  
  263.             fd_out = fopen(file_out,”wb+”);  
  264.             SET_BINARY_MODE(fd_out);  
  265.               
  266.             fprintf(stdout,”Dir %s being Compressed … ”,file_dir);  
  267.             ret = compress_dir(file_dir,fd_out);  
  268.             fclose(fd_out);  
  269.         }  
  270.         else  
  271.         {  
  272.             fprintf(stdout,”File %s being Compressed … ”,file_dir);  
  273.             sprintf(file_out,”%s%s”,file_dir,“.z”);  
  274.             fd_in = fopen(file_dir,”rb+”);  
  275.             fd_out = fopen(file_out,”wb+”);  
  276.                SET_BINARY_MODE(fd_in);  
  277.             SET_BINARY_MODE(fd_out);  
  278.             ret = def(fd_in, fd_out, Z_DEFAULT_COMPRESSION);  
  279.             fclose(fd_in);  
  280.             fclose(fd_out);  
  281.         }  
  282.         if (ret != 0)  
  283.         {  
  284.             fprintf(stderr,”Compress Error !!!!!!!!!!!!!! ”);  
  285.             zerr(ret);  
  286.         }  
  287.         else  
  288.             fprintf(stdout,”Compress OK————— ”);  
  289.     }  
  290.     else {  
  291.         fprintf(stdout,”zod usage: zod [file]/[directory] ”);  
  292.     }  
  293.     getch();  
  294.   
  295.     return 0;  
  296. }  
  297.   
  298. // 以上就是主要的目录压缩代码,主要是将目录/文件的名称写入后,紧跟着压缩后的数据。  
//www.zlib.net
/*




#include <stdio.h> #include <string.h> #include <assert.h> #include <dos.h> #include <direct.h> #include <zlib.h> #if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(__CYGWIN__) # include <fcntl.h> # include <io.h> # define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY) #else # define SET_BINARY_MODE(file) #endif #define CHUNK 16384 //#define USE_TAG #ifdef USE_TAG #define COMPRESS_FILE_TAG_HEAD "<<<" #define COMPRESS_FILE_TAG_TAIL ">>>" #define COMPRESS_FILE_TAG_END_LEN 3 // must be strlen(COMPRESS_FILE_TAG_HEAD) = strlen(COMPRESS_FILE_TAG_TAIL) #else #define COMPRESS_FILE_TAG_HEAD "" #define COMPRESS_FILE_TAG_TAIL "" #define COMPRESS_FILE_TAG_END_LEN 0 // must be strlen(COMPRESS_FILE_TAG_HEAD) = strlen(COMPRESS_FILE_TAG_TAIL) #endif */ /**//**//**//* Compress from file source to file dest until EOF on source. def() returns Z_OK on success, Z_MEM_ERROR if memory could not be allocated for processing, Z_STREAM_ERROR if an invalid compression level is supplied, Z_VERSION_ERROR if the version of zlib.h and the version of the library linked do not match, or Z_ERRNO if there is an error reading or writing the files. */ static int def(FILE *source, FILE *dest, int level) { int ret, flush; unsigned have; z_stream strm; unsigned char in[CHUNK]; unsigned char out[CHUNK]; /**//**//**//* allocate deflate state */ strm.zalloc = Z_NULL; strm.zfree = Z_NULL; strm.opaque = Z_NULL; ret = deflateInit(&strm, level); if (ret != Z_OK) return ret; /**//**//**//* compress until end of file */ do { strm.avail_in = fread(in, 1, CHUNK, source); if (ferror(source)) { (void)deflateEnd(&strm); return Z_ERRNO; } flush = feof(source) ? Z_FINISH : Z_NO_FLUSH; strm.next_in = in; /**//**//**//* run deflate() on input until output buffer not full, finish compression if all of source has been read in */ do { strm.avail_out = CHUNK; strm.next_out = out; ret = deflate(&strm, flush); /**//**//**//* no bad return value */ assert(ret != Z_STREAM_ERROR); /**//**//**//* state not clobbered */ have = CHUNK - strm.avail_out; if (fwrite(out, 1, have, dest) != have || ferror(dest)) { (void)deflateEnd(&strm); return Z_ERRNO; } } while (strm.avail_out == 0); assert(strm.avail_in == 0); /**//**//**//* all input will be used */ /**//**//**//* done when last data in file processed */ } while (flush != Z_FINISH); assert(ret == Z_STREAM_END); /**//**//**//* stream will be complete */ /**//**//**//* clean up and return */ (void)deflateEnd(&strm); return Z_OK; } /**//**//**//* Decompress from file source to file dest until stream ends or EOF. inf() returns Z_OK on success, Z_MEM_ERROR if memory could not be allocated for processing, Z_DATA_ERROR if the deflate data is invalid or incomplete, Z_VERSION_ERROR if the version of zlib.h and the version of the library linked do not match, or Z_ERRNO if there is an error reading or writing the files. */ static int inf(FILE *source, FILE *dest) { int ret; unsigned have; z_stream strm; unsigned char in[CHUNK]; unsigned char out[CHUNK]; /**//**//**//* allocate inflate state */ strm.zalloc = Z_NULL; strm.zfree = Z_NULL; strm.opaque = Z_NULL; strm.avail_in = 0; strm.next_in = Z_NULL; ret = inflateInit(&strm); if (ret != Z_OK) return ret; /**//**//**//* decompress until deflate stream ends or end of file */ do { strm.avail_in = fread(in, 1, CHUNK, source); if (ferror(source)) { (void)inflateEnd(&strm); return Z_ERRNO; } if (strm.avail_in == 0) break; strm.next_in = in; /**//**//**//* run inflate() on input until output buffer not full */ do { strm.avail_out = CHUNK; strm.next_out = out; ret = inflate(&strm, Z_NO_FLUSH); assert(ret != Z_STREAM_ERROR); /**//**//**//* state not clobbered */ switch (ret) { case Z_NEED_DICT: ret = Z_DATA_ERROR; /**//**//**//* and fall through */ case Z_DATA_ERROR: case Z_MEM_ERROR: (void)inflateEnd(&strm); return ret; } have = CHUNK - strm.avail_out; if (fwrite(out, 1, have, dest) != have || ferror(dest)) { (void)inflateEnd(&strm); return Z_ERRNO; } } while (strm.avail_out == 0); /**//**//**//* done when inflate() says it's done */ } while (ret != Z_STREAM_END); /**//**//**//* clean up and return */ (void)inflateEnd(&strm); return ret == Z_STREAM_END ? Z_OK : Z_DATA_ERROR; } /**//**//**//* report a zlib or i/o error */ static void zerr(int ret) { fputs("zpipe: ", stderr); switch (ret) { case Z_ERRNO: if (ferror(stdin)) fputs("error reading stdin ", stderr); if (ferror(stdout)) fputs("error writing stdout ", stderr); break; case Z_STREAM_ERROR: fputs("invalid compression level ", stderr); break; case Z_DATA_ERROR: fputs("invalid or incomplete deflate data ", stderr); break; case Z_MEM_ERROR: fputs("out of memory ", stderr); break; case Z_VERSION_ERROR: fputs("zlib version mismatch! ", stderr); } } // 以上就是zpipe.c的几个主要函数:def()、inf()和zerr(),def()是压缩函数,主要使用了zlib的deflate()接口;inf()是压缩函数,主要使用了zlib的inflate()接口;zerr()是错误打印函数。 static int write_zfile_file_header(const char *file,FILE *zfile) { int len; len = strlen(file); if (fwrite(COMPRESS_FILE_TAG_HEAD, 1, COMPRESS_FILE_TAG_END_LEN, zfile) != COMPRESS_FILE_TAG_END_LEN || ferror(zfile)) { fprintf(stderr,"When writing file or dir header to zfile: write error. "); return 1; } if (fwrite(file, 1, len, zfile) != len|| ferror(zfile)) { fprintf(stderr,"When writing file or dir header to zfile: write error. "); return 1; } if (fwrite(COMPRESS_FILE_TAG_TAIL, 1, COMPRESS_FILE_TAG_END_LEN, zfile) != COMPRESS_FILE_TAG_END_LEN || ferror(zfile)) { fprintf(stderr,"When writing file or dir header to zfile: write error. "); return 1; } return 0; } /**//* compress or decompress from stdin to stdout */ static int compress_dir(char *file_in,FILE *fd_out) { FILE *fd_in; struct _finddata_t find_data; char file[128]; long lf; int ret; write_zfile_file_header(file_in,fd_out); sprintf(file,"%s%s",file_in,"/*"); if((lf = _findfirst(file,&find_data))==-1l) // LOOKOUT: not eleven, but one and lowercase 'L' { fprintf(stdout,"file not found. "); } else { do { if(!strcmp(find_data.name,".") || !strcmp(find_data.name,"..")) continue; fprintf(stdout,"%s",find_data.name); sprintf(file,"%s%s%s",file_in,"/",find_data.name); if(find_data.attrib & _A_SUBDIR) { fprintf(stdout," ---directory--- "); ret = compress_dir(file,fd_out); } else { write_zfile_file_header(file,fd_out); if(access(file, 2) != 0) //W_OK=2 { int attrib; attrib = _chmod(file,0); _chmod(file,1,attrib & ~_A_RDONLY); fprintf(stderr,"When writing file: No privilege to write file %s. ",file); return -1; } fd_in = fopen(file,"rb+"); SET_BINARY_MODE(fd_in); ret = def(fd_in, fd_out, Z_DEFAULT_COMPRESSION); if (ret != Z_OK) zerr(ret); else fprintf(stdout," zip over "); fclose(fd_in); } }while( _findnext(lf, &find_data ) == 0 ); } return 0; } int main(int argc, char **argv) { struct _finddata_t find_data; FILE *fd_in; FILE *fd_out; const char *file_dir; char file_out[100]; int ret; if (argc == 2) { file_dir = argv[1]; if(_findfirst(file_dir,&find_data)==-1l) // LOOKOUT: not eleven, but one and lowercase 'L' { fprintf(stderr,"File or dir %s not found. ",file_dir); return 1; } if(find_data.attrib & _A_SUBDIR) { sprintf(file_out,"%s%s",file_dir,".z"); fd_out = fopen(file_out,"wb+"); SET_BINARY_MODE(fd_out); fprintf(stdout,"Dir %s being Compressed ... ",file_dir); ret = compress_dir(file_dir,fd_out); fclose(fd_out); } else { fprintf(stdout,"File %s being Compressed ... ",file_dir); sprintf(file_out,"%s%s",file_dir,".z"); fd_in = fopen(file_dir,"rb+"); fd_out = fopen(file_out,"wb+"); SET_BINARY_MODE(fd_in); SET_BINARY_MODE(fd_out); ret = def(fd_in, fd_out, Z_DEFAULT_COMPRESSION); fclose(fd_in); fclose(fd_out); } if (ret != 0) { fprintf(stderr,"Compress Error !!!!!!!!!!!!!! "); zerr(ret); } else fprintf(stdout,"Compress OK--------------- "); } else { fprintf(stdout,"zod usage: zod [file]/[directory] "); } getch(); return 0; } // 以上就是主要的目录压缩代码,主要是将目录/文件的名称写入后,紧跟着压缩后的数据。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值