您好我正在尝试使用ZipInputStream解压缩拉链(例如Unseekable输入流)。在 SharpZipLib 的帮助下。但这总是给我一个错误:
错误:
抛出异常:mscorlib.dll中的“ System.IO.DirectoryNotFoundException ” 错误:找不到路径'C:\ Users \ username \ Documents \ Visual Studio 2015 \ Projects \ WpfApplication1 \ WpfApplication1 \ bin \ Debug \ ASPNETWebAPISamples-master \'的一部分。
我甚至尝试了**ZipFile.ExtractToDirectory**
内置提取器和http://dotnetzip.codeplex.com/。他们都给了 Path too Long 例外。
我发现了几个关于路径太久异常的问题。但没有一个对我有用。
如何解决此错误? 感谢。
public static async Task HttpGetForLargeFileInRightWay()
{
using (HttpClient client = new HttpClient())
{
const string url = "https://github.com/tugberkugurlu/ASPNETWebAPISamples/archive/master.zip";
using (HttpResponseMessage response = await client.GetAsync(url, HttpCompletionOption.ResponseHeadersRead))
using (Stream streamToReadFrom = await response.Content.ReadAsStreamAsync())
{
try
{
Debug.Print("A");
UnzipFromStream(streamToReadFrom, Environment.CurrentDirectory);
Debug.Print("M");
}
catch (Exception ex)
{
Debug.Print("Error: " + ex.Message);
}
}
}
}
public static void UnzipFromStream(Stream zipStream, string outFolder)
{
ZipInputStream zipInputStream = new ZipInputStream(zipStream);
ZipEntry zipEntry = zipInputStream.GetNextEntry();
Debug.Print("B");
while (zipEntry != null)
{
String entryFileName = zipEntry.Name;
// to remove the folder from the entry:- entryFileName = Path.GetFileName(entryFileName);
// Optionally match entrynames against a selection list here to skip as desired.
// The unpacked length is available in the zipEntry.Size property.
byte[] buffer = new byte[4096]; // 4K is optimum
Debug.Print("C");
// Manipulate the output filename here as desired.
String fullZipToPath = Path.Combine(outFolder, entryFileName);
Debug.Print("D");
string directoryName = Path.GetDirectoryName(fullZipToPath);
Debug.Print("E");
if (directoryName.Length > 0)
{
Debug.Print("F");
Directory.CreateDirectory(directoryName);
Debug.Print("G");
}
Debug.Print("H");
// Unzip file in buffered chunks. This is just as fast as unpacking to a buffer the full size
// of the file, but does not waste memory.
// The "using" will close the stream even if an exception occurs.
using (FileStream streamWriter = File.Create(fullZipToPath))
{
Debug.Print("I");
StreamUtils.Copy(zipInputStream, streamWriter, buffer);
Debug.Print("J");
}
Debug.Print("K");
zipEntry = zipInputStream.GetNextEntry();
Debug.Print("L");
}
}
答案 0 :(得分:1)
问题是zipInputStream.GetNextEntry()
返回zip文件中的目录和文件。这本身不是问题,但您的代码只处理文件。要解决此问题,您需要检测fullZipToPath
变量是否包含文件或目录的路径。
这样做的方法是检查ZipEntry.IsDirectory
属性。将您的代码更改为:
if (!zipEntry.IsDirectory)
{
using (FileStream streamWriter = File.Create(fullZipToPath))
{
StreamUtils.Copy(zipInputStream, streamWriter, buffer);
}
}
下载并解压缩zip文件。
关于PathTooLongException
,请参阅this question了解详情。