Alright this one is bugging the heck out of.

I am developing some software that can auto generate a solution and projects within. I have the project templates already created (done by another co-worker), and zipped up. I have then added these as an embedded resource into my project. However, no matter what I try to do, I can't figure out how to load in the .zips as a valid Project Template. I just keep getting file can't be found like errors.

Is ther ANYway to do this? I have be searching the net for awhile, however, have had no luck. I can't believe this can't be done.

First you have to extract the embedded resourse itself to a some temporary file. Then you read and unzip temporary file to the final file.

I grabbed these code snippets from a setup program I wrote. It shows how to output an embedded resource and how to deflate (unzip) a zipped file. The embedded resource has name "packet" so change it to match whatever names you are using.

    // Namespaces for file streams and compressed streams handling
    using System.IO;
    using System.IO.Compression;

    public void extractResource(string outputFileName)
    {
        // Extract a resource named "packet" to a file
        File.WriteAllBytes(outputFileName, Properties.Resources.packet);
    }

    public void extractFile(string fileName, FileStream defStream)
    {
        FileStream fs;
        DeflateStream fromStream;
        byte[] buffer;
        int bufSize;
        int byteCount;

        bufSize = 4096;
        buffer = new byte[bufSize];
        // Deflate ("unzip") a filestream to a file
        try
        {
            fromStream = new DeflateStream(defStream, CompressionMode.Decompress, true);  // Deflate
            fs = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None);
            byteCount = 0;
            while (byteCount >= 0)
            {
                byteCount = fromStream.Read(buffer, 0, bufSize); // Read and decompress data from the stream
                if (byteCount > 0)
                {
                    fs.Write(buffer, 0, byteCount); // Write data to a new file
                }
                else
                {
                    byteCount = -1;
                }
            }
            fs.Close();
            fs.Dispose();
        }
        catch
        {
        }
    }

    public void extractZippedFile(string outputFileName)
    {
        FileStream fs;

        try
        {
            extractResource(@"temp.fil"); // Extract resource to a temp file
            fs = new FileStream(@"temp.fil", FileMode.Open, FileAccess.Read, FileShare.None); // Open temp file for reading
            extractFile(outputFileName, fs); // Decompress temp file to a new file
        }
        catch
        {
        }
        finally
        {
            fs.Close();
            fs = null;
        }
    }

HTH

WOW! I can't believe I didn't think of that! I'll give this a try and let you know (might be few days)

Well I feel silly for not thinking of Teme64's answer. This does indeed seemed to have worked.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.