# Adding Zlib::GzipFile#path

**URL:** <https://www.ruby-forum.com/t/adding-zlib-gzipfile-path/164555>\
**Category:** Development\
**Created:** [April 2, 2009, 3:09pm UTC](https://www.ruby-forum.com/t/adding-zlib-gzipfile-path/164555 "2009-04-02T15:09:07Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Akinori\_MUSHA](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/b782af/32.png) [@Akinori\_MUSHA](https://www.ruby-forum.com/u/Akinori_MUSHA)\
**Post date:** [April 2, 2009, 3:09pm UTC](https://www.ruby-forum.com/t/adding-zlib-gzipfile-path/164555/1 "2009-04-02T15:09:07Z")

</div>

ã€€Zlib::GzipFile#path ã‚’è¿½åŠ ã™ã‚‹ãƒ‘ãƒƒãƒã§ã™ã€‚zgrep ã¿ãŸã„ãªã®ã‚’  
æ›¸ãã¨ãã«ä½¿ãˆã¾ã™ã€‚

ã€€ãƒ†ã‚¹ãƒˆã®å¾ŒåŠã¯ StringIO#path ã®å‰Šé™¤ãŒå‰æãªã®ã§ã€ãã¡ã‚‰ã®å‹•å‘  
æ¬¡ç¬¬ã§å‰Šã‚‹ã‹å·®ã—æ›¿ãˆã¾ã™ã€‚

```
* * *

```

ã€€ã¨ã“ã‚ã§ã€ Document-method: path ã‚’æ›¸ã„ã¦ã„ã‚‹ã®ã« rdoc/ri ã«  
ç¾ã‚Œãªã„ã®ã¯ä»•æ§˜ãªã‚“ã§ã—ã‚‡ã†ã‹â€¦ã€‚

–  
Akinori MUSHA / [http://akinori.org/](http://akinori.org/)

# Index: ChangeLog

— ChangeLog (revision 23123)  
+++ ChangeLog (working copy)  
@@ -1,3 +1,8 @@  
+Thu Apr 2 21:53:10 2009 Akinori MUSHA [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
+

- 
  - ext/zlib/zlib.c: Define Zlib::GzipFile#path if the associated

- IO-like object responds to #path.
- 

Thu Apr 2 14:50:06 2009 Yukihiro M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

- lib/getoptlong.rb: remove unused udoc/usage from example.  
Index: NEWS  
===================================================================  
— NEWS (revision 23123)  
+++ NEWS (working copy)  
@@ -177,6 +177,10 @@ with all sufficient information, see the
  - Etc::Passwd.each
  - Etc::Group.each

+\* zlib

- 
  - new methods:

- 
  - Zlib::GzipFile#path

- 

=== Compatibility issues (excluding feature bug fixes)

- Enumerator#rewind  
Index: ext/zlib/zlib.c  
===================================================================  
— ext/zlib/zlib.c (revision 23123)  
+++ ext/zlib/zlib.c (working copy)  
@@ -163,6 +163,7 @@ static VALUE rb\_gzfile\_sync(VALUE);  
static VALUE rb\_gzfile\_set\_sync(VALUE, VALUE);  
static VALUE rb\_gzfile\_total\_in(VALUE);  
static VALUE rb\_gzfile\_total\_out(VALUE);  
+static VALUE rb\_gzfile\_path(VALUE);

static VALUE rb\_gzwriter\_s\_allocate(VALUE);  
static VALUE rb\_gzwriter\_s\_open(int, VALUE\*, VALUE);  
@@ -1653,7 +1654,7 @@ rb\_inflate\_set\_dictionary(VALUE obj, VAL  
#define OS\_CODE OS\_UNIX  
#endif

-static ID id\_write, id\_read, id\_readpartial, id\_flush, id\_seek,  
id\_close;  
+static ID id\_write, id\_read, id\_readpartial, id\_flush, id\_seek,  
id\_close, id\_path;  
static VALUE cGzError, cNoFooter, cCRCError, cLengthError;

@@ -1678,6 +1679,7 @@ struct gzfile {  
int ecflags;  
VALUE ecopts;  
char \*cbuf;

- VALUE path;  
};  
#define GZFILE\_CBUF\_CAPA 10

@@ -1699,6 +1701,7 @@ gzfile\_mark(struct gzfile \*gz)  
rb\_gc\_mark(gz-\>comment);  
zstream\_mark(&gz-\>z);  
rb\_gc\_mark(gz-\>ecopts);

- rb\_gc\_mark(gz-\>path);  
}

static void  
@@ -1745,6 +1748,7 @@ gzfile\_new(klass, funcs, endfunc)  
gz-\>ecflags = 0;  
gz-\>ecopts = Qnil;  
gz-\>cbuf = 0;

- gz-\>path = Qnil;

+/\*

- 
  - Document-method: path

- 
  - 

- 
  - call-seq: path

- 
  - 

- 
  - Returns the path string of the associated IO-like object. This

- 
  - method is only defined when the IO-like object responds to #path().

- \*/  
+static VALUE  
+rb\_gzfile\_path(VALUE obj)  
+{
- struct gzfile \*gz;
- Data\_Get\_Struct(obj, struct gzfile, gz);
- return gz-\>path;  
+}

static void  
rb\_gzfile\_ecopts(struct gzfile \*gz, VALUE opts)  
@@ -2770,6 +2789,11 @@ rb\_gzwriter\_initialize(int argc, VALUE \*  
ZSTREAM\_READY(&gz-\>z);  
rb\_gzfile\_ecopts(gz, opt);

- if (rb\_respond\_to(io, id\_path)) {
- gz-\>path = rb\_funcall(gz-\>io, id\_path, 0);
- rb\_define\_singleton\_method(obj, “path”, rb\_gzfile\_path, 0);
- }
- return obj;  
}

@@ -2965,6 +2989,11 @@ rb\_gzreader\_initialize(int argc, VALUE \*  
gzfile\_read\_header(gz);  
rb\_gzfile\_ecopts(gz, opt);

- if (rb\_respond\_to(io, id\_path)) {
- gz-\>path = rb\_funcall(gz-\>io, id\_path, 0);
- rb\_define\_singleton\_method(obj, “path”, rb\_gzfile\_path, 0);
- }
- return obj;  
}

@@ -3516,6 +3545,7 @@ Init\_zlib()  
id\_flush = rb\_intern(“flush”);  
id\_seek = rb\_intern(“seek”);  
id\_close = rb\_intern(“close”);

- id\_path = rb\_intern(“path”);

±-- Zlib::GzipFile#path  
+

- Returns the path string of the associated IO-like object. This
- method is only defined when the IO-like object responds to
- #path().
- 

== Zlib::GzipFile::Error

# Index: test/zlib/test\_zlib.rb

— test/zlib/test\_zlib.rb (revision 23123)  
+++ test/zlib/test\_zlib.rb (working copy)  
@@ -363,6 +363,34 @@ if defined? Zlib  
assert\_equal(3, gz.tell)  
end  
end  
+

- def test\_path
- 

```
 t = Tempfile.new("test_zlib_gzip_file")

```

- 

```
 t.close

```

- 
- 

```
 gz = Zlib::GzipWriter.open(t.path)

```

- 

```
 gz.print("foo")

```

- 

```
 assert_equal(t.path, gz.path)

```

- 

```
 gz.close

```

- 

```
 assert_equal(t.path, gz.path)

```

- 
- 

```
 f = Zlib::GzipReader.open(t.path)

```

- 

```
 assert_equal(t.path, f.path)

```

- 

```
 f.close

```

- 

```
 assert_equal(t.path, f.path)

```

- 
- 

```
 s = ""

```

- 

```
 sio = StringIO.new(s)

```

- 

```
 gz = Zlib::GzipWriter.new(sio)

```

- 

```
 gz.print("foo")

```

- 

```
 assert_raise(NoMethodError) { gz.path }

```

- 

```
 gz.close

```

- 
- 

```
 sio = StringIO.new(s)

```

- 

```
 f = Zlib::GzipReader.new(sio)

```

- 

```
 assert_raise(NoMethodError) { f.path }

```

- 

```
 f.close

```

- end  
end

class TestZlibGzipReader \< Test::Unit::TestCase
