Menu

Can I delete file from archive with 7z sdk/C++?

2017-10-09
2017-10-13
  • Dmitry Lodyakov

    Dmitry Lodyakov - 2017-10-09

    Hello. Is it possible to delete and also insert files to archive with 7z sdk (I'm using C++)?

     
  • Igor Pavlov

    Igor Pavlov - 2017-10-09

    Yes, if 7-zip can do some operation with 7z archive, you can do same operation with SDK. 7-Zip uses same API from SDK.

     
  • Dmitry Lodyakov

    Dmitry Lodyakov - 2017-10-09

    Thank you. Where can I see example about deleting files from archive?

     
  • Igor Pavlov

    Igor Pavlov - 2017-10-09

    Example code is 7-zip itself.
    So look 7-Zip source code.
    7-zip uses "Update" function. You must send the list of files (list of UInt32 indexes) that must be copied from previous version of archive to new version of archive. Another files will be deleted.

     

    Last edit: Igor Pavlov 2017-10-09
  • Dmitry Lodyakov

    Dmitry Lodyakov - 2017-10-09

    Thank you again.

     
  • Dmitry Lodyakov

    Dmitry Lodyakov - 2017-10-10

    And can I somehow add new file to archive?

     
  • Igor Pavlov

    Igor Pavlov - 2017-10-10

    yes, same UpdateItems() function.
    Look

    CPP\7zip\UI\Client7z\
    

    as small example.

     
  • Dmitry Lodyakov

    Dmitry Lodyakov - 2017-10-12

    Hello again. I'm trying to remove everything except file with index 0 from archive. But program is dying. Can you tell me what I'm doing wrong?

        COutFileStream *outFileStreamSpec = new COutFileStream;
        CMyComPtr<IOutStream> outFileStream = outFileStreamSpec;
        if (!outFileStreamSpec->Open((CFSTR)_root->_fullPath.utf16(), OPEN_EXISTING))
        {
          PrintError("can't create archive file");
          return;
        }
    
        if (createObjectFunc(&_archiveGUID, &IID_IOutArchive, (void **)&_outArchive) != S_OK)
        {
          PrintError("Can not get class object");
          return;
        }
    
        CArchiveUpdateCallback *updateCallbackSpec = new CArchiveUpdateCallback;
        CMyComPtr<IArchiveUpdateCallback2> updateCallback(updateCallbackSpec);
    
        QVector<int> items;
        items.append(0);
    
        updateCallbackSpec->Remove(items);
    
        HRESULT result = _outArchive->UpdateItems(outFileStream, items.size(), updateCallback);
    
        updateCallbackSpec->Finilize();
    
        ------------------------------------
          void Remove(const QVector<int> newIndexes)
      {
        _newIndexes = newIndexes;
        _removing = true;
        m_NeedBeClosed = false;
        FailedFiles.Clear();
        FailedCodes.Clear();
      }
    STDMETHODIMP CArchiveUpdateCallback::GetUpdateItemInfo(UInt32 index,
          Int32 *newData, Int32 *newProperties, UInt32 *indexInArchive)
    {
        if (_removing)
        {
          if (newData)
            *newData = BoolToInt(false);
          if (newProperties)
            *newProperties = BoolToInt(false);
          if (indexInArchive)
            *indexInArchive = (UInt32)(Int32)_newIndexes[index];
        }
      return S_OK;
    }
    
     

    Last edit: Dmitry Lodyakov 2017-10-12
  • Igor Pavlov

    Igor Pavlov - 2017-10-13

    Example:
    you have archive with 3 files: 0, 1, 2.
    you want to remove file=1.
    then you need 2 files in new archive:

    UpdateItems(outFileStream, 2)
    // and return indexes of items that you want to keep in new archive:
    GetUpdateItemInfo(0): *indexInArchive = 0;
    GetUpdateItemInfo(1): *indexInArchive = 2;
    
     
  • Dmitry Lodyakov

    Dmitry Lodyakov - 2017-10-13

    Igor, yes I did it as you said before, but program is crashing. Maybe I'm doing something wrong? Btw, can I build 7z in debug mode?

     
  • Igor Pavlov

    Igor Pavlov - 2017-10-13

    create small example archive with two items: 0 and 1.
    and try different opeartions.
    Add printf in callbacks.

     
  • Dmitry Lodyakov

    Dmitry Lodyakov - 2017-10-13

    When I was trying it was zip file, and it was crashing, but with 7z it's not crashing. Same code, but 7z instead of zip. I guess it's something with zip part of your lib.

     
  • Igor Pavlov

    Igor Pavlov - 2017-10-13

    7-zip works with objects.
    got updating you need:
    1) create object
    2) Open archive
    3) request IOutArchive from same object
    4) use IOutArchive interface.

    You didn't do step 3.

     

    Last edit: Igor Pavlov 2017-10-13
  • Dmitry Lodyakov

    Dmitry Lodyakov - 2017-10-13

    You mean that's not enough? It's just like in example.

    if (createObjectFunc(&_archiveGUID, &IID_IOutArchive, (void **)&_outArchive) != S_OK)
    {
          PrintError("Can not get class object");
          return;
    }
    
     
  • Igor Pavlov

    Igor Pavlov - 2017-10-13

    Think about case, if you want to update two archives from different threads.

     
  • Igor Pavlov

    Igor Pavlov - 2017-10-13

    There is no global object for update in 7-zip.
    So Input archive and output archive interfaces must be from same object.

     
  • Dmitry Lodyakov

    Dmitry Lodyakov - 2017-10-13

    Igor, thank you, I did it. But there is a new problem. After I remove file from zip arhive it became readonly. What is it?

     
  • Igor Pavlov

    Igor Pavlov - 2017-10-13

    There is a1.zip.
    When you remove file from a1.zip, you create a2.zip.
    So what is readonly?

     
  • Igor Pavlov

    Igor Pavlov - 2017-10-13

    1) you create a2.zip,
    2) close com interfaces to object (of a1.zip)
    3) remove a1.zip.
    4) rename a2.zip to a1.zip.

     
  • Dmitry Lodyakov

    Dmitry Lodyakov - 2017-10-13

    I've removed files from zip. Closed everything. And then I tried to remove it again with my soft, and I have #define E_NOTIMPL ((HRESULT)0x80004001L) error. And then when I'm trying to remove file with 7zFM I got error


    7-Zip

    Error Deleting File or Folder
    Operation is not supported.
    R:\Arc\Arc.zip
    Read-only


    OK

    But before I've deleted files from this zip file I could remove files fith 7zFM.

     
  • Igor Pavlov

    Igor Pavlov - 2017-10-13

    there is no remove operation in 7z.dll interface.
    After YOUR "remove" code, you have:
    1) a1.zip - old version of archive
    2) a2.zip - new version of archive
    so try to remove them.

     
  • Dmitry Lodyakov

    Dmitry Lodyakov - 2017-10-13

    What does this error mean?

    Error Deleting File or Folder
    Operation is not supported.
    R:\Arc\Arc.zip
    Read-only

     
  • Dmitry Lodyakov

    Dmitry Lodyakov - 2017-10-13

    Btw, code words great with 7z archives

     
  • Dmitry Lodyakov

    Dmitry Lodyakov - 2017-10-13

    Yes, it's working now. I save contents in the another archive and then rename it. Thank you.

     

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.