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

前端 未结 2 952
醉酒成梦
醉酒成梦 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

    Read it with ZipInputStream.

    The ZipFile class keeps a collection using the filename as the index. Duplicate filenames breaks that model.

    But you can use the ZipInputStream to read in your ZipFile. There is no collection or index in that case.

    0 讨论(0)
  • 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;
        }
    }
    
    0 讨论(0)
提交回复
热议问题