Как перезаписать существующее использование файлов lib Rubyzip

icon: (area == 0) ? icon0 : (area == 1) ? icon1 : icon2,
9
задан digitalsanctum 8 June 2009 в 17:44
поделиться

4 ответа

It appears that extract() takes an optional block (onExistsProc) that allows you to determine what to do with the file if it already exists - return true to overwrite, false to raise an exception.

If you wanted to simply overwrite all existing files, you could do:

zipfile.extract(entry, "#{target}/#{entry}") { true }

If you want to do some more complex logic to handle specific entries differently, you can do:

zipfile.extract(entry, "#{target}/#{entry}") {|entry, path| some_logic(entry, path) }

EDIT: fixed answer - as pointed out by Ingmar Hamer, my original answer passed the block as a parameter when it's expected using the above syntax.

15
ответ дан 4 December 2019 в 06:57
поделиться

Изменить: измененный код для удаления целевого файла, если он существует заранее.

require 'rubygems'
require 'fileutils'
require 'zip/zip'

def unzip_file(file, destination)
  Zip::ZipFile.open(file) { |zip_file|
   zip_file.each { |f|
     f_path=File.join(destination, f.name)
     if File.exist?(f_path) then
       FileUtils.rm_rf f_path
     end
     FileUtils.mkdir_p(File.dirname(f_path))
     zip_file.extract(f, f_path)
   }
  }
end

unzip_file('/path/to/file.zip', '/unzip/target/dir')

Изменить: измененный код для удаления целевого каталога, если он существует заранее.

require 'rubygems'
require 'fileutils'
require 'zip/zip'

def unzip_file(file, destination)
  if File.exist?(destination) then
    FileUtils.rm_rf destination
  end
  Zip::ZipFile.open(file) { |zip_file|
   zip_file.each { |f|
     f_path=File.join(destination, f.name)
     FileUtils.mkdir_p(File.dirname(f_path))
     zip_file.extract(f, f_path)
   }
  }
end

unzip_file('/path/to/file.zip', '/unzip/target/dir')

Вот исходный код из Марк Нидхэм :

require 'rubygems'
require 'fileutils'
require 'zip/zip'

def unzip_file(file, destination)
  Zip::ZipFile.open(file) { |zip_file|
   zip_file.each { |f|
     f_path=File.join(destination, f.name)
     FileUtils.mkdir_p(File.dirname(f_path))
     zip_file.extract(f, f_path) unless File.exist?(f_path)
   }
  }
end

unzip_file('/path/to/file.zip', '/unzip/target/dir')
1
ответ дан 4 December 2019 в 06:57
поделиться

Just to save others the trouble:

The extract command in answer 2 is incorrect:

The third (proc) parameter is specified wtih an ampersand, meaning ruby expects it to be in {}-Brackets after the method call like this:

zipfile.extract(entry, "#{target}/#{entry}"){ true }

or (if you need more complex logic)

zipfile.extract(entry, "#{target}/#{entry}") {|entry, path| some_logic(entry, path) }

If you use the example given in Post #2 you'll get a "invalid arguments (3 for 2)" error...

17
ответ дан 4 December 2019 в 06:57
поделиться

Эта ссылка здесь представляет собой хороший пример, работоспособность которого я проверил. Просто нужно добавить к нему require 'fileutils'.

0
ответ дан 4 December 2019 в 06:57
поделиться
Другие вопросы по тегам:

Похожие вопросы: