0

I wanted to write a ruby program to unpackage tar.gz files, and I ran into some issues. After reading the documentation on the ruby-doc site, Zlib::GzipReader and Zlib::Inflate. Then I found this Module someone wrote on GitHub, and that didn't work either. So, using the examples from the Ruby page for Zlib::GzipReader, I these were able to run successfully.

irb(main):027:0* File.open('zlib_inflate.tar.gz') do |f|
irb(main):028:1* gz = Zlib::GzipReader.new(f)
irb(main):029:1> print gz.read
irb(main):030:1> gz.close
irb(main):031:1> end
#
#
#
irb(main):023:0* Zlib::GzipReader.open('zlib_inflate.tar.gz') { |gz|
irb(main):024:1* print gz.read
irb(main):025:1> }

Then, when trying to use the Zlib::Inflate options, I kept running into incorrect header check errors.

irb(main):047:0* zstream = Zlib::Inflate.new
=> #<Zlib::Inflate:0x00000002b15790 @dictionaries={}>
irb(main):048:0> buf = zstream.inflate('zlib_inflate.tar.gz')
Zlib::DataError: incorrect header check
        from (irb):48:in `inflate'
        from (irb):48
        from C:/ruby/Ruby200p353/Ruby200-x64/bin/irb:12:in `<main>'
#
#
#
irb(main):049:0> cf = File.open("zlib_inflate.tar.gz")
=> #<File:zlib_inflate.tar.gz>
irb(main):050:0> ucf = File.open("ruby_inflate.rb", "w+")
=> #<File:ruby_inflate.rb>
irb(main):051:0> zi = Zlib::Inflate.new(Zlib::MAX_WBITS)
=> #<Zlib::Inflate:0x00000002c097f0 @dictionaries={}>
irb(main):052:0> ucf << zi.inflate(cf.read)
Zlib::DataError: incorrect header check
        from (irb):52:in `inflate'
        from (irb):52
        from C:/ruby/Ruby200p353/Ruby200-x64/bin/irb:12:in `<main>'
#
#
#
irb(main):057:0* File.open("zlib_inflate.tar.gz") {|cf|
irb(main):058:1* zi = Zlib::Inflate.new
irb(main):059:1> File.open("zlib_inflate.rb", "w+") {|ucf|
irb(main):060:2* ucf << zi.inflate(cf.read)
irb(main):061:2> }
irb(main):062:1> zi.close
irb(main):063:1> }
Zlib::DataError: incorrect header check
        from (irb):60:in `inflate'
        from (irb):60:in `block (2 levels) in irb_binding'
        from (irb):59:in `open'
        from (irb):59:in `block in irb_binding'
        from (irb):57:in `open'
        from (irb):57
        from C:/ruby/Ruby200p353/Ruby200-x64/bin/irb:12:in `<main>'

How would I go about taking a tar.gz file, and extract the contents so the files are not null? I did read this other post about Ruby Zlib from here, but that did not work for me as well. What am I doing wrong?

Community
  • 1
  • 1
FilBot3
  • 3,460
  • 6
  • 33
  • 55
  • I'm not quite sure what you're trying to do, but gzip and inflate are not the same thing (gzip uses inflate but adds various bits of header data if my memory is correct) – Frederick Cheung Jun 02 '14 at 15:38
  • I didn't know that. I'm trying to extract the contents of the tar.gz file into the current folder. I know that the GzipReader just puts teh contents into memory, but I don't know how to just get it to extract the files. – FilBot3 Jun 02 '14 at 16:38
  • Does this answer your question? http://stackoverflow.com/a/19139114/1089267 – thinkOfaNumber Jul 01 '14 at 02:40

0 Answers0