Solved

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

Posted on 2006-11-16
5
424 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Suggested Solutions

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

752 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