How to handle unzipping ZipFile with paths that are too long/duplicate

前端 未结 2 971
醉酒成梦
醉酒成梦 2021-01-11 22:47

When unzipping files in Windows, I\'ll occasionally have problems with paths

  1. that are too long for Windows (but okay in the original OS that created the file)
2条回答
  •  无人及你
    2021-01-11 23:29

    For the PathTooLongException problem, I found that you can't use DotNetZip. Instead, what I did was invoke the command-line version of 7-zip; that works wonders.

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

提交回复
热议问题