开发者

How can I include a .zip file in a .net (C++-CLI) assembly?

开发者 https://www.devze.com 2023-04-01 10:53 出处:网络
Is it possible to include a .zip file as a resource inside a .net assembly? and if so, how is it done and how do I access it?

Is it possible to include a .zip file as a resource inside a .net assembly? and if so, how is it done and how do I access it?

I think this might work to access the data in there? I've used this before to access images, but I seem to recall having to manually re-import the resource into visual studio to pick up changes. Is it possible to include the .zip at compile time?

Edit (@Tejs)开发者_如何学运维

These are the options I have available if I right-click on the resource:

How can I include a .zip file in a .net (C++-CLI) assembly?

How can I include a .zip file in a .net (C++-CLI) assembly?


You simply need to mark the zip file as Embedded Resource under Build Action. http://msdn.microsoft.com/en-us/library/0c6xyb66.aspx


Have a look at the DotNetZip library source which is hosted at CodePlex. They use a pre-build process to compress files into a .zip and embed this as a resource in the library to later access it (it contains stuff required for self-extracting archives).


Okay, I found something which seems to work:

Step 1 - Embed the .zip

  • Right-click on the project and go to properties
  • Navigate to Configuration Properties > Linker > Input
  • Add the path to the .zip file to the Embed Managed Resource File property. For my initial test case I added $(ProjectDir)\ReadMe.zip which was a zip of the readme file which visual studio creates when you make a new project.

Step 2 - Extract the .zip

Add some code to extract that .zip file:

        Reflection::Assembly^ a1 = Reflection::Assembly::GetExecutingAssembly();

        cli::array<String^>^ names = a1->GetManifestResourceNames();
        for each( String^ name in names )
        {
            IO::BinaryReader^ s1 = gcnew IO::BinaryReader(a1->GetManifestResourceStream(name)); 

            String^ fileName = "Extracted_"+name;
            IO::BinaryWriter^ sw = gcnew IO::BinaryWriter( IO::File::Open( fileName, IO::FileMode::Create ) );
            sw->Write(s1->ReadBytes(s1->BaseStream->Length));
            sw->Flush();
            sw->Close();
        }

When that snippet runs, each .zip file added to the manifest at compile time will magically appear in the same directory the program was run from. I've tried modifying the contents of the .zip and sure enough, re-compiling adds the altered file into the executable and the result is extracted as expected.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号