How to update one file in a zip archive

后端 未结 10 1177
心在旅途
心在旅途 2020-12-02 16:16

Is it possible to replace a file in a zip file without unzipping deleting the old file adding the new file and rezipping it back?

Reason is I have a zip file which i

相关标签:
10条回答
  • 2020-12-02 16:57

    There is also the -f option that will freshen the zip file. It can be used to update ALL files which have been updated since the zip was generated (assuming they are in the same place within the tree structure within the zip file).

    If your file is named /myfiles/myzip.zip all you have to do is

    zip -f /myfiles/myzip.zip
    
    0 讨论(0)
  • 2020-12-02 17:05

    Try the following:

    zip [zipfile] [file to update] 
    

    An example:

    $ zip test.zip test/test.txt
    updating: test/test.txt (stored 0%)
    
    0 讨论(0)
  • 2020-12-02 17:07

    Use the update flag: -u

    Example:

    zip -ur existing.zip myFolder

    This command will compress and add myFolder (and it's contents) to the existing.zip.


    Advanced Usage:

    The update flag actually compares the incoming files against the existing ones and will either add new files, or update existing ones.

    Therefore, if you want to add/update a specific subdirectory within the zip file, just update the source as desired, and then re-zip the entire source with the -u flag. Only the changed files will be zipped.

    If you don't have access to the source files, you can unzip the zip file, then update the desired files, and then re-zip with the -u flag. Again, only the changed files will be zipped.

    Example:

    Original Source Structure

    
    ParentDir
    ├── file1.txt
    ├── file2.txt
    ├── ChildDir
    │   ├── file3.txt
    │   ├── Logs
    │   │   ├── logs1.txt
    │   │   ├── logs2.txt
    │   │   ├── logs3.txt
    

    Updated Source Structure

    
    ParentDir
    ├── file1.txt
    ├── file2.txt
    ├── ChildDir
    │   ├── file3.txt
    │   ├── Logs
    │   │   ├── logs1.txt
    │   │   ├── logs2.txt
    │   │   ├── logs3.txt 
    │   │   ├── logs4.txt <-- NEW FILE 
    

    Usage

    $ zip -ur existing.zip ParentDir 
    > updating: ParentDir/ChildDir/Logs (stored 0%)
    >   adding: ParentDir/ChildDir/Logs/logs4.txt (stored 96%)
    
    0 讨论(0)
  • 2020-12-02 17:11

    7zip (7za) can be used for adding/updating files/directories nicely:

    Example: Replacing (regardless of file date) the MANIFEST.MF file in a JAR file. The /source/META-INF directory contains the MANIFEST.MF file that you want to put into the jar (zip):

    7za a /tmp/file.jar /source/META-INF/
    

    Only update (does not replace the target if the source is older)

    7za u /tmp/file.jar /source/META-INF/
    
    0 讨论(0)
提交回复
热议问题