抛出异常:'System.IO.DirectoryNotFoundException' 和 System.IO.PathTooLongException

Exception thrown: 'System.IO.DirectoryNotFoundException' and System.IO.PathTooLongException

您好,我正在尝试使用 ZipInputStream 解压缩 zip(例如,对于不可搜索的输入流)来解压缩 zip。在 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 异常。

我发现了几个关于路径太长异常的问题。但是 none 对我有用。

如何解决这个错误? 谢谢。

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");
        }
    }

问题是 zipInputStream.GetNextEntry() returns zip 文件中的目录和文件。这本身不是问题,但您的代码只处理文件。要解决此问题,您需要检测 fullZipToPath 变量是否包含文件或目录的路径。

方法是检查 ZipEntry.IsDirectory 属性。将您的代码更改为:

if (!zipEntry.IsDirectory)
{
    using (FileStream streamWriter = File.Create(fullZipToPath))
    {
        StreamUtils.Copy(zipInputStream, streamWriter, buffer);
    }
}

并且 zip 文件已下载并解压缩。

有关 PathTooLongException 的详细信息,请参阅 this question