Note that there are some explanatory texts on larger screens.

plurals
  1. PORuby zlib deflate and inflate not working as intended
    primarykey
    data
    text
    <p>I have been trying to get a (relatively) simple compression and decompression functionality working on any sort of object (images, csv files, docs etc) and have had no success whatsoever. </p> <p>I have tried many things and currently I think this code is close to it (not my code) however I have been having trouble getting it to work.</p> <p>Hoge.txt content:</p> <pre><code>blahblah123 </code></pre> <p>Ruby deflate class titled rubyfiletest.rb:</p> <pre><code>require "zlib" file_name = "hoge.txt" compressed_file = File.open(file_name +".gz", "w+") zd = Zlib::Deflate.new(Zlib::BEST_COMPRESSION, 15, Zlib::MAX_MEM_LEVEL, Zlib::HUFFMAN_ONLY) compressed_file &lt;&lt; zd.deflate(File.read(file_name)) compressed_file.close zd.close </code></pre> <p>So with this code what it does is create a .gz file entitled hoge.txt.gz which generates this in the command prompt </p> <pre><code>rubyfiletest.rb:9:in `read': No such file or directory - hoge.txt (Errno::ENOEN) </code></pre> <p>Also if I try to open the newly created file with winrar/7zip it will give me this error:</p> <pre><code>The archive is either in unknown format or damaged. </code></pre> <p>Ruby inflate class titled rubyinflate.rb:</p> <pre><code>require 'zlib' File.open('hoge.txt.gz', 'rb') do |compressed| File.open('hoge.txt', 'w') do |decompressed| gz = Zlib::GzipReader.new(compressed) result = gz.read decompressed.write(result) gz.close end end </code></pre> <p>I am guessing the compression is not working correctly since I cannot even open the compressed file and have been having so many problems with this. I think it is worth while to mention I am new to ruby. I also would like to know if the way this is implemented would enable images and all types of files to be compressed/decompressed fine.</p> <p><strong>* Update *</strong></p> <p>I was getting the error for wrong path because hoge was saved as hoge.txt.txt (doh!) and it was looking for hoge.txt in that path. So now I changed the ruby compression code a little so that the output creates a valid gzip file as pointed out by Matthew T. </p> <p>This is what it looks like now</p> <pre><code>require "zlib" file_name = "hoge.txt" compressed_file = File.open(file_name +".gz", "w+b") zd = Zlib::GzipWriter.new(compressed_file) zd &lt;&lt; File.read(file_name) compressed_file.close zd.close </code></pre> <p>With this as the new rubyfiletest.rb class to decompress using GzipWriter I receive this error when I run this class:</p> <pre><code>rubyfiletest.rb:8 in 'write': closed stream (IOError) </code></pre> <p>This will still generate the .gz file (and I can now open it in winrar/7zip without any problems however it is empty, should contain 'blahblah123'). After I compress it and run the rubyinflate.rb I receive this error from rubyinflate.rb:</p> <pre><code>rubyinflate.rb:6 in 'read': unecpected end of file (Zlib::GzipFile::Error) </code></pre> <p>I think these errors may be causing the data to not be copied over? If I used an image would that data be copied over assuming this code worked?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload