I tried to compress a file,it was success,but somethings were not my...
A free file archiver for extremely high compression
Brought to you by:
ipavlov
hey there,I tried to compress a file,it was success,but somethings were not my expected,
first of all the file extension name was dispeared which I compressed,my originally file name was myfile.bak,the file within the archieve file was myfile,there was not file extension name,
secondly the archieve file is not able to open by winrar and reported it was a damaged 7z file,however it totally be able to open by 7zip file manage,it could be extracted with 7z file manage,I'll show my code,I have no idea what wrong I have done. I compile the code within VS 2013 and Windows 7, thanks in advance
#include "Precomp.h"
#include <stdio.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>
using namespace std;
#define kInputBufSize ((size_t)1 << 18)
int compress7zfile(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 = "error opening source file";
strcpy_s(retmsg, strlen(errmsg) + 1, errmsg);
return -12;
}
useOutFile = True;
if (OutFile_OpenW(&outStream.file, det7zfilefullpathw) != 0)
{
const char * errmsg = "error opening 7z file";
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 during compressing";
strcpy_s(retmsg, strlen(errmsg) + 1, errmsg);
}
else if (res == SZ_ERROR_DATA)
{
const char * errmsg = "data was error during compressing";
strcpy_s(retmsg, strlen(errmsg) + 1, errmsg);
}
else if (res == SZ_ERROR_WRITE)
{
const char * errmsg = "write error during compressing";
strcpy_s(retmsg, strlen(errmsg) + 1, errmsg);
}
else if (res == SZ_ERROR_READ)
{
const char * errmsg = "not able to read during compressing";
strcpy_s(retmsg, strlen(errmsg) + 1, errmsg);
}
else
{
stringstream o;
o << res;
string myerrstr = "other error was curred,the 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.7z";
char * retmsg = new char[1000];
int returncode = 0;
int result = compress7zfile(srcbakfilefullpath, det7zfilefullpath, retmsg, returncode);
}
It's not
7z
file, it'slzma
file.So use the name
myfile.bak.lzma
instead.thanks
I tried to decompress this file,however the example didn't work
at the line of code which is
res = SzArEx_Open(&db, &lookStream.vt, &allocImp, &allocTempImp);//it didn't work here, the return value of variable which res was 17
would you please tell me what wrong I have done?thanks
Last edit: Ken 2018-07-17
thanks
lzma
and7z
are different formats.If you compress to
lzma
, then use lzma decompressorIf you compress to
7z
, then use 7z decompressorthank you very much
What I have tried 1: I have tried Googled (key words were "7z compress example clang")and double check the sample(\lzma1805\C\Util\7z\7zMain.c) & the doc (all the txt files within \lzma1805\DOC),I didn't find the samples which 7z file compress,there was just a decompress demo in 7zMain.c file,no compress example
Question: would you please point out where is the 7z file compress sample within LZMA SDK?
What I have tried 2: I have found the sample which LZMA compress/decompress,however I don't even know how to list the contents within the LZMA archieve files,I tried to Google it,it didn't work,there were no such web pages
Question: would you please show me a little bit how to list the contents of LZMA file?
Last edit: Ken 2018-07-19
1) you must use C++ code to compress to 7z archive
or use command line:
2) LZMA file contains only one file (like gz or bzip2).
So you don't need any list.
thank you very much
you are dope & really awesome & nice,thanks
Last edit: Ken 2018-07-20
sorry for disturbing you again,I'd like to ask my last question I guess,I have Googled "C++ 7z test archieve example",it didn't work,my question is would you please point out the sample which is c++ test 7z archieve file,I just found an example in C sample,however it's not C++ sample,thanks
Last edit: Ken 2018-07-30
CPP\7zip\UI\Client7z\
thank you for answering my question,however it doesn't contains the example of test archiev file,I tried to Google how do I calculate the CRC32 checksum from the file which extract the file from the archieve,I thought it's the method which test the aricheve file,I have double checked the example,it contains 3 example which are #1 compress #2 decompress #3 list the contents within the archive file,there's no test archieve file example
Last edit: Ken 2018-08-01
sorry for distrubing you again,I have double checked the documents and codes,and I found this in Iarchive.h
STDMETHOD(Extract)(const UInt32 indices, UInt32 numItems, Int32 testMode, IArchiveExtractCallback extractCallback) MY_NO_THROW_DECL_ONLY x; \
the explains were
IInArchive::Extract:
indices must be sorted
numItems = (UInt32)(Int32)-1 = 0xFFFFFFFF means "all files"
testMode != 0 means "test files without writing to outStream"
I think if add the value of true to paramters testMode,it would be test the archieve file I guess,cuz I couldn't found the code which implementation of this function,it's weird that I can't find the code of Extract function
I'm gonna try this
Last edit: Ken 2018-08-01