2012-03-12 221 views
0

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

情况如下。我有大约1000个需要更新的zip文件,特别是每个文件中的一个文件需要被替换。档案都是相同的结构。有没有一种快速和肮脏的方式来使用Ruby,或者Ruby的库/ gem来简单地说“使用文件系统上的这个文件替换这个zip压缩文件中的文件”?

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

+0

是bash的接受呢? – Reactormonk 2012-03-12 16:33:30

回答

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. 

如果你想要一个纯Ruby的解决方案来看看ZipFileSystem

+0

这样做会很好。谢谢。 – 2012-03-12 19:20:25

0

Zip::ZipFile看起来很有希望。它似乎有办法删除和添加文件到zip压缩文件。