C# 如何处理路径过长/重复的ZipFile解压缩
在Windows中解压文件时,我偶尔会遇到路径问题C# 如何处理路径过长/重复的ZipFile解压缩,c#,zipfile,dotnetzip,C#,Zipfile,Dotnetzip,在Windows中解压文件时,我偶尔会遇到路径问题 这对于Windows来说太长(但在创建该文件的原始操作系统中可以) 由于不区分大小写而“重复” 使用DotNetZip,每当读取带有这些问题之一的zip文件时,ZipFile.Read(path)调用就会失效。这意味着我甚至不能尝试过滤掉它 using (ZipFile zip = ZipFile.Read(path)) { ... } 什么是处理读取这些文件的最佳方法 更新: 此处的示例zip: 副本: https://githu
ZipFile.Read(path)
调用就会失效。这意味着我甚至不能尝试过滤掉它
using (ZipFile zip = ZipFile.Read(path))
{
...
}
什么是处理读取这些文件的最佳方法
更新:
此处的示例zip:
副本:
https://github.com/MonoReports/MonoReports/tree/master/src/MonoReports.Model/DatasourceType.cs
以下是有关例外情况的更多详细信息:
Ionic.Zip.ZipException:无法将其作为ZipFile读取--->System.ArgumentException:已添加具有相同密钥的>项。
在System.ThrowHelper.ThrowArgumentException(例外资源)
在System.Collections.Generic.Dictionary
2.Insert(TKey键、TValue值、布尔加法)
在System.Collections.Generic.Dictionary中添加(TKey,TValue)
在Ionic.Zip.ZipFile.ReadCentralDirectory(ZipFile zf)中
在Ionic.Zip.ZipFile.ReadIntoInstance(ZipFile zf)上
分辨率:
根据@Cheeso的建议,我可以阅读流中的所有内容,避免重复的内容,以及路径问题:
//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 });
}
}
}
}
用ZipInputStream阅读它
ZipFile
类使用文件名作为索引保存一个集合。重复的文件名会破坏该模型
但是您可以使用ZipInputStream
读取ZipFile
。这种情况下没有集合或索引。对于PathToolLongException
问题,我发现您无法使用。相反,我所做的是调用;这真是奇迹
public static void Extract(string zipPath, string extractPath)
{
try
{
ProcessStartInfo processStartInfo = new ProcessStartInfo
{
WindowStyle = ProcessWindowStyle.Hidden,
FileName = Path.GetFullPath(@"7za.exe"),
Arguments = "x \"" + zipPath + "\" -o\"" + extractPath + "\""
};
Process process = Process.Start(processStartInfo);
process.WaitForExit();
if (process.ExitCode != 0)
{
Console.WriteLine("Error extracting {0}.", extractPath);
}
}
catch (Exception e)
{
Console.WriteLine("Error extracting {0}: {1}", extractPath, e.Message);
throw;
}
}
文件是.zip还是.gz?.zip(特别是从github zipfile下载的文件)可以显示错误吗?它是文件中的路径名吗?是目标文件位置太长吗?我更新了问题,使之更一般:重复文件和长路径都是一个问题,并包含示例。这实际上是我找到的最简单的解决方案。在发表此评论时,知道7za.exe在7-zip下载中,说明中带有“7-zip额外:…”可能会有所帮助。()参数
行不正确;它应该是Arguments=“x\”+zipPath+“\”-o\”+extractPath+“\”
(注意o开关和提取路径
之间没有空格。谢谢@sigil,根据您的回答是正确的,我更新了我的答案。回答很好。另外,为了避免进程在提示覆盖现有参数时停止,您可以将-y
添加到参数中