Click here to Skip to main content
15,886,067 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In my project i have added a Resources.resx file which may / may not contain a .zip file
So i have to check if that .zip exists=) then read that to a byte[] else no action.

What I have tried:

C#
var resourcedata = Resources.ResourceManager.GetObject("test");
if (resourcedata != null)
{                       
 byte[] zipFile;
 BinaryFormatter bf = new BinaryFormatter();                        
 using (MemoryStream ms = new MemoryStream())
 {
   bf.Serialize(ms, resourcedata);
   zipFile = ms.ToArray();
  
 }

System.IO.File.WriteAllBytes(myPath, zipFile);
ZipFile.ExtractToDirectory(myPath, targetPath);
}


Working fine,.zip file copied to "myPath"
but issue arise when tried to extract programmatically ,Showing exception like
"Number of entries expected in End Of Central Directory does not correspond to number of entries in Central Directory"

Am i doing the right way?or is there any better way to implement this??
Please help.
Posted
Updated 19-Jan-21 10:48am
Comments
Alan N 19-Jan-21 18:30pm    
What is the resource 'test'? I would expect it to be the contents of a zip file stored as byte[] in which case the correct way to get it back would be to cast 'resourcedata', e.g. byte[] zipcontent = resourcedata as byte[].

Your use of a BinaryFormatter to serialise the 'resourcedata' cannot be correct and will only transform it into something which is definitely not a zipfile.
Member 14978771 20-Jan-21 10:17am    
Thank you so much for the clarification and that worked perfect. :)

Maybe you can use embedded resources, see this CodeProject tip:
Embed Resources in .NET Assemblies[^]

More information here: Working with .resx Files Programmatically | Microsoft Docs[^]

The problem could also be caused when a compression format is used that is unknown to the ZipFile.ExtractToDirectory() method, see:
ZipFile.ExtractToDirectory Method (System.IO.Compression) | Microsoft Docs[^]
 
Share this answer
 
v3
Comments
Member 14978771 20-Jan-21 10:07am    
I can't use 'Resources.myfile' in my project directly since it can/cannot be added to the .resx file. That's why I need an alternate way where I can use my resource file name as a string representation. Is there any way to do so?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900