Zlib::GzipWriter#close error

G

Gavin Kistner

Why is Zlib::GzipWriter#close throwing an error here? The resulting
file comes out as desired.

For now I've just thrown a rescue on the IOError and I'm happy, but
(based on the example in the Zlib documentation) I don't see what's
wrong here.

[Slim:Sites/SVG/GFTraits] gavinkis% ls *.svgz
tcsh: ls: No match.

[Slim:Sites/SVG/GFTraits] gavinkis% tail -n 11 SocialNetworkSolver.rb
require 'erb'
require 'zlib'
File.open( $output_file, "w+" ){ |out_file|
gz = Zlib::GzipWriter.new( out_file )
File.open( $template_file, 'r' ){ |template|
gz.write( ERB.new( template.read ).result( binding ) )
}
gz.close
}
Stopwatch.mark( "Write SVGZ" )

[Slim:Sites/SVG/GFTraits] gavinkis% ruby SocialNetworkSolver.rb
Load file: 0.004s
Create springs: 0.003s
Create repulsors: 0.003s
Randomize placement: 0.001s
Run Simulation: 5.187s
Create SVG: 0.237s
SocialNetworkSolver.rb:251:in `close': closed stream (IOError)
from SocialNetworkSolver.rb:251:in `open'
from SocialNetworkSolver.rb:251

[Slim:Sites/SVG/GFTraits] gavinkis% ruby --version
ruby 1.8.2 (2004-12-25) [powerpc-darwin8.0.0]

[Slim:Sites/SVG/GFTraits] gavinkis% uname -a
Darwin Slim.local 8.2.0 Darwin Kernel Version 8.2.0: Fri Jun 24
17:46:54 PDT 2005; root:xnu-792.2.4.obj~3/RELEASE_PPC Power Macintosh
powerpc

[Slim:Sites/SVG/GFTraits] gavinkis% ls *.svgz
GFTraits.svgz
 
R

Robert Klemme

Gavin said:
Why is Zlib::GzipWriter#close throwing an error here? The resulting
file comes out as desired.

For now I've just thrown a rescue on the IOError and I'm happy, but
(based on the example in the Zlib documentation) I don't see what's
wrong here.

What happens if you uncomment the line gz.write...? As #result has access
to the current binding there's a chance that somewhere down the line
there's a gz.close() which would close the GzipWriter.

Other than that I don't have an idea either (apart from a bug).

robert
[Slim:Sites/SVG/GFTraits] gavinkis% ls *.svgz
tcsh: ls: No match.

[Slim:Sites/SVG/GFTraits] gavinkis% tail -n 11 SocialNetworkSolver.rb
require 'erb'
require 'zlib'
File.open( $output_file, "w+" ){ |out_file|
gz = Zlib::GzipWriter.new( out_file )
File.open( $template_file, 'r' ){ |template|
gz.write( ERB.new( template.read ).result( binding )
) }
gz.close
}
Stopwatch.mark( "Write SVGZ" )

[Slim:Sites/SVG/GFTraits] gavinkis% ruby SocialNetworkSolver.rb
Load file: 0.004s
Create springs: 0.003s
Create repulsors: 0.003s
Randomize placement: 0.001s
Run Simulation: 5.187s
Create SVG: 0.237s
SocialNetworkSolver.rb:251:in `close': closed stream (IOError)
from SocialNetworkSolver.rb:251:in `open'
from SocialNetworkSolver.rb:251

[Slim:Sites/SVG/GFTraits] gavinkis% ruby --version
ruby 1.8.2 (2004-12-25) [powerpc-darwin8.0.0]

[Slim:Sites/SVG/GFTraits] gavinkis% uname -a
Darwin Slim.local 8.2.0 Darwin Kernel Version 8.2.0: Fri Jun 24
17:46:54 PDT 2005; root:xnu-792.2.4.obj~3/RELEASE_PPC Power Macintosh
powerpc

[Slim:Sites/SVG/GFTraits] gavinkis% ls *.svgz
GFTraits.svgz
 
A

Austin Ziegler

Why is Zlib::GzipWriter#close throwing an error here? The resulting
file comes out as desired.

I don't think that it's GZipWriter#close with the error. I think it's
the block form of File.open that's doing a close on out_file, which
may be already closed because you used GzipWriter#close.

I had similar problems with Archive::Tar::Minitar. I think that
there's a different method for GzipWriter that will "finish" the
GzipWriter instance but not close the resulting stream.

(rummage)

Here it is. Change gz.close to gz.finish.

-------------------------------------------------- Zlib::GzipFile#finish
finish()
------------------------------------------------------------------------
Closes the GzipFile object. Unlike Zlib::GzipFile#close, this
method never calls the close method of the associated IO object.
Returns the associated IO object.

-austin
--=20
Austin Ziegler * (e-mail address removed)
* Alternate: (e-mail address removed)
 
R

Robert Klemme

Austin said:
I don't think that it's GZipWriter#close with the error. I think it's
the block form of File.open that's doing a close on out_file, which
may be already closed because you used GzipWriter#close.

I had similar problems with Archive::Tar::Minitar. I think that
there's a different method for GzipWriter that will "finish" the
GzipWriter instance but not close the resulting stream.

(rummage)

Here it is. Change gz.close to gz.finish.

--------------------------------------------------
Zlib::GzipFile#finish finish()
------------------------------------------------------------------------
Closes the GzipFile object. Unlike Zlib::GzipFile#close, this
method never calls the close method of the associated IO object.
Returns the associated IO object.

-austin

Maybe the block form of GzipWriter#open is a solution, too...

Zlib::GzipWriter.open('hoge.gz') do |gz|
gz.write 'jugemu jugemu gokou no surikire...'
endhttp://www.ruby-doc.org/stdlib/libdoc/zlib/rdoc/index.htmlKind regards
robert
 
G

Gavin Kistner

--Apple-Mail-1--837781006
Content-Transfer-Encoding: 7bit
Content-Type: text/plain;
charset=US-ASCII;
delsp=yes;
format=flowed

What happens if you uncomment the line gz.write...? As #result has
access
to the current binding there's a chance that somewhere down the line
there's a gz.close() which would close the GzipWriter.

The error still occurs if I comment the line gz.write. (It was
previously uncommented, so I assume that was a typo.)

--Apple-Mail-1--837781006--
 
G

Gavin Kistner

--Apple-Mail-1--749869132
Content-Transfer-Encoding: 7bit
Content-Type: text/plain;
charset=US-ASCII;
format=flowed

Here it is. Change gz.close to gz.finish.

That was it. Well done :)

Thanks.
--Apple-Mail-1--749869132--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Instance Eval of a Proc 10
irb/completion 3

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,152
Latest member
LorettaGur
Top