Discussion:
ZIP: Writing binary file fails
Dmytro Bablinyuk
2005-09-06 06:22:20 UTC
Permalink
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
Thomas Sondergaard
2005-09-06 08:51:27 UTC
Permalink
Post by Dmytro Bablinyuk
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
Thomas Sondergaard
2005-09-06 19:36:28 UTC
Permalink
Post by Dmytro Bablinyuk
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
Dmytro Bablinyuk
2005-09-06 23:02:24 UTC
Permalink
Post by Thomas Sondergaard
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
Thomas Sondergaard
2005-09-07 07:06:30 UTC
Permalink
Post by Dmytro Bablinyuk
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
Tim Ferrell
2005-09-07 19:53:04 UTC
Permalink
Post by Dmytro Bablinyuk
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
Christian Neukirchen
2005-09-07 20:06:44 UTC
Permalink
Post by Tim Ferrell
Post by Dmytro Bablinyuk
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 /...)
Post by Tim Ferrell
Cheers,
Tim
--
Christian Neukirchen <***@gmail.com> http://chneukirchen.org
Continue reading on narkive:
Loading...