Menu

#264 I tried to compress a file,it was success,but somethings were not my expected,I have no idea how to fix it.

open
nobody
None
5
2018-08-01
2018-07-17
Ken
No

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); 



}

Discussion

  • Igor Pavlov

    Igor Pavlov - 2018-07-17

    It's not 7z file, it's lzma file.
    So use the name myfile.bak.lzma instead.

     
    • Ken

      Ken - 2018-07-17

      thanks

       
    • Ken

      Ken - 2018-07-17

      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

      int decompress7zfile(const char * db7zfilefullpath,char * retmsg,int ignoreinsuffientspace,int ignorecrcerror,int returncode)
      {
          int useDestPath = 0;
          if (db7zfilefullpath == NULL)
          {
              return -1;
          }
      
          CFileInStream archiveStream;
          CLookToRead2 lookStream;
          CSzArEx db;
          SRes res;
          ISzAlloc allocImp;
          ISzAlloc allocTempImp;
          UInt16 *temp = NULL;
          UInt16 *destPath = NULL;
          size_t tempSize = 0;
          size_t destSize = 0;
          // UInt32 parents[NUM_PARENTS_MAX];
      
      
      
          UINT g_FileCodePage = CP_ACP;
      
          allocImp.Alloc = SzAlloc;
          allocImp.Free = SzFree;
      
          allocTempImp.Alloc = SzAllocTemp;
          allocTempImp.Free = SzFreeTemp;
      
          wchar_t * db7zfilefullpathw = new wchar_t[strlen(db7zfilefullpath) + 1];
          mbstowcs_s(NULL, db7zfilefullpathw, strlen(db7zfilefullpath) + 1, db7zfilefullpath, strlen(db7zfilefullpath));
      
          string fullpathstr(db7zfilefullpath);
          size_t found;
      
          wchar_t * db7zfolderw = new wchar_t[fullpathstr.length() + 1];
          mbstowcs_s(NULL, db7zfolderw, fullpathstr.length() + 1, fullpathstr.c_str(), fullpathstr.length());
      
          found = fullpathstr.find_last_of("/\\");
          const string folderpath=fullpathstr.substr(0, found);
      
          if (InFile_OpenW(&archiveStream.file, db7zfilefullpathw))
          {
              const char * errmsg = "it's not able to open source file";
              strcpy_s(retmsg, strlen(errmsg) + 1, errmsg);
              return -1;
          }
      
          FileInStream_CreateVTable(&archiveStream);
          LookToRead2_CreateVTable(&lookStream, False);
      
          lookStream.realStream = &archiveStream.vt;
          LookToRead2_Init(&lookStream);
      
      lookStream.buf = NULL;
      
      
          res = SZ_OK;
      
          {
              lookStream.buf =(byte *) ISzAlloc_Alloc(&allocImp, kInputBufSize);
              if (!lookStream.buf)
                  res = SZ_ERROR_MEM;
              else
              {
                  lookStream.bufSize = kInputBufSize;
                  lookStream.realStream = &archiveStream.vt;
                  LookToRead2_Init(&lookStream);
              }
          }
      
      
          CrcGenerateTable();
      
          SzArEx_Init(&db);
      
      **  res = SzArEx_Open(&db, &lookStream.vt, &allocImp, &allocTempImp);//it didn't work here res value was 17**
      
          if (res == SZ_OK)
          {
              int listCommand = 0, testCommand = 0, fullPaths = 1;
      
              if (res == SZ_OK)
              {
                  UInt32 i;
      
                  /*
                  if you need cache, use these 3 variables.
                  if you use external function, you can make these variable as static.
                  */
                  UInt32 blockIndex = 0xFFFFFFFF; /* it can have any value before first call (if outBuffer = 0) */
                  Byte *outBuffer = 0; /* it must be 0 before first call for each new archive. */
                  size_t outBufferSize = 0;  /* it can have any value before first call (if outBuffer = 0) */
      
                  for (i = 0; i < db.NumFiles; i++)
                  {
                      size_t offset = 0;
                      size_t outSizeProcessed = 0;
                      // const CSzFileItem *f = db.Files + i;
                      size_t len;
                      unsigned isDir = SzArEx_IsDir(&db, i);
                      if (listCommand == 0 && isDir && !fullPaths)
                          continue;
                      len = SzArEx_GetFileNameUtf16(&db, i, NULL);
                      // len = SzArEx_GetFullNameLen(&db, i);
      
                      if (len > tempSize)
                      {
                          SzFree(NULL, temp);
                          tempSize = len;
                          temp = (UInt16 *)SzAlloc(NULL, tempSize * sizeof(temp[0]));
                          if (!temp)
                          {
                              res = SZ_ERROR_MEM;
                              break;
                          }
                      }
      
                      SzArEx_GetFileNameUtf16(&db, i, temp);
      
                      //compare string if is "myfile"
      
                      const wchar_t * filenamew = L"myfile";
      
                      if (wcscmp(filenamew, (wchar_t *)temp) == 0)
                      {
      
                          UInt64 fileSize;
      
                          PULARGE_INTEGER freespace = 0;
                          PULARGE_INTEGER totalnumbtype = 0; 
                          PULARGE_INTEGER totalnumberfreebyte = 0;
      
                          if (ignoreinsuffientspace == 1)
                          {
                              //compare the free space is enough
                              fileSize = SzArEx_GetFileSize(&db, i);
                              BOOL myresult=GetDiskFreeSpaceExA(
                                   folderpath.c_str(),
                                  freespace,
                                  totalnumbtype,
                                  totalnumberfreebyte
                                  );
      
                              if (fileSize > (UInt64)freespace)
                              {
                                  const char * errmsg = "error occured the space was insuffcient";
                                  strcpy_s(retmsg, strlen(errmsg) + 1, errmsg);
                                  return -2;
                              }
      
                          }
      
      
                      }
      
      
      
      
      
                     //\\try to test the file is correct
      
                          if (ignorecrcerror == 1)
                          {
                              CSzFile outFile;
                              size_t processedSize;
      
                              UInt16 *name = (UInt16 *)temp;
                              if (useDestPath) {
                                  SzFree(NULL, destPath);
                                  destSize = wcslen(db7zfolderw);
                                  destSize += tempSize;
                                  destPath = (UInt16 *)SzAlloc(NULL, destSize* sizeof(destPath[0]));
                                  if (!destPath)
                                  {
                                      res = SZ_ERROR_MEM;
                                      break;
                                  }
                                  wcscpy((wchar_t*)destPath, db7zfolderw);
                                  wcscat((wchar_t*)destPath, (wchar_t*)temp);
                                  name = destPath + wcslen(db7zfolderw);
                              }
                              else
                                  destPath = (UInt16 *)name;
      
      
      
      
      
                              if (OutFile_OpenW(&outFile, (wchar_t*)destPath))
                              {
                                  const char * errmsg = "it's not able to openfile during crc checking";
                                  strcpy_s(retmsg, strlen(errmsg) + 1, errmsg);
      
                                  res = SZ_ERROR_FAIL;
                                  break;
                              }
      
                              processedSize = outSizeProcessed;
      
                              if (File_Write(&outFile, outBuffer + offset, &processedSize) != 0 || processedSize != outSizeProcessed)
                              {
                                  const char * errmsg = "can't write file during crc checking";
                                  strcpy_s(retmsg, strlen(errmsg) + 1, errmsg);
      
                                  res = SZ_ERROR_FAIL;
                                  break;
                              }
      
                              if (File_Close(&outFile))
                              {
                                  const char * errmsg = "can't close file during CRC checking";
                                  strcpy_s(retmsg, strlen(errmsg) + 1, errmsg);
      
                                  res = SZ_ERROR_FAIL;
                                  break;
                              }
      
      
                              if (SzBitWithVals_Check(&db.Attribs, i))
                                  SetFileAttributesW((wchar_t*)destPath, db.Attribs.Vals[i]);
                          }
                      //\\end checking
      
      
                      res = SzArEx_Extract(&db, &lookStream.vt, i,
                          &blockIndex, &outBuffer, &outBufferSize,
                          &offset, &outSizeProcessed,
                          &allocImp, &allocTempImp);
                      if (res != SZ_OK)
                          break;
      
      
                  }
                  IAlloc_Free(&allocImp, outBuffer);
              }
          }
      
          SzArEx_Free(&db, &allocImp);
          SzFree(NULL, temp);
          if (useDestPath) {
              SzFree(NULL, destPath);
          }
      
          File_Close(&archiveStream.file);
      
          if (res == SZ_OK)
          {
              const char * errmsg = "ok,well done";
              strcpy_s(retmsg, strlen(errmsg) + 1, errmsg);
              return 0;
          }
      
          if (res == SZ_ERROR_UNSUPPORTED)
          {
              const char * errmsg = "error:it's not supported";
              strcpy_s(retmsg, strlen(errmsg) + 1, errmsg);
      
          }
          else if (res == SZ_ERROR_MEM)
          {
      
              const char * errmsg = "error memory allocated";
              strcpy_s(retmsg, strlen(errmsg) + 1, errmsg);
          }
          else if (res == SZ_ERROR_CRC)
          {
              const char * errmsg = "error CRC checking error";
              strcpy_s(retmsg, strlen(errmsg) + 1, errmsg);
          }
          else
          {
              stringstream o;
              o << res;
              string myerrstr = "erorr occured,the code was" + o.str();
              strcpy_s(retmsg, myerrstr.length() + 1, myerrstr.c_str());
          }
          returncode = res;
          return returncode;
      }
      
      int _tmain(int argc, _TCHAR* argv[])
      {   
      
      
          const char * db7zfilefullpath = "D:\\dbak\\myfile.lzma";
          char * retmsg = new char[1000];
          int ignoreinsuffientspace = 1;
          int ignorecrcerror = 1;
          int returncode = 0;
      
          int result = decompress7zfile(db7zfilefullpath, retmsg, ignoreinsuffientspace, ignorecrcerror, returncode);
      
      
      
      }
      
       

      Last edit: Ken 2018-07-17
  • Ken

    Ken - 2018-07-17

    thanks

     
  • Igor Pavlov

    Igor Pavlov - 2018-07-17

    lzma and 7z are different formats.
    If you compress to lzma, then use lzma decompressor
    If you compress to 7z, then use 7z decompressor

     
    • Ken

      Ken - 2018-07-17

      thank you very much

       
    • Ken

      Ken - 2018-07-18

      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
  • Igor Pavlov

    Igor Pavlov - 2018-07-19

    1) you must use C++ code to compress to 7z archive

    CPP\7zip\UI\Client7z\
    

    or use command line:

    7z a a.7z *.txt
    

    2) LZMA file contains only one file (like gz or bzip2).
    So you don't need any list.

     
    • Ken

      Ken - 2018-07-20

      thank you very much

       
    • Ken

      Ken - 2018-07-20

      you are dope & really awesome & nice,thanks

       

      Last edit: Ken 2018-07-20
    • Ken

      Ken - 2018-07-30

      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
  • Igor Pavlov

    Igor Pavlov - 2018-08-01

    CPP\7zip\UI\Client7z\

     
    • Ken

      Ken - 2018-08-01

      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
    • Ken

      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

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.