开发者

SharpZipLib Problems Due to Zip Files with \ instead of /

I have a load of Legacy zips that have been zipped incorrectly.

Files were saved in the zip so they are folder\filename rather than folder/filename.

What that means is code like this:

using (ZipFile zipFile = new ZipFile(@"zippath.zip"))
{ 
     for (int i = 0; i < zipFile.Count; i++)
     {
          var entry = zipFile[i];
      开发者_C百科    zipFile.BeginUpdate();
          zipFile.Delete(entry);
          zipFile.CommitUpdate();

...

Is throwing a

Cannot find entry to delete

This is a known problem found here but unfortunately I have no control on how the zips are made and there are years worth of zip files I need to work with. Is there a way I can either fix the delete statement or repair (in C#) the zip file before using it?

Thanks!


There is a public function inside of ZipEntry that cleans the name for you. "ZipEntry.CleanName(yourstring)". Make this call when you are both adding your entry AND trying to delete it. note: If you are using this for file paths it will interpret the path correctly in the zip file even though the CleanName function replaces "\" with "/"

**

ADDING ZIPENTRY

**

zipEntryKey = file.FullName.Replace(file.Directory.Root.ToString(), string.Empty);
zipEntryKey = ZipEntry.CleanName(zipEntryKey);
ZipEntry entry = new ZipEntry(zipEntryKey);
entry.DateTime = file.LastWriteTime;


Stream fileStream = Minify(file);
byte[] buffer = new byte[fileStream.Length];
entry.Size = fileStream.Length;
fileStream.Read(buffer, 0, buffer.Length);
fileStream.Close();

zipStream.PutNextEntry(entry);
zipStream.Write(buffer, 0, buffer.Length);
zipStream.CloseEntry();

DELETING ZIPENTRY

zipEntryKey = file.FullName.Replace(file.Directory.Root.ToString(), string.Empty);
zipEntryKey = ZipEntry.CleanName(zipEntryKey);

if (existingZip.FindEntry(zipEntryKey, true) > -1)
{
    existingZip.BeginUpdate();
    ZipEntry Existing = existingZip[existingZip.FindEntry(zipEntryKey, true)];
    existingZip.Delete(Existing);
    existingZip.CommitUpdate();
}


It appears that the fix is at the bottom of your own link. One of the developers wrote a post with some code that simply replaced the \ character with a /. Try that, and perhaps from there you can then delete the zip entry.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜