Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to overwrite existing files using Rubyzip lib

Tags:

ruby

unzip

I'm attempting to unzip a file with several files that may or may not already exist in the target directory. It seems the default behavior is to throw an exception if the file already exists.

How do I unzip to a directory and simply overwrite existing files?

Here's my code:

begin
  Zip::ZipFile.open(source) do |zipfile|
    dir = zipfile.dir
    dir.entries('.').each do |entry|
      zipfile.extract(entry, "#{target}/#{entry}")
    end
  end
rescue Exception => e
  log_error("Error unzipping file: #{local_zip}  #{e.to_s}")
end
like image 861
digitalsanctum Avatar asked Jun 08 '09 17:06

digitalsanctum


3 Answers

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...

like image 127
Ingmar Hamer Avatar answered Nov 17 '22 23:11

Ingmar Hamer


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.

like image 37
Greg Campbell Avatar answered Nov 17 '22 23:11

Greg Campbell


Edit: Modified code to remove target file if it exists beforehand.

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')

Edit: Modified code to remove target directory if it exists beforehand.

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')

Here's the original code from Mark Needham:

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')
like image 43
mechanical_meat Avatar answered Nov 17 '22 22:11

mechanical_meat