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();

}

This article is part of the GWB Archives. Original Author: Liam Westley

New on Geeks with Blogs

  • We Won The One Award I Actually Care About

    Full Scale made the Inc. 5000 for the fifth year straight, the 12th listing across my three companies. Here is why the one award you cannot buy is worth stopping for.

  • Your Customers Build the Features Now

    I let a tool I liked sit dead for a year rather than build the features I wanted. An MCP server meant I never had to, and your customers can do the same to your product.

  • Get the Size of a Directory in Linux the Easy Way

    du -sh for the quick answer, ncdu for the cleanup, df for the disk itself: every command for checking directory size in Linux, plus why du and df never agree.

  • Vim Search and Replace: The Ultimate Guide

    One :%s command replaces every match in a file before a find dialog would even open. The Vim substitute patterns worth the muscle memory: flags, ranges, capture groups, and multi-file edits.