There are a lot of different implementations of folding for emacs. Some are specific to a particular programming language or syntax, others are more generic. Of the generic ones, it sounds like
yafolding is exactly what you're asking for. I tried it out, and while it feels somewhat slow, it does work.
Another I tried is called
origami, and I liked the results a lot better. It goes by indentation if that's all it knows about your file, but if it's one of a dozen or so supported languages then it uses syntax markers like curly braces to do the folding. It was also much quicker to fold an entire file. On the other hand, it apparently comes with no pre-configured keybindings, so to use it you'll have to set those up yourself (it does provide a keymap to add them to).
There are a number of others you might check out; run
M-x list-packages and then search for "fold".
Many people also use
outline-minor-mode to do folding and organization of code. It works off of embedded headline markers in comments rather than the structure of the code, but that's handy for many things as well. It also has the advantage of being built-in.