如何处理与过长的路径解压的ZipFile /复制 [英] How to handle unzipping ZipFile with paths that are too long/duplicate

查看:1447
本文介绍了如何处理与过长的路径解压的ZipFile /复制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当在Windows中解压缩文件,我会偶尔有路径的问题。




  1. 过长的Windows(但还好在创建该文件原来的操作系统)。

  2. 被复制,由于不区分大小写



使用DotNetZip,在 ZipFile.Read(路径)通话将废话了,只要读取与这些问题之一zip文件。这意味着我不能甚至尝试筛选出来。

 使用(ZIP使用ZipFile = ZipFile.Read(路径))
{

}

什么是最好的方式?处理读取这些文件进行排序的。



更新:



例如,从这里拉链:
https://github.com/MonoReports/MonoReports/zipball/master



重复:
HTTPS ://github.com/MonoReports/MonoReports/tree/master/src/MonoReports.Model/DataSourceType.cs
https://github.com/MonoReports/MonoReports/tree/master/src/ MonoReports.Model / DatasourceType.cs



下面是例外的更多详细信息:




Ionic.Zip.ZipException:无法读取,作为一个ZipFile的结果
---> System.ArgumentException:一个>使用相同的密钥项目已被添加结果
。在系统.ThrowHelper.ThrowArgumentException(ExceptionResource资源)结果
。在System.Collections.Generic.Dictionary 2.插入(TKEY的关键,TValue值,布尔加)结果
。在System.Collections.Generic.Dictionary
2.增加(TKEY的关键,TValue值)结果
在Ionic.Zip.ZipFile.ReadCentralDirectory(ZipFile的ZF)结果
在Ionic.Zip.ZipFile.ReadIntoInstance(ZipFile的ZF)




分辨率:



根据@ Cheeso的建议,我可以从流中读到的一切,那些避免重复和路径问题:

  //使用(ZIP使用ZipFile = ZipFile.Read(路径))
使用(ZipInputStream流=新ZipInputStream(路径))
{
的ZipEntryË;
,而((E = stream.GetNextEntry())!= NULL)
//的foreach(ZipEntry的E在拉链)
{
如果(e.FileName.ToLower() .EndsWith(CS)||
e.FileName.ToLower()的endsWith(XAML。))
{
//变种毫秒=新的MemoryStream();
//e.Extract(ms);
变种SR =新的StreamReader(流);
{
//ms.Position = 0;
CodeFiles.Add(新的CodeFile(){内容= sr.ReadToEnd(),文件名= e.FileName});
}
}
}
}


解决方案

与ZipInputStream阅读。



zip文件级保留使用文件名作为索引的集合。复制文件名休息的模型。



但你可以使用ZipInputStream在你的zip文件阅读。没有集合或指数在这种情况下


When unzipping files in Windows, I'll occasionally have problems with paths

  1. that are too long for Windows (but okay in the original OS that created the file).
  2. that are "duplicate" due to case-insensitivity

Using DotNetZip, the ZipFile.Read(path) call will crap out whenever reading zip files with one of these problems. Which means I can't even try filtering it out.

using (ZipFile zip = ZipFile.Read(path))
{
    ...
}

What is the best way to handle reading those sort of files?

Updated:

Example zip from here: https://github.com/MonoReports/MonoReports/zipball/master

Duplicates: https://github.com/MonoReports/MonoReports/tree/master/src/MonoReports.Model/DataSourceType.cs https://github.com/MonoReports/MonoReports/tree/master/src/MonoReports.Model/DatasourceType.cs

Here is more detail on the exception:

Ionic.Zip.ZipException: Cannot read that as a ZipFile
---> System.ArgumentException: An > item with the same key has already been added.
at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)
at System.Collections.Generic.Dictionary2.Insert(TKey key, TValue value, Boolean add)
at System.Collections.Generic.Dictionary
2.Add(TKey key, TValue value)
at Ionic.Zip.ZipFile.ReadCentralDirectory(ZipFile zf)
at Ionic.Zip.ZipFile.ReadIntoInstance(ZipFile zf)

Resolution:

Based on @Cheeso's suggestion, I can read everything from the stream, those avoiding duplicates, and path issues:

//using (ZipFile zip = ZipFile.Read(path))
using (ZipInputStream stream = new ZipInputStream(path))
{
    ZipEntry e;
    while( (e = stream.GetNextEntry()) != null )
    //foreach( ZipEntry e in zip)
    {
        if (e.FileName.ToLower().EndsWith(".cs") ||
            e.FileName.ToLower().EndsWith(".xaml"))
        {
            //var ms = new MemoryStream();
            //e.Extract(ms);
            var sr = new StreamReader(stream);
            {
                //ms.Position = 0;
                CodeFiles.Add(new CodeFile() { Content = sr.ReadToEnd(), FileName = e.FileName });
            }
        }
    }
}

解决方案

Read it with ZipInputStream.

The ZipFile class keeps a collection using the filename as the index. Duplicate filenames breaks that model.

But you can use the ZipInputStream to read in your zipfile. There is no collection or index in that case.

这篇关于如何处理与过长的路径解压的ZipFile /复制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆