Zlib::GzipWriter#close error

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

Gavin Kistner wrote:

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

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()

···

On 7/14/05, Gavin Kistner <gavin@refinery.com> wrote:

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

------------------------------------------------------------------------
     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
--
Austin Ziegler * halostatue@gmail.com
               * Alternate: austin@halostatue.ca

Austin Ziegler wrote:

···

On 7/14/05, Gavin Kistner <gavin@refinery.com> wrote:

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

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

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

···

On Jul 14, 2005, at 7:45 AM, Robert Klemme wrote:

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.

That was it. Well done :slight_smile:

Thanks.

···

On Jul 14, 2005, at 11:21 AM, Austin Ziegler wrote:

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