# Newbie trying to zip all .dat files in directory

**URL:** <https://rubytalk.org/t/newbie-trying-to-zip-all-dat-files-in-directory/20180>\
**Category:** ruby-talk\
**Created:** [16 August 2005 00:41 UTC](https://rubytalk.org/t/newbie-trying-to-zip-all-dat-files-in-directory/20180 "2005-08-16T00:41:26Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Len\_Sumnler](https://avatars.discourse-cdn.com/v4/letter/l/aeb1de/32.png) [@Len\_Sumnler](https://rubytalk.org/u/Len_Sumnler)\
**Post date:** [16 August 2005 00:41 UTC](https://rubytalk.org/t/newbie-trying-to-zip-all-dat-files-in-directory/20180/1 "2005-08-16T00:41:26Z")

</div>

Hello all.

I am running Ruby 1.8.2 on XP professional. Have installed RubyZip  
through gems.

I have tried to create a zip file containing all of the .dat file in a  
particular directory. It appears that I only get the last .dat file  
ziped. Can anyone point me in the right direction. Please remember I  
have just started to learn Ruby but am trying to create some small but  
usefull functions. I looked through the news group for similar post  
and tried googling for additional documentation on the RubyZip and seem  
to have come up short.

Len Sumnler

---

<div class="post-metadata">

**Author:** ![Wilson\_Bilkovich](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/wilson_bilkovich/32/8247_2.png) [@Wilson\_Bilkovich](https://rubytalk.org/u/Wilson_Bilkovich)\
**Post date:** [16 August 2005 03:14 UTC](https://rubytalk.org/t/newbie-trying-to-zip-all-dat-files-in-directory/20180/2 "2005-08-16T03:14:00Z")

</div>

Well, I was just going to bang this one out, and help you out, but  
rubyzip is pretty odd.  
It's the most Java-like Ruby program I have seen. Looking at the docs  
and the source code, I've just written a number of test programs,  
starting with this:

require 'rubygems'  
require 'zip/zip'  
Zip::ZipFile.open('example.zip',Zip::ZipFile::CREATE) do |zfile|  
&nbsp;&nbsp;Dir["\*.dat"].each do |file\_name|  
&nbsp;&nbsp;&nbsp;&nbsp;zfile.add(file\_name, file\_name)  
&nbsp;&nbsp;end  
end

However, that gives me:  
c:/ruby/lib/ruby/1.8/delegate.rb:105:in `method\_missing': undefined  
method `copy\_stream' for #\<Zip::ZipStreamableFile:0x2cbdeb8\>  
(NoMethodError)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from c:/ruby/lib/ruby/gems/1.8/gems/rubyzip-0.5.9/lib/zip/zip.rb:1282:in  
`write\_to\_zip\_output\_stream'

copy\_stream comes from "fileutils.rb", and I get that error even if I  
include it and edit the rubyzip source to make an explicit call to it.

So.. Blocked by that, I found another mailing list posting which got  
me headed in the right direction.. This now works for me. (Note the  
'b' as part of the file mode. Nothing worked until I set it to binary  
mode, at least on Windows XP)

require 'zip/zip'  
Zip::ZipOutputStream.open("example.zip") do |zos|  
&nbsp;&nbsp;Dir["\*.dat"].each do |file\_name|  
&nbsp;&nbsp;&nbsp;&nbsp;zos.put\_next\_entry(file\_name)  
&nbsp;&nbsp;&nbsp;&nbsp;File.open(file\_name,'rb') do |f|  
&nbsp;&nbsp;&nbsp;&nbsp;zos.write(f.read)  
&nbsp;&nbsp;end  
&nbsp;&nbsp;end  
end

Short answer: Someone needs to point out another zip library for Ruby. Heh.

> **···**
>
> On 8/15/05, len \<lsumnler@gmail.com\> wrote:
> 
> > Hello all.
> > 
> > I am running Ruby 1.8.2 on XP professional. Have installed RubyZip  
> > through gems.
> > 
> > I have tried to create a zip file containing all of the .dat file in a  
> > particular directory. It appears that I only get the last .dat file  
> > ziped. Can anyone point me in the right direction. Please remember I  
> > have just started to learn Ruby but am trying to create some small but  
> > usefull functions. I looked through the news group for similar post  
> > and tried googling for additional documentation on the RubyZip and seem  
> > to have come up short.

---

<div class="post-metadata">

**Author:** ![Len\_Sumnler](https://avatars.discourse-cdn.com/v4/letter/l/aeb1de/32.png) [@Len\_Sumnler](https://rubytalk.org/u/Len_Sumnler)\
**Post date:** [16 August 2005 03:51 UTC](https://rubytalk.org/t/newbie-trying-to-zip-all-dat-files-in-directory/20180/3 "2005-08-16T03:51:14Z")

</div>

Thanks

I really do appreciate all of the work you did. I'm new to Ruby and in  
many respects to this whole OO thing. I will study and use the code  
you supplied.

Quick question, I was kind of doing the same thing but I did not prefix  
the first line with Zip:: --- what is the significance.

Len Sumnler

---

<div class="post-metadata">

**Author:** ![dave](https://avatars.discourse-cdn.com/v4/letter/d/ee7513/32.png) [@dave](https://rubytalk.org/u/dave)\
**Post date:** [16 August 2005 08:50 UTC](https://rubytalk.org/t/newbie-trying-to-zip-all-dat-files-in-directory/20180/4 "2005-08-16T08:50:42Z")

</div>

> Quick question, I was kind of doing the same thing but I did not prefix  
> the first line with Zip:: --- what is the significance.

It's the Zip module namespace.

If you write a program, Test, a class such as Settings can cause problems.

Using a module:

module Test  
&nbsp;&nbsp;class Settings  
&nbsp;&nbsp;end  
end

Test::Settings will not cause definition problems with other programs that  
use the same class name: Settings.

> **···**
>
> --
> 
> > here are more things in heaven and earth,
> 
> horatio, than are dreamt of in your philosophy.

---

<div class="post-metadata">

**Author:** ![Wilson\_Bilkovich](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/wilson_bilkovich/32/8247_2.png) [@Wilson\_Bilkovich](https://rubytalk.org/u/Wilson_Bilkovich)\
**Post date:** [16 August 2005 12:41 UTC](https://rubytalk.org/t/newbie-trying-to-zip-all-dat-files-in-directory/20180/5 "2005-08-16T12:41:09Z")

</div>

I was thinking about my message, and realized I had perhaps come  
across as harsh on RubyZip. I wouldn't want the author to read my  
post and come slash my tires or anything.

I just meant that it is (in its own words, as well) a direct port of  
the Java zip library.  
As such, it's very much not idiomatic Ruby. I just wanted to make  
sure you realized that as you studied it. Don't try to make your code  
look like this, unless that makes you happy.

Speaking of which.. does anyone else know what's up with the  
copy\_stream problem I ran into on my first try? I've never used that  
method before, so I'm not sure exactly how it should work.

> **···**
>
> On 8/15/05, len \<lsumnler@gmail.com\> wrote:
> 
> > Thanks
> > 
> > I really do appreciate all of the work you did. I'm new to Ruby and in  
> > many respects to this whole OO thing. I will study and use the code  
> > you supplied.
> > 
> > Quick question, I was kind of doing the same thing but I did not prefix  
> > the first line with Zip:: --- what is the significance.

---

<div class="post-metadata">

**Author:** ![Chris6](https://avatars.discourse-cdn.com/v4/letter/c/b2d939/32.png) [@Chris6](https://rubytalk.org/u/Chris6)\
**Post date:** [16 August 2005 18:16 UTC](https://rubytalk.org/t/newbie-trying-to-zip-all-dat-files-in-directory/20180/6 "2005-08-16T18:16:15Z")

</div>

I am able to run the original code on Win2K with ruby 1.8.2  
(2004-12-25) [i386-mswin32] and RubyZip-0.5.8

Possibly a bug introduced in RubyZip-0.5.9?

Cheers

---

<div class="post-metadata">

**Author:** ![Thomas\_Sondergaard4](https://avatars.discourse-cdn.com/v4/letter/t/8491ac/32.png) [@Thomas\_Sondergaard4](https://rubytalk.org/u/Thomas_Sondergaard4)\
**Post date:** [29 August 2005 14:26 UTC](https://rubytalk.org/t/newbie-trying-to-zip-all-dat-files-in-directory/20180/7 "2005-08-29T14:26:22Z")

</div>

Wilson Bilkovich wote:

> I was thinking about my message, and realized I had perhaps come  
> across as harsh on RubyZip. I wouldn't want the author to read my  
> post and come slash my tires or anything.

I wouldn't think of it. besides I don't know where you live 🙂

> I just meant that it is (in its own words, as well) a direct port of  
> the Java zip library.  
> As such, it's very much not idiomatic Ruby. I just wanted to make  
> sure you realized that as you studied it. Don't try to make your code  
> look like this, unless that makes you happy.

It is certainly not a direct port, as I have never seen the source code  
to java's ZipFile/ZipInputStream. I wrote rubyzip to learn ruby and I  
have tried to make it ruby-like. For instance the stream-objects are  
IO-like and there is an API that let you access the contents of a zip  
file with an interface similar to that provided by ruby's File and Dir  
classes  
([http://rubyzip.sourceforge.net/classes/Zip/ZipFileSystem.html\](http://rubyzip.sourceforge.net/classes/Zip/ZipFileSystem.html%5C)). The  
idea is of course to minimize the amount of API you have to learn. I've  
never used rubyzip myself, it's just a pet project for playing with  
ruby. If you have suggestions for (API) improvements I'd be happy to  
hear about them.

> Speaking of which.. does anyone else know what's up with the  
> copy\_stream problem I ran into on my first try? I've never used that  
> method before, so I'm not sure exactly how it should work.

0.5.9 is broken - unfortunately I released it just before I went on  
vacation. 0.5.10 will be released shortly, until then 0.5.8 works.

br,

Thomas
