替换zip存档中的文件

时间:2012-03-12 15:43:24

标签: ruby rubygems zip

使用Ruby(1.9.3)我需要替换zip存档中的单个文件。

情况如下。我有~1000个需要更新的zip档案,特别是需要更换每个档案中的一个档案。档案都是相同的结构。对于Ruby,或Ruby的库/ gem,有一种快速而肮脏的方式,只需说"用文件系统中的这个文件替换此zip存档中的文件"?

在此期间,我将自己解决方案。

2 个答案:

答案 0 :(得分:1)

您可以使用从ruby调用的zip命令,这可能是最佳解决方案。来自zip联机帮助页zip manpage

       -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

答案 1 :(得分:0)

Zip::ZipFile看起来很有希望。它似乎有办法删除文件并将其添加到zip存档。

相关问题