Menu

Replace file in a 7z archive

omidsolo
2017-12-30
2020-06-07
  • omidsolo

    omidsolo - 2017-12-30

    I have a .7z archive and I want to replace (delete/add) a file inside archive.
    I don't want to change the compression of whole archive and I also want the file which is being replaced to be added with the same compression as original file being replaced.
    Both old and new files are same name "Setup.ism".
    Please advise the best way to do so, no any properties of original .7z should be changed as well.
    Thanks.

     
  • Igor Pavlov

    Igor Pavlov - 2017-12-31
    7z a a.7z Setup.ism
    
     
  • Vladimir V Kozlov

    I have a similar problem - I am trying to surgically manipulate one file in a .docx file, which is in reality a ZIP file produced by MS Word. Specifically, this file is word\comments.xml, which contains comments and author information. The ,docx may be large (my example is 815K), thus no desire to extract the entire archive (onto a slow network). Lastly, everything is done using automation and the Windows Shell through VBA.

    Extracting this file is no problem.
    "C:\Program Files\7-Zip\7z.exe" e -y -oC:\Users\VVKOZLOV\AppData\Local\Temp\ "C:\Users\VVKOZLOV\Documents\Zaraza.docx" word\comments.xml

    The file is edited with a few lines of VBA. Now I need to put it back.

    I now do this in three steps. Update:
    "C:\Program Files\7-Zip\7z.exe" u -y "C:\Users\VVKOZOLV\Documents\Zaraza.docx" C:\Users\VVKOZLOV\AppData\Local\Temp\comments.xml

    Delete:
    "C:\Program Files\7-Zip\7z.exe" d -y "C:\Users\VVKOZLOV\Documents\Zaraza.docx" word\comments.xml

    Rename:
    "C:\Program Files\7-Zip\7z.exe" rn -y "C:\Users\VVKOZLOV\Documents\Zaraza.docx" comments.xml word\comments.xml

    Is there a better way to do this, but with one step?

    Suggestion:
    If not, and not without building and manipulating more temporary files and directories, I would have expected to see something like this:
    "C:\Program Files\7-Zip\7z.exe" u -y -oword\ "C:\Users\VVKOZOLV\Documents\Zaraza.docx" C:\Users\VVKOZLOV\AppData\Local\Temp\comments.xml

    The -o switch would work like it does in e and x, but in reverse - it would indicate the directory in the archive where to write the file.

     
  • Igor Pavlov

    Igor Pavlov - 2018-01-05
    7z x a.zip word\comments.xml
    7z a a.zip word\comments.xml
    
     
  • omidsolo

    omidsolo - 2018-01-19

    7z a "D:\7-Zip\Data1.7z" "D:\7-Zip\Setup.ism"

    "D:\7-Zip\Data1.7z" is my original compressed file which contain many setup files including Setup.ism.

    And Setup.ism is my new file which I'd like to update the original 7z ARCHIVE with it.

    But as you mentioned:
    7z a "D:\7-Zip\Data1.7z" "D:\7-Zip\Setup.ism"
    Does not work:
    WARNING: The system cannot find the file specified. : D:\7-Zip\Setup.ism
    But it is there.
    Is that the right command to insert the new Setup.ism into Data1.7z and replace it with original one, compression and all archive properties must be intact, so my installer won't get in trouble.
    Vital, please :)

     
  • Igor Pavlov

    Igor Pavlov - 2018-01-20

    It must work.
    Check it again:

    dir "D:\7-Zip\Setup.ism"
    7z a "D:\7-Zip\Data1.7z" "D:\7-Zip\Setup.ism"
    
     
  • omidsolo

    omidsolo - 2018-01-20

    7z a "D:\7-Zip\Data1.7z" "D:\7-Zip\PowerDVD.sim" -m0=LZMA2:d=26 -aoa

    OK, but something now:!

     

    Last edit: omidsolo 2018-01-20
  • omidsolo

    omidsolo - 2018-01-20

    and should I specify the exact path of .sim file to be replaced inside the archive?
    once it's in the root, and for another file it's inside some sub folders...
    Thanks indeed...

     
  • Igor Pavlov

    Igor Pavlov - 2018-01-20

    1) When you update files, 7-Zip creates new solid block for such files. And if call update again, these files can be updated faster without recompression of another blocks.
    2) you can store full paths with -spf2 switch:

    -spf2
    
     
  • omidsolo

    omidsolo - 2018-01-20

    1) thanks but I don't want add new blocks, my file.sim is in Block 0 for example, I wanna add new file.sim which has exact same name to archive with the same properties (comp mode, etc and also same block, even if itakes long time it's OK.
    Whats the final command ? :(

     
  • Igor Pavlov

    Igor Pavlov - 2018-01-20

    No way now.
    7-zip creates new block with new method when you add/update file to archive.

    For example, someone created 7z archive at Xeon CPU with 64 GB of RAM usage.
    You extract small txt file and then update it back to same archive in another computer with 2 GB of RAM. But you have no 64 GB of ram to use same method settings. So you use another method that works with your 2 GB ram.

     

    Last edit: Igor Pavlov 2018-01-20
  • Gonen Shoham

    Gonen Shoham - 2020-06-07

    I need help on replacing single file in specific folder within an archive file.

    step 1
    I extract config.txt file form folder ARIA0801m01\bin\packages\0801m01\skel\config.txt in archive D:\ricore\0801m01\FRMCRP2\ARIA0801m01.zip into D:\temp using the following command:

    7z x D:\ri_core\0801m01\FRM_CRP2\ARIA0801m01.zip ARIA0801m01\bin\packages\0801m01\skel\config.txt -oD:\temp\unzip -y

    Step 2
    then I edit and update the file manually

    I would like to replace it back in the original location within the archive. ARIA0801m01\bin\packages\0801m01\skel\config.txt

    Note:
    there are many config.txt files within the archive in other folders.
    I am asking to replace only the original file without touching the others

    thank you for any help.

     
  • Igor Pavlov

    Igor Pavlov - 2020-06-07

    Use relative paths from current folder:

    7z a c:\f1\f2.zip folder1\folder2\file3.txt
    
     
  • Gonen Shoham

    Gonen Shoham - 2020-06-07

    thank for a quick reply....

    I tried many options with no luck.....

    it aways creates the file in the root folder of the zip file.....

    I ask for more details....

    the zip file is located in :
    D:\ricore\0801m01\FRMCRP2\ARIA0801m01.zip

    The target config.txt file is located in the zip file in:
    *\ARIA0801m01\bin\packages\0801m01\skel*

    where should I place the new config.txt file that I would like to use as input for the replace?
    How would the command look like?

    thanks a lot for help !!!!

     
  • Gonen Shoham

    Gonen Shoham - 2020-06-07

    This is where I came up to:

    D:\temp>"C:\Program Files\7-Zip\7z" a "d:\ri_core\0801m01\FRM_CRP2\ARIA0801m01.zip" "d:\ARIA0801m01\bin\packages\0801m01\skel\config.txt" "\ARIA0801m01\bin\packages\0801m01\skel"

    output on screen
    7-Zip 19.00 (x64) : Copyright (c) 1999-2018 Igor Pavlov : 2019-02-21

    Open archive: d:\ri_core\0801m01\FRM_CRP2\ARIA0801m01.zip

    Path = d:\ri_core\0801m01\FRM_CRP2\ARIA0801m01.zip
    Type = zip
    Physical Size = 1986022630

    Scanning the drive:
    1 folder, 2 files, 518 bytes (1 KiB)

    Updating archive: d:\ri_core\0801m01\FRM_CRP2\ARIA0801m01.zip

    Keep old data in archive: 502 folders, 10676 files, 3011557311 bytes (2873 MiB)
    Add new data to archive: 1 folder, 2 files, 518 bytes (1 KiB)

    Files read from disk: 2
    Archive size: 1986023308 bytes (1895 MiB)
    Everything is Ok

    results: (can be seen in the attached file)
    config.txt file - added on the root
    skel folder with file config.txt was added on the root

    expected:
    update/add file config.txt in \ARIA0801m01\bin\packages\0801m01\skel

    thanks for help

     

Log in to post a comment.