'How can I avoid "Zip end of central directory signature not found (Zip::Error)" with rubyzip?

I'm reading a lot of zip file with rubyzip.

However this error message is always showing in only specific file even it is zip file.

/app/vendor/bundle/ruby/2.3.0/gems/rubyzip-1.2.1/lib/zip/central_directory.rb:143:in `get_e_o_c_d': Zip end of central directory signature not found (Zip::Error)

I guess this error occures in rubyzip.

How can I manage this error?

Here is my code.

url = 'http://example.zip'

zipfilename = open(url)

Zip::File.open(zipfilename, :allow_redirections => :all) do |zip_file|

   entry = zip_file.glob("*ixbrl.htm").first

   stream = entry.get_input_stream.read

   puts stream

end

Thank you!



Solution 1:[1]

I faced this error when I try to extract data from a uploaded .xlsx file in my application. On my context, the .xlsx file was corrupted, so my solution was save the content (I usually handle the buffer file after the upload) as a csv file (in my application I dont need to worry about the file extension), fixing the content by force it to encode as utf-8, and extract it's data after it. here is a example of the code, I'm using roo-xls gem to handle .xls files and roo gem to handle .csv and .xlsx files.

Solution 2:[2]

I ran into the same error. Also only reproducible on Heroku. The error was fixed after I added an unzip buildpack (second, after Ruby).

https://github.com/davidlibrera/heroku-buildpack-unzip

Solution 3:[3]

If you're using a docs xlsx file try checking if the access of the sheet link is public.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Matheus Porto
Solution 2 chrisM
Solution 3 Hariss Mansoor