簡體   English   中英

替換zip存檔中的文件

[英]Replacing a file in a zip archive

使用Ruby(1.9.3),我需要替換zip存檔中的單個文件。

情況如下。 我有約1000個zip檔案需要更新,特別是每個檔案都需要替換。 檔案都具有相同的結構。 對於Ruby,還是對於Ruby的庫/寶石,有沒有一種快速而又骯臟的方法,只是說“將zip存檔中的文件與此文件系統上的文件替換”?

同時,我將自己制定解決方案。

您可以使用從ruby調用的zip命令,這可能是最好的解決方案。 從zip手冊頁zip手冊頁

       -d
   --delete
          Remove (delete) entries from a zip archive.  For example:

                 zip -d foo foo/tom/junk foo/harry/\* \*.o

          will remove the entry foo/tom/junk, all of the files that start with foo/harry/, and all of the files that end with .o (in any path).  Note that  shell  path‐
          name expansion has been inhibited with backslashes, so that zip can see the asterisks, enabling zip to match on the contents of the zip archive instead of the
          contents of the current directory.  (The backslashes are not used on MSDOS-based platforms.)  Can also use quotes to escape the asterisks as in

                 zip -d foo foo/tom/junk "foo/harry/*" "*.o"

          Not escaping the asterisks on a system where the shell expands wildcards could result in the asterisks being converted to a  list  of  files  in  the  current
          directory and that list used to delete entries from the archive.

          Under  MSDOS,  -d  is  case sensitive when it matches names in the zip archive.  This requires that file names be entered in upper case if they were zipped by
          PKZIP on an MSDOS system.  (We considered making this case insensitive on systems where paths were case insensitive, but it is possible the archive came  from
          a  system  where case does matter and the archive could include both Bar and bar as separate files in the archive.)  But see the new option -ic to ignore case
          in the archive.

如果您想使用純紅寶石解決方案,請查看ZipFileSystem

Zip :: ZipFile看起來很有前途。 它似乎具有刪除文件並將文件添加到zip存檔的方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM