4

I am reading blob data from the database which is zipped(the contents of file is in CSV). The result of my variable looks like this:

irb(main):144:0> my_string => "\x1F\x8B\b\x00\xCB'\xFBY\x02\xFF\xC5R\xC1n\xDB0\f\xBD\xF7+\x04\xF46\xA8\x81d\xCB\x96}\f\x92\x15\xEB\x90\xE6\xB0\x04\xD8qPd&\xD1\x12K\xAE$w\xE8\xDF\x8F\x96\x9A\xA2\x87\x01\x03r\t\r\x8B|\x92H>>\xD\x7F\xC0\xCB\b\xE1\x03\x84\xC1\xD9\x00\x19=z\xD7o\xC1\xF7\x19m]\x8Aw*\xEA\xE3z\xECwX%ze\x83\xD2\xD18\xFB\xD4\x91\x88\xC7\xC6\xE2\xC5\xB7\x01\x88V\xBEKA\x88*\x8E\x81D\xD3c\x17\xD5\x0FD\xBD\x82W\aX\x1D\xB2\xD9\xAC\xDE!VM;\xB9\xFB\xD4*\xC1\xC1\xD8\xC3<\xD7\xC8\x04R\x9B\xDE \x01\xFCU\xEFF\x8B\xD3\x98a\x9E#\x0F\xFA\x9D\xED\xCEX\xD2+M,\xC4?\xCE\x9F\xC8\xD1\x85\xB8U\x87\xE41s\f\xE0\xD1\x81\xF7\xCFAG:\xD0gc\xA1#\xC6\xBE:\xA3\x013pc\xD2\xB5#\xBF\xDD\xE8q~rV\x16\xAC\xEA\xB3\b\xDF\xDC\xB9\x03\xBF\x9E 6@\xDEOK\xA2\xB4\x8E_m\xF48\xBD;\x81\xC5f\xFB\xA5\x8A\x8A\x84\xD1\xEB\xA3\xF28\xD8\xDE\x9D\x8DCxF0B\xA0\x0F\x1E\xA9\xA1\xA4\xC1DHBbt\xB0\xA8\xB7\x87\xAC\xFD\x05\xDD\xDDc\xE30\xD5\xE1\xB3\x8A\x143.f\xDB\x15\xBA\xA2\xBAc\xF4\xF2\xA5\xA5\x9C\xFE\xA6\x94\xAC\x15\x92I)(C\xFB\xB4

Now, I want to read this information in memory, unzip it and parse the csv file. I am using rubyzip and tried the following:

Zip::File.open_buffer(my_string) do |zip|
  zip.each do |entry|
    decompressed_data += entry.get_input_stream.read
  end
end

RuntimeError: Zip::ZipFile.open_buffer expects an argument of class String or IO. Found: String from (irb):224

Is there a way to retrieve the contents of the zip file?

Snake Sanders
  • 2,641
  • 2
  • 31
  • 42
Micheal
  • 2,272
  • 10
  • 49
  • 93

1 Answers1

2

i think that the main issue here is that the error-message is a bit misleading...

it should say expected filename or io.

in your code, you will need to wrap the content in a StringIO object so it can be treated as a stream.

since open_buffer is deprecated you should use open.

so it would look like open(StringIO.new(my_string)

phoet
  • 18,688
  • 4
  • 46
  • 74
  • I don't know which version @Michael uses but in 0.9.9 there is [following piece of code](https://github.com/rubyzip/rubyzip/blob/0.9.9/lib/zip/zip_file.rb#L110-L118) inside `open_buffer` which seems to produce this exactly message and it does not expect the String argument to be a file name. So there might be a quite different issue. – SergGr Nov 25 '17 at 14:36
  • Zip::File.open(StringIO.new(my_string)) do |zip| gives me: TypeError: no implicit conversion of StringIO into String – Micheal Nov 27 '17 at 16:09
  • what version of zip are you using? it looks like it's an old one – phoet Nov 29 '17 at 09:25