由于 Zip 文件使用 \ 而不是 / 导致的 SharpZipLib 问题

发布于 2024-10-22 02:19:01 字数 624 浏览 5 评论 0原文

我有一堆旧版拉链,但压缩方式不正确。

文件保存在 zip 中,因此它们是文件夹\文件名而不是文件夹/文件名。

这意味着这样的代码:

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

...

正在抛出一个

找不到要删除的条目

这是此处发现的已知问题,但不幸的是我无法控制 zip 的制作方式,而且我需要使用多年的 zip 文件。有没有办法可以在使用之前修复删除语句或修复(在 C# 中)zip 文件?

谢谢!

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];
          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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

太阳男子 2024-10-29 02:19:01

ZipEntry 内部有一个公共函数可以为您清理名称。 “ZipEntry.CleanName(您的字符串)”。当您添加条目并尝试删除它时,请拨打此电话。 注意:如果您将其用于文件路径,即使 CleanName 函数将“\”替换为“/”,它也会正确解释 zip 文件中的路径

**

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

删除 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();
}

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();
}
素罗衫 2024-10-29 02:19:01

修复似乎位于您自己的链接的底部。一位开发人员写了一篇文章,其中的一些代码只是将 \ 字符替换为 /。尝试一下,也许您可​​以从那里删除该邮政编码条目。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文