Search code examples
.netsfx

How to extract specific files from the same zip file as the executable


I am aware of a few libraries that allow me to extract files from a zip (DotnetZip, SharpZipLib, etc), however is it possible for the executable doing the extracting to be in the same zip file it is extracting specific files from?

Here is my vision:

1) User double clicks a zip file to view its contents. 2) Double clicks an exe 3) That exe extracts a few of the files from the zip it is contained in, into a specified directory 4) This directory now has the desired files. 5) An exe in this directory is run.

I know, this sounds a lot like existing SFX solutions. However, none of these seem to be able to meet my needs described in this question: How can I build a .Net application which is distributed as a single and self-contained file/entity (no installer)?

I know these requirements sound bizarre. If I could change the Powers That Be, I would just ship an MSI, and an XML file or two for configurations.

Of course, I welcome critical feedback and questions.


Solution

  • You really can't do this unless you control the process from end-to-end.

    When you double-click a zip file in Windows (since XP), it will show you the contents. When you double click a file in the zip, it copies that file to a temp directory and then your file is executed (or the appropriate shell handler is applied to the file) from there.

    When this happens, your executable (or the executable handling your file type) doesn't get any special parameters/environment variables/indicators that say "hey, I was in a zip", as far as your file is concerned, you're just in another directory.

    Additionally, the shell isn't the only program that can handle the .zip file type; there are plenty of utilities that can, and they will all approach handling double clicking on a file in the zip differently.

    Granted, they might all use a temp directory to copy the file to, but there's nothing that forces them (by convention or through an API) to do it the same way.

    If you're going to do this, then you're really going to have to distribute an EXE which will control the zip process from beginning to end (you could package your real executable as a resource in the file).