How to update one file in a zip archive

后端 未结 10 1230
心在旅途
心在旅途 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 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%)
    

提交回复
热议问题