ZIP: Writing binary file fails

Hi,

Ruby version 1.8.2 (windows XP)
RubyZip version 0.5.11

I am trying to zip a binary file.
For example

Zip::ZipFile.open(target, Zip::ZipFile::CREATE) { |zipfile|
    zipfile.dir.mkdir('bin')
    zipfile.file.open('bin\eclipse.exe', 'w'){ |f|
      f.write(File.open('bin\eclipse.exe', 'rb').read)
    }
}

This zips the file but file has only first 163 bytes written (original file size ~100K)

What I am doing wrong?

Thank you

Dmytro Bablinyuk wrote:

Hi,

Ruby version 1.8.2 (windows XP)
RubyZip version 0.5.11

I am trying to zip a binary file.
For example

Zip::ZipFile.open(target, Zip::ZipFile::CREATE) { |zipfile|
   zipfile.dir.mkdir('bin')
   zipfile.file.open('bin\eclipse.exe', 'w'){ |f|
     f.write(File.open('bin\eclipse.exe', 'rb').read)
   }
}

This zips the file but file has only first 163 bytes written (original file size ~100K)

What I am doing wrong?

You are using backslashes in the zip-file, which you shouldn't do, but I doubt that is the cause of the problem. Other than that your code works fine on my linux box. I'll try it in windows when I get home. I suspect it is a bug in rubyzip.

Thomas

Dmytro Bablinyuk wrote:

I am trying to zip a binary file.
For example

Zip::ZipFile.open(target, Zip::ZipFile::CREATE) { |zipfile|
   zipfile.dir.mkdir('bin')
   zipfile.file.open('bin\eclipse.exe', 'w'){ |f|
     f.write(File.open('bin\eclipse.exe', 'rb').read)
   }
}

This zips the file but file has only first 163 bytes written (original file size ~100K)

What I am doing wrong?

Nothing. You found yet another bug in rubyzip - this time Windows specific, so I'm semi-excused :-). I have fixed it and put up rubyzip-0.5.12.

gem install rubyzip

or get it from http://rubyforge.org/projects/rubyzip.

Thomas

Nothing. You found yet another bug in rubyzip - this time Windows specific, so I'm semi-excused :-). I have fixed it and put up rubyzip-0.5.12.

gem install rubyzip

or get it from http://rubyforge.org/projects/rubyzip.

Wow, it's really great and really quick fix!
Thank you very much Thomas for your work - it's working now!

As for path separator I am using File::SEPARATOR if this is ok. e.g.
zipfile.file.open('bin' + File::SEPARATOR + 'eclipse.exe', 'w'){ |f|
..

Dimitry

Dmytro Bablinyuk wrote:

As for path separator I am using File::SEPARATOR if this is ok. e.g.
zipfile.file.open('bin' + File::SEPARATOR + 'eclipse.exe', 'w'){ |f|

It's only okay if File::SEPARATOR is a forward slash. The zip spec says that the path separator is a forward slash. Perhaps I should define Zip::SEPARATOR='/' or just make sure to substitute all copies of File::SEPARATOR with '/'.

Thomas

Dmytro Bablinyuk wrote:

As for path separator I am using File::SEPARATOR if this is ok. e.g.
zipfile.file.open('bin' + File::SEPARATOR + 'eclipse.exe', 'w'){ |f|
..

Dimitry

I may be wrong, but I believe you can just use forward slashes and ruby will handle it properly regardless of platform. Of course if you are generating paths for consumption outside of ruby then File::SEPARATOR is the ticket...

Cheers,
Tim

Tim Ferrell <Tim.Ferrell@s0nspark.com> writes:

Dmytro Bablinyuk wrote:

As for path separator I am using File::SEPARATOR if this is ok. e.g.
zipfile.file.open('bin' + File::SEPARATOR + 'eclipse.exe', 'w'){ |f|
..

Dimitry

I may be wrong, but I believe you can just use forward slashes and
ruby will handle it properly regardless of platform. Of course if you
are generating paths for consumption outside of ruby then
File::SEPARATOR is the ticket...

Even better: File.join("bin", "eclipse.exe")

(And with "eclipse.exe", you can as well hardcode the /...)

···

Cheers,
Tim

--
Christian Neukirchen <chneukirchen@gmail.com> http://chneukirchen.org