Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include "Precomp.h"
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <string>
- #include <sstream>
- #include "CpuArch.h"
- #include "7z.h"
- #include "7zCrc.h"
- #include "Alloc.h"
- #include "7zAlloc.h"
- #include "7zFile.h"
- #include "7zVersion.h"
- #include "LzmaDec.h"
- #include "LzmaEnc.h"
- #include <Windows.h>
- #include <Shlwapi.h> // PathGetDriveNumber, PathBuildRoot
- #pragma comment(lib, "Shlwapi.lib")
- using namespace std;
- #define kInputBufSize ((size_t)1 << 18)
- #define IN_BUF_SIZE (1 << 16)
- #define OUT_BUF_SIZE (1 << 16)
- int compresslzmafile(const char * srcbakfilefullpath, const char * det7zfilefullpath,char * retmsg,int returncode)
- {
- CFileSeqInStream inStream;
- CFileOutStream outStream;
- int res;
- char *rs;
- Bool useOutFile = False;
- FileSeqInStream_CreateVTable(&inStream);
- File_Construct(&inStream.file);
- FileOutStream_CreateVTable(&outStream);
- File_Construct(&outStream.file);
- size_t t4 = sizeof(UInt32);
- size_t t8 = sizeof(UInt64);
- if (t4 != 4 || t8 != 8)
- {
- const char * errmsg = "UInt32 or UInt64 error";
- strcpy_s(retmsg, strlen(errmsg) + 1, errmsg);
- return -11;
- }
- wchar_t * srcbakfilefullpathw = new wchar_t[strlen(srcbakfilefullpath) + 1];
- mbstowcs_s(NULL, srcbakfilefullpathw, strlen(srcbakfilefullpath) + 1, srcbakfilefullpath, strlen(srcbakfilefullpath));
- wchar_t * det7zfilefullpathw = new wchar_t[strlen(det7zfilefullpath) + 1];
- mbstowcs_s(NULL, det7zfilefullpathw, strlen(det7zfilefullpath) + 1, det7zfilefullpath, strlen(det7zfilefullpath));
- if (InFile_OpenW(&inStream.file, srcbakfilefullpathw) != 0)
- {
- const char * errmsg = "source file open failed";
- strcpy_s(retmsg, strlen(errmsg) + 1, errmsg);
- return -12;
- }
- useOutFile = True;
- if (OutFile_OpenW(&outStream.file, det7zfilefullpathw) != 0)
- {
- const char * errmsg = "file open error";
- strcpy_s(retmsg, strlen(errmsg) + 1, errmsg);
- return -13;
- }
- UInt64 fileSize;
- File_GetLength(&inStream.file, &fileSize);
- //\start encord
- CLzmaEncHandle enc;
- CLzmaEncProps props;
- UNUSED_VAR(rs);
- enc = LzmaEnc_Create(&g_Alloc);
- if (enc == 0)
- return SZ_ERROR_MEM;
- LzmaEncProps_Init(&props);
- res = LzmaEnc_SetProps(enc, &props);
- if (res == SZ_OK)
- {
- Byte header[LZMA_PROPS_SIZE + 8];
- size_t headerSize = LZMA_PROPS_SIZE;
- int i;
- res = LzmaEnc_WriteProperties(enc, header, &headerSize);
- for (i = 0; i < 8; i++)
- header[headerSize++] = (Byte)(fileSize >> (8 * i));
- if (outStream.vt.Write(&outStream.vt, header, headerSize) != headerSize)
- res = SZ_ERROR_WRITE;
- else
- {
- if (res == SZ_OK)
- res = LzmaEnc_Encode(enc, &outStream.vt, &inStream.vt, NULL, &g_Alloc, &g_Alloc);
- }
- }
- LzmaEnc_Destroy(enc, &g_Alloc, &g_Alloc);
- return res;
- //\end encode
- if (useOutFile)
- File_Close(&outStream.file);
- File_Close(&inStream.file);
- if (res != SZ_OK)
- {
- if (res == SZ_ERROR_MEM)
- {
- const char * errmsg = "allocated memory error";
- strcpy_s(retmsg, strlen(errmsg) + 1, errmsg);
- }
- else if (res == SZ_ERROR_DATA)
- {
- const char * errmsg = "data error";
- strcpy_s(retmsg, strlen(errmsg) + 1, errmsg);
- }
- else if (res == SZ_ERROR_WRITE)
- {
- const char * errmsg = "write error";
- strcpy_s(retmsg, strlen(errmsg) + 1, errmsg);
- }
- else if (res == SZ_ERROR_READ)
- {
- const char * errmsg = "source file read error";
- strcpy_s(retmsg, strlen(errmsg) + 1, errmsg);
- }
- else
- {
- stringstream o;
- o << res;
- string myerrstr = "other error occured,code was" + o.str();
- strcpy_s(retmsg, myerrstr.length() + 1, myerrstr.c_str());
- }
- }
- return 0;
- }
- int _tmain(int argc, _TCHAR* argv[])
- {
- const char * srcbakfilefullpath = "D:\dbak\myfile.bak";
- const char * det7zfilefullpath = "D:\dbak\myfile.lzma";
- char * retmsg = new char[1000];
- int returncode = 0;
- int result = compresslzmafile(srcbakfilefullpath, det7zfilefullpath, retmsg, returncode);
- }
Add Comment
Please, Sign In to add comment