Solved

C# - Urgent. -- Get a file from the assembly and pass it to the FileStream class

Posted on 2006-11-16
5
430 Views
Last Modified: 2008-01-09
Hi ,

This is very urgent. I have a file as an embedded resource. I want to read the file and pass it to the GZipStream class. There are 2 ways in which the GZipStream accepts :-

1) As a memoryStream
2) As a FileStream.

The Embedded file is a compressed file.

1) I have test it with the FileStream method when the copressed file is present on the local.
But how do I read an embedded resource as a FileStream ?

2) I have used the Assembly.Load("assemblyName").GetManifestResourceStream("FileName");
I passed this to a StreamReader but the data is getting corrupted.
I also used the BufferedStream with the same result.

hence my instinct is -- since I know that the FileStream works -- somehow to get the embedded resource from the Assembly as a FileStream, then the remaining code would work. But how to do that ??

there is GetFile() method which returns a FileStream but currently its giving a null value.

Assembly.Load("StylesheetLTC").GetFile("nameOfFile");

Any help ????

thanks,

0
Comment
Question by:paromitabanerjee
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 22

Expert Comment

by:cookre
ID: 17962303
string AppPath=Assembly.GetExecutingAssembly().Location;
Assembly ThisAssembly=Assembly.LoadFrom(AppPath);
System.IO.Stream fs=ThisAssembly.GetManifestResourceStream("name.embeddedresourcename");
StreamReader x=new StreamReader(fs);
WholeFile=x.ReadToEnd();
x.Close();
fs.Close();
0
 

Author Comment

by:paromitabanerjee
ID: 17962537
Hi cookre,

Thanks for the prompt reply. I had already tried StreamReader, while compiling it doesn't give an error. As I said, the data coming back is a compressed data. So while decompressing, it is giving me InvalidDataException.

But if I decompress the same file in another project but using the FileStream object -- it runs perfectly and gives back the proper decompressed data.

My code for decompressing :

        public static String GetXSL(String nameOfFile)
        {
            String str = "";
            try
            {
                  string nameoffile = "StylesheetLTC50." + nameOfFile;
                    try
                    {
                      //   fstr = (Assembly.Load("StylesheetLTC50")).GetFile(nameOfFile);
                        fstr = (FileStream)(Assembly.Load("StylesheetLTC50").GetManifestResourceStream(nameoffile));
                    }catch(Exception ex)
                    {
                        Console.WriteLine("FileStream Exception == "+ex);
                    }
                    // The StreamReader is not giving proper data -- the GzipStream throws an exception
                    StreamReader stread = new StreamReader(Assembly.Load("StylesheetLTC50").GetManifestResourceStream(nameoffile));                                                                  
                    strData = stread.ReadToEnd();
                       
                     // So now we pass the byte to the MemoryStream.  
                    MemoryStream ms = new MemoryStream(System.Text.Encoding.UTF8.GetByte(strData));                

                    GZipStream compressedZipStream = new GZipStream(fstr, CompressionMode.Decompress);
                     int offset = 0;
                     int totalBytes = 0;
                     byte[] smallBuffer = new byte[(ms.Length + 230000)];
                 
                     try
                     {
                         while (true)
                         {
                             int bytesRead = compressedZipStream.Read(smallBuffer, 0, smallBuffer.Length);
                             if (bytesRead == 0)
                             {
                                 break;
                             }
                             offset += bytesRead;
                             totalBytes += bytesRead;
                         }
                     }catch(Exception exc)
                     {
                         Console.WriteLine("Exception == "+exc);
                     }
                     compressedZipStream.Close();

           
                     GZipStream ccompressedZipStream = null;
                     // Decompress the file contents
                     ccompressedZipStream = new GZipStream(fstr, CompressionMode.Decompress);                                                          
                     byte[] bufferrr = new byte[totalBytes];
                     try
                     {
                         ccompressedZipStream.Read(bufferrr, 0, totalBytes);
                     }catch(Exception ex)
                     {
                         Console.WriteLine("The read line is throwing an error == "+ex);
                     }
                     stread.Close();
                     ccompressedZipStream.Close();                    
                     Console.ReadLine();

                      str = System.Text.Encoding.UTF8.GetString(bufferrr);
                      Console.WriteLine("str="+ str);

                                 
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception == " + ex);
            }

            return str;
        }

Is something wrong with the code ???? especially the part where I am converting the string to bytes ??

 thanks,
0
 

Author Comment

by:paromitabanerjee
ID: 17962672
Can someone please help. this is urgent ............
0
 
LVL 12

Accepted Solution

by:
sumix earned 500 total points
ID: 17963294
Hi,
I guess that you cannot decompress the embedded file because GetManifestResourceStream method returns a memorystream and this cannot be converted to a FileStream. Therefore, fstr is null and compressedZipStream is not valid.

Here's a way that should work:

// this is how compressedZipStream can be created:
GZipStream compressedZipStream = new GZipStream(asb.GetManifestResourceStream(nameoffile), CompressionMode.Decompress);

// i choose here to read one byte at a time and put it in a FileStream
FileStream strOut = File.Create(@"uncompressed.txt");
int bt;
while ((bt = compressedZipStream.ReadByte()) != -1)
            strOut.WriteByte((byte)bt);
compressedZipStream.Close();
strOut.Close();

You can check now that "uncompressed.txt" is created ok.
0
 

Author Comment

by:paromitabanerjee
ID: 17965279
Hi sumix,

yup this worked gt8 !!!

I was thinking that GZipStream accepts only a memoryStream and trying to assign the same to it.
Thanks for ur help..... I had been breaking my head at this for a day !! :)



0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question