# ZipFile.extract overwrite option

**URL:** <https://www.ruby-forum.com/t/zipfile-extract-overwrite-option/176947>\
**Category:** Ruby\
**Created:** [October 8, 2009, 6:41pm UTC](https://www.ruby-forum.com/t/zipfile-extract-overwrite-option/176947 "2009-10-08T18:41:36Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jimboberlin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jimboberlin](https://www.ruby-forum.com/u/jimboberlin)\
**Post date:** [October 8, 2009, 6:41pm UTC](https://www.ruby-forum.com/t/zipfile-extract-overwrite-option/176947/1 "2009-10-08T18:41:36Z")

</div>

Hi

I’m trying to extract a Zipfile using the ZipFile Class.  
According to the docs,  
[Class: Zip::ZipFile](http://rubyzip.sourceforge.net/classes/Zip/ZipFile.html#M000150)"  
the extract method takes a third param to specify the behaviour if a  
file already exists.

However if I try to use that param I get a “wrong number of arguments (3  
for 2)” error.

My code:

=====================================================  
require ‘fileutils’  
require ‘zip/zip’  
require ‘zip/zipfilesystem’

# def unzip(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, proc { true }) } } end

What am I doing wrong?

---

<div class="post-metadata">

**Author:** ![jimboberlin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jimboberlin](https://www.ruby-forum.com/u/jimboberlin)\
**Post date:** [October 8, 2009, 7:59pm UTC](https://www.ruby-forum.com/t/zipfile-extract-overwrite-option/176947/2 "2009-10-08T19:59:36Z")

</div>

2009/10/8 Ingmar H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> # zip\_file.each { |f| f\_path=File.join(destination, f.name) FileUtils.mkdir\_p(File.dirname(f\_path)) zip\_file.extract(f, f\_path, proc { true }) } } end
> 
> What am I doing wrong?

Zipfile#extract’s third argument is prefixed with &, which means it  
expects to be called like so:

…  
zip\_file.extract(f, f\_path) { true }  
…

---

<div class="post-metadata">

**Author:** ![jimboberlin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jimboberlin](https://www.ruby-forum.com/u/jimboberlin)\
**Post date:** [October 9, 2009, 12:24pm UTC](https://www.ruby-forum.com/t/zipfile-extract-overwrite-option/176947/3 "2009-10-09T12:24:29Z")

</div>

> …  
> zip\_file.extract(f, f\_path) { true }  
> …

works like a charm!

Thanks a lot, I’ve been misguided by the only example of zip.extract  
with overwrite I found here:

> <https://stackoverflow.com/questions/966054/how-to-overwrite-existing-files-using-rubyzip-lib>

You’re my hero for the day Tom!  
Thanks again
