2012-03-12 3 views
0

Utilisation de Ruby (1.9.3) Je dois remplacer un fichier unique dans une archive zip.Remplacement d'un fichier dans une archive zip

La situation est la suivante. J'ai ~ 1000 archives zip qui ont besoin d'être mis à jour, en particulier un fichier dans chacun d'entre eux doit être remplacé. Les archives ont toutes la même structure. Existe-t-il un moyen rapide et sale pour Ruby, ou une bibliothèque/gemme pour Ruby, de simplement dire "remplacer le fichier dans cette archive zip par ce fichier sur le système de fichiers"?

Je vais travailler sur une solution en attendant.

+0

est bash trop acceptable? – Reactormonk

Répondre

1

Vous pouvez utiliser la commande zip, appelée depuis le ruby, qui sera probablement la meilleure solution. De l'manpage 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. 

Si vous voulez une solution de pur rubis jeter un oeil à ZipFileSystem

+0

Cela fera très bien. Merci. –

0

Zip::ZipFile semble prometteur. Il semble avoir un moyen de supprimer et d'ajouter des fichiers dans une archive zip.

Questions connexes