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