locked
Create silverlight xap zip file at run time RRS feed

  • Question

  • Hi,

       Anyone.

               We are using silverlight web application project. As per my project requirement, we need to create silverlight build xap file dynamically. Compress as a zip file at runtime.

    Any solution,

    Regards,

    Prabakaran G,

    Maruti Computers P Ltd.

    Monday, August 6, 2012 1:22 PM

Answers

  • Hi, Mike.

              Apologies for long delay to reply. I saw Mr Khanna's suggestion that is not based on creation of silverlight XAP at runtime. That's depends on already created XAP file. Instead, I used StreamUtils.Copy(streamReader, zipStream, buffer); method to copy the Zipoutstream temporarly and recursion function to create XAP.

    Below code should helpful to create silverlight XAP dynamically,

    public void CreateSample(string outPathname, string password, string folderName) {

    FileStream fsOut = File.Create(outPathname);
    ZipOutputStream zipStream = new ZipOutputStream(fsOut);

    zipStream.SetLevel(3); //0-9, 9 being the highest level of compression
    int folderOffset = folderName.Length + (folderName.EndsWith("\\") ? 0 : 1);

    CompressFolder(folderName, zipStream, folderOffset);

    zipStream.IsStreamOwner = true; // Makes the Close also Close the underlying stream
    zipStream.Close();
    }
    private void CompressFolder(string path, ZipOutputStream zipStream, int folderOffset) {

    string[] files = Directory.GetFiles(path);

    foreach (string filename in files) {

    FileInfo fi = new FileInfo(filename);

    string entryName = filename.Substring(folderOffset); // Makes the name in zip based on the folder
    entryName = ZipEntry.CleanName(entryName); // Removes drive from name and fixes slash direction
    ZipEntry newEntry = new ZipEntry(entryName);
    newEntry.DateTime = fi.LastWriteTime; // Note the zip format stores 2 second granularity

    newEntry.Size = fi.Length;

    zipStream.PutNextEntry(newEntry);
    byte[ ] buffer = new byte[4096];
    using (FileStream streamReader = File.OpenRead(filename)) {
    StreamUtils.Copy(streamReader, zipStream, buffer);
    }
    zipStream.CloseEntry();
    }
    string

    [ ] folders = Directory.GetDirectories(path);
    foreach (string folder in folders) {
    CompressFolder(folder, zipStream, folderOffset);
    }
    } Using this recursion function we can create silverlight XAP file dynamically.

    Thanks,
    Prabakaran G.
    Maruti Computers P Ltd.

    • Marked as answer by Mike Feng Tuesday, August 14, 2012 8:19 AM
    Tuesday, August 14, 2012 7:14 AM

All replies

    • Proposed as answer by Mike Feng Thursday, August 9, 2012 11:07 AM
    Monday, August 6, 2012 2:55 PM
  • Hi, Gaurav

       Thanks for your response. I will try this way.

    Regards,

    Prabakaran G,

    Maruti Computers P Ltd.

    Tuesday, August 7, 2012 1:36 PM
  • Hi Prabakar,

    Welcome to the MSDN Forum.

    How about Khanna's suggestion?

    Do you get any update?

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, August 9, 2012 11:07 AM
  • Hi, Mike.

              Apologies for long delay to reply. I saw Mr Khanna's suggestion that is not based on creation of silverlight XAP at runtime. That's depends on already created XAP file. Instead, I used StreamUtils.Copy(streamReader, zipStream, buffer); method to copy the Zipoutstream temporarly and recursion function to create XAP.

    Below code should helpful to create silverlight XAP dynamically,

    public void CreateSample(string outPathname, string password, string folderName) {

    FileStream fsOut = File.Create(outPathname);
    ZipOutputStream zipStream = new ZipOutputStream(fsOut);

    zipStream.SetLevel(3); //0-9, 9 being the highest level of compression
    int folderOffset = folderName.Length + (folderName.EndsWith("\\") ? 0 : 1);

    CompressFolder(folderName, zipStream, folderOffset);

    zipStream.IsStreamOwner = true; // Makes the Close also Close the underlying stream
    zipStream.Close();
    }
    private void CompressFolder(string path, ZipOutputStream zipStream, int folderOffset) {

    string[] files = Directory.GetFiles(path);

    foreach (string filename in files) {

    FileInfo fi = new FileInfo(filename);

    string entryName = filename.Substring(folderOffset); // Makes the name in zip based on the folder
    entryName = ZipEntry.CleanName(entryName); // Removes drive from name and fixes slash direction
    ZipEntry newEntry = new ZipEntry(entryName);
    newEntry.DateTime = fi.LastWriteTime; // Note the zip format stores 2 second granularity

    newEntry.Size = fi.Length;

    zipStream.PutNextEntry(newEntry);
    byte[ ] buffer = new byte[4096];
    using (FileStream streamReader = File.OpenRead(filename)) {
    StreamUtils.Copy(streamReader, zipStream, buffer);
    }
    zipStream.CloseEntry();
    }
    string

    [ ] folders = Directory.GetDirectories(path);
    foreach (string folder in folders) {
    CompressFolder(folder, zipStream, folderOffset);
    }
    } Using this recursion function we can create silverlight XAP file dynamically.

    Thanks,
    Prabakaran G.
    Maruti Computers P Ltd.

    • Marked as answer by Mike Feng Tuesday, August 14, 2012 8:19 AM
    Tuesday, August 14, 2012 7:14 AM