Extract zip from stream using DotNetZip

public void ZipExtract(Stream inputStream, string outputDirectory)
    {

        using (ZipFile zip = ZipFile.Read(inputStream))
        {
            Directory.CreateDirectory(outputDirectory);
            zip.ExtractSelectedEntries("name=*.jpg,*.jpeg,*.png,*.gif,*.bmp", " ", outputDirectory,
                                       ExtractExistingFileAction.OverwriteSilently);
        }
    }
 [HttpPost]
    public ContentResult Uploadify(HttpPostedFileBase filedata)
    {
        var path = Server.MapPath(@"~/Files");
        var filePath = Path.Combine(path, filedata.FileName);
       if (filedata.FileName.EndsWith(".zip"))
        {

            ZipExtract(Request.InputStream,path);

        }
        filedata.SaveAs(filePath); 

            _db.Photos.Add(new Photo
                               {
                                   Filename = filedata.FileName
                               });

        _db.SaveChanges();
        return new ContentResult{Content = "1"};

    }

      

I am trying to read a zip archive from a stream and extract files. Received the following exception at line "using (ZipFile zip = ZipFile.Read (inputStream))": ZipEntry :: ReadDirEntry (): Bad signature (0xC618F879) at position 0x0000EE19 Any ideas how to deal with this exception?

+1


source to share


2 answers


The error occurs because the stream you are trying to read is not a valid zip stream. In most cases, Request.InputStream will not represent a zip file. It will be an HTTP message that looks like this:

POST /path/for/your/app.aspx HTTP/1.1
User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.2; ...)
Content-Type: application/x-www-form-urlencoded
Content-Length: 11132

 ...more stuff here...

      

I think you are trying to read this message as if it were a zip file. It won't work. The content of the file is actually embedded in the "... more stuff here ..." part.

To solve this problem, I suggest you work in smaller steps.



First, upload the file to work, saving the contents of the downloaded file to a file system file on the server. Then, on the server, try opening the file as a zip file. If it works, you can replace the save part of the file with ZipFile.Read (). If you cannot open the file you saved, then the file you saved is not a zip file. Either it is incomplete, or rather, it includes extraneous data such as HTTP headers.

If you are having trouble downloading a binary file such as a zip file successfully, download the text file first. You can more easily check the download of a text file on the server by simply opening the downloaded content in a text editor and checking that it contains exactly the content of the file downloaded from the client. After that, you will be taken to the binary. Then you can go for the full streaming approach using DotNetZip to read the stream. Once you get to this point, you won't need to save the file to the filesystem before reading it as a zip file, but you can save it anyway for other reasons.

To help, you can use Fiddler2, an HTTP debug proxy. Install it on your browser, enable it, and it helps you see messages that are sent from the browser to the ASPNET application on the server. You will see that the file upload contains more than just bare file data.

+2


source


A more stable solution might be to use ICSharpCode ZipLib: http://www.sharpdevelop.net/OpenSource/SharpZipLib/Default.aspx



-2


source







All Articles