【问题标题】:Unzip a zip file that is inside another zip with SharpZipLib使用 SharpZipLib 解压缩另一个 zip 中的 zip 文件
【发布时间】:2015-11-17 08:08:05
【问题描述】:

我正在尝试解压缩另一个 zip 中的 zip 文件。当我尝试获取第二个 zip 的 FileStream 时,它给了我一个错误。我如何查看内容? 这是我的代码:

try
        {
            FileStream fs = File.OpenRead(location);
            ZipFile zipArchive = new ZipFile(fs);
            foreach (ZipEntry elementInsideZip in zipArchive)
            {
                String ZipArchiveName = elementInsideZip.Name;
                if (ZipArchiveName.Equals("MyZMLFile.xml"))
                {
                    // I NEED XML FILES
                    Stream zipStream = zipArchive.GetInputStream(elementInsideZip);
                    doc.Load(zipStream);
                    break;
                }
                // HERE!! I FOUND ZIP FILE
                else if (ZipArchiveName.Contains(".zip"))
                {
                    // I NEED XML FILES INSIDE THIS ZIP
                    string filePath2 = System.IO.Path.GetFullPath(ZipArchiveName);
                    ZipFile zipArchive2 = null;
                    FileStream fs2 = File.OpenRead(filePath2);// HERE I GET ERROR: Could not find a part of the path
                    zipArchive2 = new ZipFile(fs2);
                }
            }
        }

【问题讨论】:

    标签: c# filestream sharpziplib


    【解决方案1】:

    此时,zip 存档名称不是磁盘上的文件。它只是 zip 存档中的一个文件,就像 xml 文件一样。您应该像处理 xml 文件一样为此使用 GetInputStream(),Stream zipStream = zipArchive.GetInputStream(elementInsideZip);然后你可以递归该方法再次提取这个zip。

    您需要先解压 zip 文件,然后递归调用相同的函数(因为该 zip 文件也可以包含一个 zip 文件):

    private static void ExtractAndLoadXml(string zipFilePath, XmlDocument doc)
    {
        using(FileStream fs = File.OpenRead(zipFilePath))
        {
            ExtractAndLoadXml(fs, doc);
        }
    }
    
    private static void ExtractAndLoadXml(Stream fs, XmlDocument doc)
    {
        ZipFile zipArchive = new ZipFile(fs);
        foreach (ZipEntry elementInsideZip in zipArchive)
        {
            String ZipArchiveName = elementInsideZip.Name;
            if (ZipArchiveName.Equals("MyZMLFile.xml"))
            {
                Stream zipStream = zipArchive.GetInputStream(elementInsideZip);
                doc.Load(zipStream);
                break;
            }
            else if (ZipArchiveName.Contains(".zip"))
            {
                Stream zipStream = zipArchive.GetInputStream(elementInsideZip);
                string zipFileExtractPath = Path.GetTempFileName();
                FileStream extractedZipFile = File.OpenWrite(zipFileExtractPath);
                zipStream.CopyTo(extractedZipFile);
                extractedZipFile.Flush();
                extractedZipFile.Close();
                try
                {
                    ExtractAndLoadXml(zipFileExtractPath, doc);
                }
                finally
                {
                    File.Delete(zipFileExtractPath);
                }
            }
        }
    }
    
    public static void Main(string[] args)
    {
        string location = null;
        XmlDocument xmlDocument = new XmlDocument();
        ExtractAndLoadXml(location, xmlDocument);
    }
    

    【讨论】:

      【解决方案2】:

      我不确定这是否可能。让我解释一下:

      读取 ZIP 文件需要随机访问文件 IO 来读取标题、文件表、目录表等。压缩的 ZIP(文件)流不会为您提供随机访问流,而是提供顺序流-- 这正是 Deflate 等算法的工作方式。

      要在 zip 文件中加载 zip 文件,您需要先将内部 zip 文件存储在某处。为此,您可以使用临时文件或简单的MemoryStream(如果不是太大)。这基本上为您提供了随机访问要求,从而解决了问题。

      【讨论】:

        猜你喜欢
        • 2021-04-29
        • 2018-08-02
        • 1970-01-01
        • 2022-11-09
        • 1970-01-01
        • 1970-01-01
        • 2021-10-08
        • 1970-01-01
        相关资源
        最近更新 更多