Linux中,向.tar.gz/.gz文件中添加一个文件,如何做?
迷茫
迷茫 2017-04-17 15:30:03
0
3
913

描述问题

需求: 对.zip .gz .tar.gz文件添加一个/几个新文件进去

在Windows上,操作极其简单,借助WinRAR,学习成本为0

在Linux上, 我

  1. 先是man tar man gzip

  2. 搜索 update append (gzip的搜索,直接没有, 关键词不对?)

  3. 然而没有找到支持压缩文件的选项(仅仅是对归档文件的支持)

注意到我需要:

  1. .zip .gz .tar.gz

  2. append/delete
    因此有6个需求

解决方法是?

上下文环境

Linux(仅仅使用发行版内置命令, 不借助任何外部工具, 换句话来说: 命令大概是gzip tar ...这些)
仅仅使用bash命令行(无GUI)

重现

查看man命令即可

相关代码

man的一部分信息

 -A, --catenate, --concatenate
           append tar files to an archive   是archive!

     -c, --create
           create a new archive

     -d, --diff, --compare
           find differences between archive and file system

     --delete
           delete from the archive (not on mag tapes!)

     -r, --append
           append files to the end of an archive

     -t, --list
           list the contents of an archive

     --test-label
           test the archive volume label and exit

     -u, --update
           only append files newer than copy in archive

报错信息

上述选项,对.tar.gz使用时会报错
比如: 
tar rzv -f zipfile.tar.gz new-file
tar: Cannot update compressed archives

相关截图

已经尝试哪些方法仍然没有解决(附上相关链接)

Google了

  1. append file to .tar.gz

  2. append file to .gz

  3. append dir to .tar.gz

附:
不成熟的吐槽

Linux是不是把事情搞复杂了?
还是我搜索/探索姿势不对? 
为啥有时候在Win上极其简单的一件事情,到了Linux上就要上升到折腾地步?  (得到一种莫名其妙的自我感动? )
迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

全部回覆(3)
PHPzhong
tar: Cannot update compressed archives

這個報錯其實已經說明問題了--不能對壓縮的’包‘進行更新操作。所以,可行的辦法就是將tar.gz 先解壓成 tar,然後再更新,最後再重新壓縮(可以寫個腳本)。

關於‘Win上極其簡單的一件事情’,只是因為 Win 上的軟體通常封裝的比較好而已,把“解壓-更新-壓縮”的過程隱藏起來了而已。這裡面孰優孰劣要看具體情況,winrar 如題主所說學習成本為零,但是缺點是不可不可編程(也可能是我孤陋寡聞)。試想,如果有每天有大量的壓縮包需要更新,那是不是要專門找個人在 winrar 裡點點呢?

PHPzhong

目前所知,壓縮後的檔案是不可追加的。
可以先追加到tar文件,再壓縮也行吧?
tar -rvf ttt.tar aaa.txt

黄舟

只用targzip的話,.tar.gz好像只能先解壓縮再壓縮...

話說難道我們用的google不是同一個?為啥我搜到了個解決方案:

http://unix.stackexchange.com...

裡面提到使用FUSE或tarfs就可以了。

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!