SharpZipLib, minor tweak to samples allows opening of ZIP files in Windows XP SP2

The free C# ZIP library (http://www.icsharpcode.net/OpenSource/SharpZipLib/Download.aspx) is great for reading and creating ZIP files.  However, I came across a problem with ZIP files created using the samples in their help file when opening them under Windows XP SP2.  Windows XP refuses to view the files and wants you to 'unblock' them, but when you follow the instructions the 'unblock' option is not available. 

I found out it was a simple fix, documented in the help file but not actually used in the samples.  As I couldn't Google a solution (one guy had the problem, but no solution was posted) I thought I'd post it here.  Just use the ZipEntry.CleanName method on the original filename when creating the new ZipEntry object, i.e.,

ZipEntry entry = new ZipEntry(ZipEntry.CleanName(@"c:\boot.ini"));

Basically, Windows XP SP2 doesn't like the names of ZIP file entries to include file system items such as drive letters, UNC share names or backslashes.

Liam

--------- full sample converted from help file (place on Windows Form inside button click event ---------

using System.IO;

using ICSharpCode.SharpZipLib.Zip;

using ICSharpCode.SharpZipLib.Checksums;

private void button1_Click(object sender, System.EventArgs e)

{

    Crc32 crc = new Crc32();

    ZipOutputStream s = new ZipOutputStream(File.Create(@"c:\newfile.zip"));

    s.SetLevel(9); // 0 - store only to 9 - means best compression

    FileStream fs = File.OpenRead(@"c:\boot.ini");

    byte[] buffer = new byte[fs.Length];

    fs.Read(buffer, 0, buffer.Length);

    ZipEntry entry = new ZipEntry(ZipEntry.CleanName(@"c:\boot.ini"));

    entry.DateTime = DateTime.Now;

    entry.Comment = "test file";

    entry.ZipFileIndex = 1;

    entry.Size = fs.Length;

    fs.Close();

    crc.Reset();

    crc.Update(buffer);

    entry.Crc = crc.Value;

    s.PutNextEntry(entry);

    s.Write(buffer, 0, buffer.Length);

    s.Finish();

    s.Close();

}

Print | posted on Tuesday, November 08, 2005 7:56 AM

Feedback

# re: SharpZipLib, minor tweak to samples allows opening of ZIP files in Windows XP SP2

left by Virgil at 11/16/2005 1:21 PM Gravatar
thanks!
works!

# re: SharpZipLib, minor tweak to samples allows opening of ZIP files in Windows XP SP2

left by juanmanuelz@gmail.com at 1/5/2006 9:01 PM Gravatar
do you know how i can repair a zipfile with this lib?

# re: SharpZipLib, minor tweak to samples allows opening of ZIP files in Windows XP SP2

left by Liam Westley at 1/6/2006 12:16 PM Gravatar
Juan,

I haven't used the library to do this I've actually never had a corrupt ZIP file, so have not needed to investigate this area.

# re: SharpZipLib, minor tweak to samples allows opening of ZIP files in Windows XP SP2

left by Oleg Kosenkov at 3/16/2006 2:35 PM Gravatar
I use buffered input and output so that will work with large files as well:
(note, this is a part of MSBuild task, so I keep the function unchanged )

private bool ZipFiles()
{
Crc32 crc = new Crc32();
ZipOutputStream zs = null;

try
{
Log.LogMessage(MSBuild.Community.Tasks.Properties.Resources.ZipCreating, _zipFileName);

using (zs = new ZipOutputStream(File.Create(_zipFileName)))
{

// make sure level in range
_zipLevel = System.Math.Max(0, _zipLevel);
_zipLevel = System.Math.Min(9, _zipLevel);

zs.SetLevel(_zipLevel);
if (!string.IsNullOrEmpty(_comment))
zs.SetComment(_comment);

byte[] buffer = new byte[32768];
// add files to zip
foreach (ITaskItem fileItem in _files)
{
string name = fileItem.ItemSpec;
FileInfo file = new FileInfo(name);
if (!file.Exists)
{
Log.LogWarning(MSBuild.Community.Tasks.Properties.Resources.FileNotFound, file.FullName);
continue;
}

// clean up name
if (_flatten)
name = file.Name;
else if (!string.IsNullOrEmpty(_workingDirectory)
&& name.StartsWith(_workingDirectory, true, CultureInfo.InvariantCulture))
name = name.Remove(0, _workingDirectory.Length);

name = ZipEntry.CleanName(name, true);

ZipEntry entry = new ZipEntry(name);
entry.DateTime = file.LastWriteTime;
entry.Size = file.Length;

using (FileStream fs = file.OpenRead())
{
crc.Reset();
long len = fs.Length;
while (len > 0)
{
int readSoFar = fs.Read(buffer, 0, buffer.Length);
crc.Update(buffer, 0, readSoFar);
len -= readSoFar;
}
entry.Crc = crc.Value;
zs.PutNextEntry(entry);

len = fs.Length;
fs.Seek(0, SeekOrigin.Begin);
while (len > 0)
{
int readSoFar = fs.Read(buffer, 0, buffer.Length);
zs.Write(buffer, 0, readSoFar);
len -= readSoFar;
}
}
Log.LogMessage(MSBuild.Community.Tasks.Properties.Resources.ZipAdded, name);
} // foreach file
zs.Finish();
}
Log.LogMessage(MSBuild.Community.Tasks.Properties.Resources.ZipSuccessfully, _zipFileName);
return true;
}
catch (Exception exc)
{
Log.LogErrorFromException(exc);
return false;
}
finally
{
if (zs != null)
zs.Close();
}
}

If we just read the whole file buffer in memory this will cause the exceptions in some cases - smth about insufficient memory resources per process domain.

# re: SharpZipLib, minor tweak to samples allows opening of ZIP files in Windows XP SP2

left by Jacob Raines at 11/14/2006 5:21 PM Gravatar
Thank you so much. That was exactly the problem I was having. P.S. What is the reason for the ZipFileIndex?

# re: SharpZipLib, minor tweak to samples allows opening of ZIP files in Windows XP SP2

left by AG at 11/17/2006 8:16 PM Gravatar
Thank you for this!

# re: SharpZipLib, minor tweak to samples allows opening of ZIP files in Windows XP SP2

left by byock at 12/13/2006 8:09 AM Gravatar
I used this lines of code...

If Not File.Exists(FileName) Then
outputStrm = New ZipOutputStream(File.Create(FileName))
Else
outputStrm = New ZipOutputStream(File.Open(FileName, FileMode.Truncate))
End If

and the FileName is C:\download\zip\zipfile.zip


But i am getting this error:
Could not find a part of the path "C:\download\zip\zipfile.zip".


Doesn't the above lines of code create this file path for me if it not existing? Because i am getting this error if i dont have C:\download\zip in my directory....but when i physically create C:\download\zip....the code runs smoothly...

Any idea on what i should do?

Thanks in advance

# re: SharpZipLib, minor tweak to samples allows opening of ZIP files in Windows XP SP2

left by Liam Westley at 12/13/2006 8:26 AM Gravatar
In the MSDN help it describes the File.Create method as;

Creates a file in the *specified path*.

I would guess it only creates the file, not the entire path if it does not already exist. So it's working exactly as expected I'm afraid.

# re: SharpZipLib, minor tweak to samples allows opening of ZIP files in Windows XP SP2

left by lindinho at 1/11/2007 5:42 PM Gravatar
Many thanks Oleg for the code!
-lindinho

# re: SharpZipLib, minor tweak to samples allows opening of ZIP files in Windows XP SP2

left by Ireis at 3/12/2007 10:54 AM Gravatar
Anyone know how to add the password?
my code below:

crc = new Crc32();
zip = new ZipOutputStream(File.Create(destDir));
zip.SetLevel(zipLevel);
if (zipPass != string.Empty)
zip.Password = zipPass;

but it doesn't work.

# re: SharpZipLib, minor tweak to samples allows opening of ZIP files in Windows XP SP2

left by Krunal at 7/3/2007 8:43 PM Gravatar
thanks for this help......

# re: SharpZipLib, minor tweak to samples allows opening of ZIP files in Windows XP SP2

left by D at 10/31/2007 9:28 PM Gravatar
I've tried to set password and it's works fine:
code:

crc = new Crc32();
zip = new ZipOutputStream(File.Create(destDir));
zip.SetLevel(zipLevel);

zip.Password = "testPassword";

# re: SharpZipLib, minor tweak to samples allows opening of ZIP files in Windows XP SP2

left by Niru at 8/28/2008 10:38 PM Gravatar
Thanks a lot for the post. I had this issue that the zipped file were blocked by all the email clients. All email clients detected it to be a virus. Your post helped me fix it.

# re: SharpZipLib, minor tweak to samples allows opening of ZIP files in Windows XP SP2

left by Nilesh Makwana at 11/25/2008 10:23 AM Gravatar
Hello, I want to password protext the zip files. Writing the fol. line of code :

zip.Password = "123456";

gives me a dialog to enter password when I unzip. But it gives error that the password is incorrent evenif I enter the right password.

Why it is so ?
Title  
Name
Email (never displayed)
Url
Comments   
Please add 4 and 4 and type the answer here: