1

I have an application in ASP.NET where user can upload ZIP file. I'm trying to extract file using ICSharpZipLib (I also tried DotNetZip, but had same issue).

This zip file contains single xml document (9KB before compress).

When I open this file with other applications on my desktop (7zip, windows explorer) it seems to be ok. My unzip method throws System.OutOfMemoryException and I have no idea why is that. When I debugged my unziping method I noticed that zipInputStreams' Length property throws Exception and is not available:

Stream UnZipSingleFile(Stream memoryStream)
        {

            var zipInputStream = new ZipInputStream(memoryStream);

            memoryStream.Position = 0;

            zipInputStream.GetNextEntry();

            MemoryStream unzippedStream = new MemoryStream();

            int len;
            byte[] buf = new byte[4096];
            while ((len = zipInputStream.Read(buf, 0, buf.Length)) > 0)
            {
                unzippedStream.Write(buf, 0, len);
            }

            unzippedStream.Position = 0;
            memoryStream.Position = 0;

            return unzippedStream;
    }

and here's how I get string of unzippedStream:

string GetString()
        {
            var reader = new StreamReader(unzippedStream);
            var result = reader.ReadToEnd();
            unzippedStream.Position = 0;
            return result;
        }
JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
TrN
  • 1,230
  • 2
  • 17
  • 32
  • Try using `string result = System.Text.Encoding.Default.GetString(unzippedStream);`, as it is probably encoded and resulting in what you see in your current result. – Nahydrin Feb 21 '12 at 14:19
  • Is `unzippedStream` a property of your class? How is it getting to `GetString()`? I'm wondering if you have a name collision somewhere. Can you post the method that calls `GetString()`? – D Stanley Feb 21 '12 at 14:24
  • How are you calling those 2 methods? Could you shown an example? – Darin Dimitrov Feb 21 '12 at 14:29
  • this code is simplified example (only UnZipSingleFile looks excactly the same) – TrN Feb 21 '12 at 14:36

2 Answers2

0

While this post is quite old, I think it could be beneficial to illustrate how I did this for compression and decompression using ICSharpZipLib (C# package version 1.1.0). I put this together by looking into the examples shown here (see ie. these compression and decompression examples).

Assumption: The input to the compression and decompression below should be in bytes. If you have ie. an xml file you could load it to an XDocument, and convert it into an XmlDocument with .ToXmlDocument(). From there, you could access the string contents by calling .OuterXml, and converting the string to a byte array.

// Compression (inputBytes = ie. string-to-compress, as bytes)
using var dataStream = new MemoryStream(inputBytes);
var outputStream = new MemoryStream();
using (var zipStream = new ZipOutputStream(outputStream))
{
    zipStream.SetLevel(3);
    var newEntry = new ZipEntry("someFilename.someExtension");
    newEntry.DateTime = DateTime.Now;
    zipStream.PutNextEntry(newEntry);
    StreamUtils.Copy(dataStream, zipStream, new byte[4096]);
    zipStream.CloseEntry();
    zipStream.IsStreamOwner = false;
}
outputStream.Position = 0;
var outputBytes = outputStream.ToArray();

// Decompression (inputBytes = ie. string-to-decompress, as bytes)
using var dataStream = new MemoryStream(inputBytes);
var outputStream = new MemoryStream();
using (var zipStream = new ZipInputStream(dataStream))
{
    while (zipStream.GetNextEntry() is ZipEntry zipEntry)
    {
        var buffer = new byte[4096];
        StreamUtils.Copy(zipStream, outputStream, buffer);
    }
}
var outputBytes = outputStream.ToArray();
Fhyarnir
  • 453
  • 1
  • 4
  • 12
0

From their wiki:

"Sharpzip supports Zip files using both stored and deflate compression methods and also supports old (PKZIP 2.0) style and AES encryption"

Are you sure the format of the uploaded zip file is acceptable for SharpZipLib?

Steve
  • 213,761
  • 22
  • 232
  • 286
  • No I am not, and I cannot be. You propably got a point. I have no possibility to affect on users which format they choose. – TrN Feb 21 '12 at 14:39
  • hm... nope, this one is deflete – TrN Feb 21 '12 at 14:47
  • In their [examples](http://wiki.sharpdevelop.net/SharpZipLib-Zip-Samples.ashx) they use a StreamUtils.Copy to unzip when using ZipInputStream. Perhaps this method will give back some hint if it fails. – Steve Feb 21 '12 at 16:04