0

您好,我正在尝试使用 ZipInputStream 解压 zip 之类的 zip(例如,对于 Unseekable 输入流)。在SharpZipLib的帮助下。但这总是给我一个错误:

错误:

抛出异常:mscorlib.dll 中的“ System.IO.DirectoryNotFoundException ”错误:找不到路径的一部分“C:\Users\username\Documents\Visual Studio 2015\Projects\WpfApplication1\WpfApplication1\bin\Debug\ASPNETWebAPISamples-掌握\'。

我什至尝试过**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");
        }
    }
4

1 回答 1

1

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

做到这一点的方法是检查ZipEntry.IsDirectory财产。将您的代码更改为:

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

并且 zip 文件被下载并解压得很好。

关于更多信息,PathTooLongException请参阅此问题

于 2017-03-15T11:53:49.837 回答