Search code examples
ruby-on-railsrubygithubweb-scrapingzip

download github zip file and display contents in web app


How would you display the contents of a github zip file and display the contents in a web application with the correct file structure?

Could I somehow insert the zip file into the database and then print the contents onto a webpage instead of scraping every single file?


Solution

  • Making the assumption that you are trying to build a view of the contents of the zipfile similar to Githubs representation, after uploading said zipfile to your server, the easiest way would be to:

    1. Use RubyZip Zip::ZipFile to access the uploaded Zipfile (how to store it as file db depends on your needs).
    2. Store the contents in a nested hash and display said hash in a view. The hash can then include drill down links to the extracted files.

    To give you a simple example stub how to parse the file (e.g. named test.zip)

    Zip::ZipFile.open("test.zip") do |zipfile|            
      zipfile.each do |entry|
        # Do something with the enty / create your hash like you need it
      end
    end
    

    You can find more details in the documentation.

    HTH