Guest User

Untitled

a guest
Jul 17th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.13 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include "Precomp.h"
  3.  
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <string>
  8. #include <sstream>
  9.  
  10. #include "CpuArch.h"
  11.  
  12. #include "7z.h"
  13. #include "7zCrc.h"
  14. #include "Alloc.h"
  15. #include "7zAlloc.h"
  16. #include "7zFile.h"
  17. #include "7zVersion.h"
  18. #include "LzmaDec.h"
  19. #include "LzmaEnc.h"
  20. #include <Windows.h>
  21. #include <Shlwapi.h> // PathGetDriveNumber, PathBuildRoot
  22. #pragma comment(lib, "Shlwapi.lib")
  23.  
  24. using namespace std;
  25.  
  26. #define kInputBufSize ((size_t)1 << 18)
  27. #define IN_BUF_SIZE (1 << 16)
  28. #define OUT_BUF_SIZE (1 << 16)
  29.  
  30.  
  31. int compresslzmafile(const char * srcbakfilefullpath, const char * det7zfilefullpath,char * retmsg,int returncode)
  32. {
  33. CFileSeqInStream inStream;
  34. CFileOutStream outStream;
  35.  
  36. int res;
  37.  
  38. char *rs;
  39. Bool useOutFile = False;
  40.  
  41. FileSeqInStream_CreateVTable(&inStream);
  42. File_Construct(&inStream.file);
  43.  
  44. FileOutStream_CreateVTable(&outStream);
  45. File_Construct(&outStream.file);
  46.  
  47.  
  48.  
  49.  
  50.  
  51. size_t t4 = sizeof(UInt32);
  52. size_t t8 = sizeof(UInt64);
  53. if (t4 != 4 || t8 != 8)
  54. {
  55. const char * errmsg = "UInt32 or UInt64 error";
  56. strcpy_s(retmsg, strlen(errmsg) + 1, errmsg);
  57. return -11;
  58. }
  59.  
  60. wchar_t * srcbakfilefullpathw = new wchar_t[strlen(srcbakfilefullpath) + 1];
  61. mbstowcs_s(NULL, srcbakfilefullpathw, strlen(srcbakfilefullpath) + 1, srcbakfilefullpath, strlen(srcbakfilefullpath));
  62.  
  63.  
  64. wchar_t * det7zfilefullpathw = new wchar_t[strlen(det7zfilefullpath) + 1];
  65. mbstowcs_s(NULL, det7zfilefullpathw, strlen(det7zfilefullpath) + 1, det7zfilefullpath, strlen(det7zfilefullpath));
  66.  
  67. if (InFile_OpenW(&inStream.file, srcbakfilefullpathw) != 0)
  68. {
  69.  
  70. const char * errmsg = "source file open failed";
  71. strcpy_s(retmsg, strlen(errmsg) + 1, errmsg);
  72. return -12;
  73. }
  74.  
  75.  
  76. useOutFile = True;
  77. if (OutFile_OpenW(&outStream.file, det7zfilefullpathw) != 0)
  78. {
  79. const char * errmsg = "file open error";
  80. strcpy_s(retmsg, strlen(errmsg) + 1, errmsg);
  81. return -13;
  82. }
  83.  
  84.  
  85. UInt64 fileSize;
  86. File_GetLength(&inStream.file, &fileSize);
  87.  
  88. //\start encord
  89. CLzmaEncHandle enc;
  90.  
  91. CLzmaEncProps props;
  92.  
  93. UNUSED_VAR(rs);
  94.  
  95. enc = LzmaEnc_Create(&g_Alloc);
  96. if (enc == 0)
  97. return SZ_ERROR_MEM;
  98.  
  99. LzmaEncProps_Init(&props);
  100. res = LzmaEnc_SetProps(enc, &props);
  101.  
  102. if (res == SZ_OK)
  103. {
  104.  
  105. Byte header[LZMA_PROPS_SIZE + 8];
  106. size_t headerSize = LZMA_PROPS_SIZE;
  107. int i;
  108.  
  109. res = LzmaEnc_WriteProperties(enc, header, &headerSize);
  110. for (i = 0; i < 8; i++)
  111. header[headerSize++] = (Byte)(fileSize >> (8 * i));
  112. if (outStream.vt.Write(&outStream.vt, header, headerSize) != headerSize)
  113. res = SZ_ERROR_WRITE;
  114. else
  115. {
  116. if (res == SZ_OK)
  117. res = LzmaEnc_Encode(enc, &outStream.vt, &inStream.vt, NULL, &g_Alloc, &g_Alloc);
  118. }
  119. }
  120. LzmaEnc_Destroy(enc, &g_Alloc, &g_Alloc);
  121. return res;
  122.  
  123. //\end encode
  124.  
  125.  
  126.  
  127. if (useOutFile)
  128. File_Close(&outStream.file);
  129. File_Close(&inStream.file);
  130.  
  131. if (res != SZ_OK)
  132. {
  133. if (res == SZ_ERROR_MEM)
  134. {
  135. const char * errmsg = "allocated memory error";
  136. strcpy_s(retmsg, strlen(errmsg) + 1, errmsg);
  137. }
  138. else if (res == SZ_ERROR_DATA)
  139. {
  140. const char * errmsg = "data error";
  141. strcpy_s(retmsg, strlen(errmsg) + 1, errmsg);
  142. }
  143. else if (res == SZ_ERROR_WRITE)
  144. {
  145. const char * errmsg = "write error";
  146. strcpy_s(retmsg, strlen(errmsg) + 1, errmsg);
  147. }
  148. else if (res == SZ_ERROR_READ)
  149. {
  150. const char * errmsg = "source file read error";
  151. strcpy_s(retmsg, strlen(errmsg) + 1, errmsg);
  152. }
  153. else
  154. {
  155. stringstream o;
  156. o << res;
  157. string myerrstr = "other error occured,code was" + o.str();
  158. strcpy_s(retmsg, myerrstr.length() + 1, myerrstr.c_str());
  159. }
  160. }
  161. return 0;
  162.  
  163. }
  164.  
  165.  
  166. int _tmain(int argc, _TCHAR* argv[])
  167. {
  168. const char * srcbakfilefullpath = "D:\dbak\myfile.bak";
  169. const char * det7zfilefullpath = "D:\dbak\myfile.lzma";
  170. char * retmsg = new char[1000];
  171. int returncode = 0;
  172. int result = compresslzmafile(srcbakfilefullpath, det7zfilefullpath, retmsg, returncode);
  173. }
Add Comment
Please, Sign In to add comment