If you have its path, dpkg already has an option to do that: "dpkg -S". Although some extra logic is needed for symlinks.