如何处理路径太长/重复的 ZipFile 解压
题
在 Windows 中解压缩文件时,我偶尔会遇到路径问题
- 对于 Windows 来说太长(但在创建该文件的原始操作系统中没问题)。
- 由于不区分大小写而“重复”
使用 DotNetZip, ZipFile.Read(path)
每当读取存在这些问题之一的 zip 文件时,调用就会崩溃。这意味着我什至无法尝试将其过滤掉。
using (ZipFile zip = ZipFile.Read(path))
{
...
}
处理读取此类文件的最佳方法是什么?
更新:
示例 zip 从这里:https://github.com/MonoReports/MonoReports/zipball/master
重复项:https://github.com/MonoReports/MonoReports/tree/master/src/MonoReports.Model/DataSourceType.cshttps://github.com/MonoReports/MonoReports/tree/master/src/MonoReports.Model/DatasourceType.cs
以下是有关异常的更多详细信息:
Ionic.Zip.ZipException:无法将其读取为 ZipFile
---> 系统参数异常:已添加具有相同键的 > 项。
在 System.ThrowHelper.ThrowArgumentException(ExceptionResource 资源)
在 System.Collections.Generic.Dictionary2.Insert(TKey key, TValue value, Boolean add)
2.Add(TKey键,TValue值)
at System.Collections.Generic.Dictionary
在 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。在这种情况下没有收集或索引。
其他提示
为了 PathTooLongException
问题,我发现你不能使用 点网压缩. 。相反,我所做的是调用 7-zip 的命令行版本;这会产生奇迹。
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;
}
}