stdafx.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // stdafx.cpp : 只包括标准包含文件的源文件
  2. // test.pch 将作为预编译头
  3. // stdafx.obj 将包含预编译类型信息
  4. #include "stdafx.h"
  5. HMODULE g_hlzma = NULL;
  6. lzma_compress g_PtrCompress = NULL;
  7. lzma_uncompress g_PtrUnCompress = NULL;
  8. BOOL LoadlzmaLibarary()
  9. {
  10. if ( g_hlzma == NULL )
  11. {
  12. g_hlzma = (HMODULE)LoadLibrary(_T("Lzmalib.dll"));
  13. if (!g_hlzma)
  14. return FALSE;
  15. }
  16. g_PtrCompress = (lzma_compress)GetProcAddress(g_hlzma, "LzmaCompress");
  17. if ( !g_PtrCompress )
  18. {
  19. FreelzmaLibarary();
  20. return FALSE;
  21. }
  22. g_PtrUnCompress = (lzma_uncompress)GetProcAddress(g_hlzma, "LzmaUncompress");
  23. if ( !g_PtrUnCompress )
  24. {
  25. FreelzmaLibarary();
  26. return FALSE;
  27. }
  28. return TRUE;
  29. }
  30. void FreelzmaLibarary()
  31. {
  32. if ( g_hlzma )
  33. {
  34. if ( FreeLibrary(g_hlzma) )
  35. {
  36. g_hlzma = NULL;
  37. g_PtrCompress = NULL;
  38. g_PtrUnCompress = NULL;
  39. }
  40. }
  41. }
  42. const TCHAR g_szLzma[] = _T("#LZMA#");
  43. BOOL CompressFile(CString strCompressFile)
  44. {
  45. //////////////////////////////////////////////////////////////////////////
  46. // 压缩文件;
  47. //////////////////////////////////////////////////////////////////////////
  48. byte prop[5] = {0};
  49. size_t nPropSize = 5;
  50. ULONGLONG ulTickCount = GetTickCount64();
  51. CString strSaveFile = strCompressFile.Mid(0,strCompressFile.ReverseFind('.')) + ".lam";
  52. if ( PathFileExists(strSaveFile) )
  53. {
  54. // 已经压缩了;
  55. return TRUE;
  56. }
  57. FILE* fp = fopen(strCompressFile, "rb");
  58. if ( NULL != fp )
  59. {
  60. fseek(fp, 0, SEEK_END);
  61. size_t nSrcLen = ftell(fp);
  62. fseek(fp, 0, SEEK_SET);
  63. byte* pSrcData = (byte*)malloc(nSrcLen);
  64. fread(pSrcData, nSrcLen, 1, fp);
  65. fclose(fp);
  66. size_t nDesLen = nSrcLen;
  67. byte* pDesData = (byte*)malloc(nDesLen);
  68. #ifdef USE_LIB
  69. int nRet = LzmaCompress(pDesData, &nDesLen, pSrcData, nSrcLen, prop, &nPropSize, 9, (1<<24), 8, 0, 2, 32, 2);
  70. #else
  71. int nRet = LzmaCompress(pDesData, &nDesLen, pSrcData, nSrcLen, prop, &nPropSize, 9, (1<<24), 3, 0, 2, 32, 2);
  72. #endif
  73. if ( SZ_ERROR_OUTPUT_EOF == nRet )
  74. {//申请内存不足;
  75. free(pDesData);
  76. nDesLen += nSrcLen;
  77. pDesData = (byte*)malloc(nDesLen);
  78. #ifdef USE_LIB
  79. nRet = LzmaCompress(pDesData, &nDesLen, pSrcData, nSrcLen, prop, &nPropSize, 5, (1<<24), 8, 0, 2, 32, 2);
  80. #else
  81. nRet = LzmaCompress(pDesData, &nDesLen, pSrcData, nSrcLen, prop, &nPropSize, 9, (1<<24), 3, 0, 2, 32, 2);
  82. #endif
  83. }
  84. if ( SZ_OK == nRet )
  85. {//压缩完成,保存文件;
  86. fp = fopen(strSaveFile, "wb+");
  87. if ( NULL != fp )
  88. {
  89. // 写入压缩标记;
  90. fwrite(g_szLzma, strlen(g_szLzma), 1, fp);
  91. // 写入prop;
  92. fwrite(prop, 5, 1, fp);
  93. // 写入nPropSize;
  94. fwrite(&nPropSize, sizeof(size_t), 1, fp);
  95. // 写入文件大小;
  96. fwrite(&nSrcLen, sizeof(size_t), 1, fp);
  97. // 写入压缩数据;
  98. fwrite(pDesData, nDesLen, 1, fp);
  99. fclose(fp);
  100. }
  101. else
  102. {
  103. nRet = SZ_ERROR_FAIL;
  104. }
  105. }
  106. free(pDesData);
  107. free(pSrcData);
  108. if ( nRet != SZ_OK )
  109. return FALSE;
  110. }
  111. else
  112. {
  113. return FALSE;
  114. }
  115. return TRUE;
  116. }