Issues with SharpZipLib

C

Christer

Hi all,

I've been trying to use the SharpZipLib as it sounds promising and a
lot of people refer to it in discussions on compression. However, I am
getting some bad output, when I try to unzip a (correctly hand made)
..zip file via code. I DO get the right amount of files, however, some
seem cut in half ... when viewing the output files directly, some of
the .gif file show only half, some of the included .swf files show
nothing.

Furthermore, the filenames get corrupted when using special
characters.

Could someone with experience in using the SharpZipLib, please give me
a view on my troubles? Extraction code below...

Kind regards,
Christer

string rootPath = @"c:\sletmig\";
FlushExtractDir(); //Delete existing output directory
ZipFile zFile = new ZipFile(rootPath + "vejledning.zip");
try
{
//For each entry in zip file...
foreach (ZipEntry e in zFile)
{

string fileName = Path.GetFileName(rootPath + @"udpak\" + e.Name);
string directoryName = Path.GetDirectoryName(rootPath + @"udpak\" +
e.Name);

//Ensure directory...
if (!Directory.Exists(directoryName))
{
Directory.CreateDirectory(directoryName);
}
//If file entry, create the file...
if (fileName.Length > 0)
{
System.IO.Stream s = zFile.GetInputStream(e);

int length = (int)e.Size;
byte[] bytes = new byte[length];
s.Read(bytes, 0, length);
FileStream newfile = new FileStream(rootPath + @"udpak\" + e.Name,
FileMode.Create);
newfile.Write(bytes, 0, length);
newfile.Close();
s = null;
}
}
}
finally
{
zFile.Close();
}
 
C

Chris Dunaway

I'm not sure, but I ran into a similar issue to this and I think you
need to check the return value from the s.Read call. It's possible
that it is not reading all the bytes from the zip stream. I think you
need to run it in a loop and keep reading until there is no more data
to read. I don't have the code I used at hand, but when I can find it
I'll post it.

Chris
 
J

Jon Skeet [C# MVP]

Christer said:
I've been trying to use the SharpZipLib as it sounds promising and a
lot of people refer to it in discussions on compression. However, I am
getting some bad output, when I try to unzip a (correctly hand made)
.zip file via code. I DO get the right amount of files, however, some
seem cut in half ... when viewing the output files directly, some of
the .gif file show only half, some of the included .swf files show
nothing.

See http://www.pobox.com/~skeet/csharp/readbinary.html

Don't assume that Stream.Read will return as many bytes as you
requested from it in one go.
Furthermore, the filenames get corrupted when using special
characters.

That I'm not sure about - there may be a restriction in the zip format
itself.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top