Newbie trying to zip all .dat files in directory

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

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|
  Dir["*.dat"].each do |file_name|
    zfile.add(file_name, file_name)
  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)
        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|
  Dir["*.dat"].each do |file_name|
    zos.put_next_entry(file_name)
    File.open(file_name,'rb') do |f|
    zos.write(f.read)
  end
  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.

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

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

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.

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

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 :slight_smile:

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