# Creating a zip file using rubyzip

**URL:** <https://www.ruby-forum.com/t/creating-a-zip-file-using-rubyzip/168523>\
**Category:** Ruby\
**Created:** [May 21, 2009, 8:28pm UTC](https://www.ruby-forum.com/t/creating-a-zip-file-using-rubyzip/168523 "2009-05-21T20:28:36Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![raimon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/59ef9b/32.png) [@raimon](https://www.ruby-forum.com/u/raimon)\
**Post date:** [May 21, 2009, 8:28pm UTC](https://www.ruby-forum.com/t/creating-a-zip-file-using-rubyzip/168523/1 "2009-05-21T20:28:36Z")

</div>

I’m trying to create a zip file, but I’m getting an error saying the  
file doesn’t exist, but it exists, as shown in the code.

Any clues ?

I think the permissions are OK, as everyboday can read it …

maybe there’s another way to create a zip file …

thanks,

r.

require “rubygems”  
require ‘zip/zip’  
require ‘zip/zipfilesystem’

file\_path =  
“/Users/montx/Documents/Ruby/codi/scanns/download/bs\_promotors/masivo\_prestamos\_20090521.txt”  
file\_zip="/Users/montx/Documents/Ruby/codi/scanns/download/bs\_promotors/masivo\_prestamos\_20090521.zip"

EXAMPLE\_ZIP=file\_zip  
Zip::ZipFile.open(EXAMPLE\_ZIP, Zip::ZipFile::CREATE) { |zf|

puts “File ready to zip” if File.exists?(file\_path)

puts zf.file.read(file\_path)  
}

output:  
File ready to zip  
Errno::ENOENT: No such file or directory -  
Users/montx/Documents/Ruby/codi/scanns/download/bs\_promotors/masivo\_prestamos\_20090521.txt

method get\_entry in zip.rb at line 1515  
method get\_input\_stream in zip.rb at line 1411  
method read in zip.rb at line 1435  
method read in zipfilesystem.rb at line 562  
method read in zipfilesystem.rb at line 383  
at top level in untitled at line 16  
method open in zip.rb at line 1382  
at top level in untitled at line 11  
copy output  
Program exited with code #1 after 0.16 seconds.

---

<div class="post-metadata">

**Author:** ![raimon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/59ef9b/32.png) [@raimon](https://www.ruby-forum.com/u/raimon)\
**Post date:** [May 21, 2009, 9:49pm UTC](https://www.ruby-forum.com/t/creating-a-zip-file-using-rubyzip/168523/2 "2009-05-21T21:49:20Z")

</div>

Raimon Fs wrote:

> puts zf.file.read(file\_path)

This is asking to read a file out of the zip file - and as it says,  
there is no such file, as you’ve only just created the empty zip file.

---

<div class="post-metadata">

**Author:** ![raimon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/59ef9b/32.png) [@raimon](https://www.ruby-forum.com/u/raimon)\
**Post date:** [May 21, 2009, 11:31pm UTC](https://www.ruby-forum.com/t/creating-a-zip-file-using-rubyzip/168523/3 "2009-05-21T23:31:09Z")

</div>

Brian C. wrote:

> Raimon Fs wrote:
> 
> > puts zf.file.read(file\_path)
> 
> This is asking to read a file out of the zip file - and as it says,  
> there is no such file, as you’ve only just created the empty zip file.

yes, you’re right, I thought it was reading the file and at the same  
time adding it …

In the documentation I have found .add

add(entry, srcPath, &continueOnExistsProc)

and now works …

thanks,

r.
