Hi
I decided to use SharpLibZip for a MVC project to make it possible to upload files to a web server, validate said file and then extract the files into file system.
I have 3 layers in here, the Presentation Layer which hosts the controller that takes the [POST] action that gets a HttpPostedFileBase file (among other things), then transforms it into a byte[ to pass it over to the Business Layer, which decompresses the zip file from said binaries, validates specific files from it (for instance some files have to have a specific extension, etc), then passes the byte[ to the Data Access Layer, which decompresses the zip and extracts it into a specific folder
However, when I try doing this I get an "EOF in header" exception in "ZipEntry entry = zipInputStream.GetNextEntry()".
The zip files I'm using for testing are compressed using 7-zip and they work fine. I dunno if this is something I did wrong in the code, or something I did wrong on the data conversion (for each layer), or if it's a bug or something
I tried testing it from another project, where I load the byte[ from a FileStream instead of HttpPostedFileBase, and the same error occurs.
I'll post the general view of my code here (at least the parts I deem important):
PresentationLayer:
HttpPostedFileBase file = ... //Gets file from the model
byte[ buffer = new byte[file.ContentLength];
file.InputStream.Read(buffer, 0, buffer.Length);
MyBusinessFunction(buffer);
BusinessLayer:
MyBusinessFunction(byte[ buffer){
MemoryStream stream = new MemoryStream();
stream.Write(buffer, 0, buffer.Length);
using (ZipInputStream zip = new ZipInputStream(stream))
{
ZipEntry entry;
while ((entry = zip.GetNextEntry()) != null)
{
//I use entry to validate the file, like using "entry.IsFile" and "entry.Name". If it doesn't validate throws exception
}
}
MyDataFunction(buffer)
}
Data Access Layer:
MyDataFunction(buffer){
MemoryStream stream = new MemoryStream()
stream.Write(buffer, 0, buffer.Length);
using (ZipInputStream zip = new ZipInputStream(stream))
{
ZipEntry entry;
while ((entry = zip.GetNextEntry()) != null)
{
//Iterate all entries and write them to a specific path
}
}
}
The exception is thrown when "entry = zip.GetNextEntry()" is called in the BL (if I test the data layer separately it is thrown there as well).
I googled but I couldnt' find anything, other than this:
http://community.sharpdevelop.net/forums/t/4499.aspx
But I never explicitely call Crc or anything like that, I just use the ZipEntry properties and that's it
Is there something wrong with the way I'm passing the buffer, or coverting the InputStream in the byte[ then into MemoryStream, or something like that?
Pre-Edit: seems byte[ isn't showed properly (at least in the preview mode). When I write byte[ I mean an array of bytes (just in case)